introspection

Java introspection: object to map

有些话、适合烂在心里 提交于 2019-11-27 17:59:23
I have a Java object obj that has attributes obj.attr1 , obj.attr2 etc. The attributes are possibly accessed through an extra level of indirection: obj.getAttr1() , obj.getAttr2() , if not public. The challenge : I want a function that takes an object, and returns a Map<String, Object> , where the keys are strings "attr1" , "attr2" etc. and values are the corresponding objects obj.attr1 , obj.attr2 . I imagine the function would be invoked with something like toMap(obj) , or toMap(obj, "attr1", "attr3") (where attr1 and attr3 are a subset of obj 's attributes), or perhaps toMap(obj, "getAttr1"

Calling a selector with unknown number of arguments using reflection / introspection

僤鯓⒐⒋嵵緔 提交于 2019-11-27 16:51:26
问题 Lately I wrote an application in java (for android) which used reflection to invoke methods of some objects. The argument number and type was unknown, meaning, I had a unified mechanism that received an object name, method name and array of parameters (using JSON) and invoked the specified method on the specified object with an array of the arguments (Object[] filled with arguments of the required types). Now I need to implement the same for iOS, I was able to invoke a selector when I knew

Java: How to find if a method is overridden from base class? [duplicate]

走远了吗. 提交于 2019-11-27 15:28:26
This question already has an answer here: How to quickly determine if a method is overridden in Java 8 answers How to find out if a method is overridden by child classes? For example, public class Test { static public class B { public String m() {return "From B";}; } static public class B1 extends B { } static public class B2 extends B { public String m() {return "from B2";}; } /** * @param args * @throws FileNotFoundException */ public static void main(String[] args) { B b1 = new B1(); System.out.println("b1 = " + b1.m()); B b2 = new B2(); System.out.println("b1 = " + b2.m()); } } Given an

Convert object's properties and values to array of key value pairs

左心房为你撑大大i 提交于 2019-11-27 13:59:42
I'm fairly new to JavaScript and am not sure this is possible to do but basically I would like to take an object and convert it into an array of strings in the format; array[0] = 'prop1=value1' The reasoning behind this is that I'm having a user enter a list of k=v pairs into a form, later it's written as an object within a json blob. Going from the key value csl to the json object was simple, now I need to go back the other way (I've received the JSON via an ajax call and want to populate a blank form). Is this possible in JavaScript? If not please offer a reasonable work around. Sample code;

How do I list all fields of an object in Objective-C?

喜欢而已 提交于 2019-11-27 13:33:33
If I have a class, how can I list all its instance variable names? eg: @interface MyClass : NSObject { int myInt; NSString* myString; NSMutableArray* myArray; } I would like to get "myInt", "myString", and "myArray". Is there some way to perhaps get an array of names that I can iterate over? I've tried searching the Objective-C documentation but couldn't find anything (and I'm not sure what this is called either). As mentioned, you can use the Objective-C runtime API to retrieve the instance variable names: unsigned int varCount; Ivar *vars = class_copyIvarList([MyClass class], &varCount); for

How to list all fields of a class (and no methods)?

浪子不回头ぞ 提交于 2019-11-27 13:25:43
问题 Suppose o is a Python object, and I want all of the fields of o , without any methods or __stuff__ . How can this be done? I've tried things like: [f for f in dir(o) if not callable(f)] [f for f in dir(o) if not inspect.ismethod(f)] but these return the same as dir(o) , presumably because dir gives a list of strings. Also, things like __class__ would be returned here, even if I get this to work. 回答1: You can get it via the __dict__ attribute, or the built-in vars function, which is just a

C++ iterate into nested struct field with boost fusion adapt_struct

℡╲_俬逩灬. 提交于 2019-11-27 12:56:05
Two stackoverflow answers suggest the approach using fusion adapt_struct to iterate over struct fields. The approach looks nice. However, how do you iterate into a field which itself is a struct? Following the previous answers, I come up with the code below. The problem is at the "#if 0" clause the code does not compile. As an alternative solution I created "decode()" function to take a void pointer to the target argument. That works, but loses the type information at compile time. Is there a better solution? struct Foo_s { int i; }; BOOST_FUSION_ADAPT_STRUCT( Foo_s, (int, i) ) struct Bar_s {

Inspect python class attributes

淺唱寂寞╮ 提交于 2019-11-27 11:48:28
I need a way to inspect a class so I can safely identify which attributes are user-defined class attributes. The problem is that functions like dir(), inspect.getmembers() and friends return all class attributes including the pre-defined ones like: __class__ , __doc__ , __dict__ , __hash__ . This is of course understandable, and one could argue that I could just make a list of named members to ignore, but unfortunately these pre-defined attributes are bound to change with different versions of Python therefore making my project volnerable to changed in the python project - and I don't like

What is the C# equivalent to Java's isInstance()?

心已入冬 提交于 2019-11-27 11:24:27
问题 I know of is and as for instanceof , but what about the reflective isInstance() method? 回答1: The equivalent of Java’s obj.getClass().isInstance(otherObj) in C# is as follows: bool result = obj.GetType().IsAssignableFrom(otherObj.GetType()); Note that while both Java and C# work on the runtime type object (Java java.lang.Class ≣ C# System.Type ) of an obj (via .getClass() vs .getType() ), Java’s isInstance takes an object as its argument, whereas C#’s IsAssignableFrom expects another System

Java introspection and reflection

↘锁芯ラ 提交于 2019-11-27 11:01:25
Can any one explain the use of Java reflection and introspection? When we need to use both? Anthony Forloney Reflection (taken from oracle java tutorial ) Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible.