reflection
Does Java have an “is kind of class” test method
if(object instanceof WhereStatement) { WhereStatement where = (WhereStatement) object; doSomething(where); } Note that code like this usually means that your base class is missing a polymorphic method. i.e. doSomething() should be a method of Statement, possibly abstract, that is overridden by sub-classes.
How do you put a Func in a C# attribute (annotation)?
I don’t think it is possible, c# only allows you to use Attributes with constant values only. One way of doing this would be to create a class instead of a func, and then you pass the type of that class into the attribute. You’d then need to instantiate it with reflection and execute a … Read more
how to get parameter’s annotation in java?
getParameterAnnotations returns one array per parameter, using an empty array for any parameter which doesn’t have any annotations. For example: import java.lang.annotation.*; import java.lang.reflect.*; @Retention(RetentionPolicy.RUNTIME) @interface TestMapping { } public class Test { public void testMethod(String noAnnotation, @TestMapping String withAnnotation) { } public static void main(String[] args) throws Exception { Method method = Test.class.getDeclaredMethod (“testMethod”, … Read more
`Type.GetProperties` property order
The order simply isn’t guaranteed; whatever happens…. Happens. Obvious cases where it could change: anything that implements ICustomTypeDescriptor anything with a TypeDescriptionProvider But a more subtle case: partial classes. If a class is split over multiple files, the order of their usage is not defined at all. See Is the “textual order” across partial classes … Read more
Get only public methods of a class using Java reflection
I don’t know how you are using Modifier, but here’s how it’s meant to be used Method[] allMethods = Test.class.getDeclaredMethods(); for (Method method : allMethods) { if (Modifier.isPublic(method.getModifiers())) { System.out.println(method); // use the method } }
C# creating instance of class and set properties by name in string
You could use Reflection: using System; using System.Reflection; public class Foo { public string Bar { get; set; } } public class Program { static void Main() { string name = “Foo”; string property = “Bar”; string value = “Baz”; // Get the type contained in the name string Type type = Type.GetType(name, true); // … Read more
How to instantiate a type dynamically using reflection?
Try Activator.CreateInstance(Type) http://msdn.microsoft.com/en-us/library/wccyzw83.aspx
How can I create a generic array in Java?
I have to ask a question in return: is your GenSet “checked” or “unchecked”? What does that mean? Checked: strong typing. GenSet knows explicitly what type of objects it contains (i.e. its constructor was explicitly called with a Class<E> argument, and methods will throw an exception when they are passed arguments that are not of … Read more
What reflection capabilities can we expect from Scala 2.10?
update 2012-07-04: Daniel SOBRAL (also on SO) details in his blog post “JSON serialization with reflection in Scala! Part 1 – So you want to do reflection?” some of the features coming with reflection: To recapitulate, Scala 2.10 will come with a Scala reflection library. That library is used by the compiler itself, but divided … Read more