reflection

Class.forname(“name”).newInstance() vs name.class.newInstance : Difference in usage perspective

坚强是说给别人听的谎言 提交于 2020-01-04 05:24:07
问题 I will start with my example: I have a class classload.Loadable . package classload; public class Loadable { static{ System.out.println("Loaded already....."); } public Loadable(){ System.out.println("Now created....."); } } which will be loaded and created instance in the following 2 ways. First: public static void main(String[] args) throws Exception { System.out.println("Starting ....."); Class.forName("classload.Loadable").newInstance(); } Second: public static void main(String[] args)

Create DynamicMethod from Action<T> instructions

拜拜、爱过 提交于 2020-01-04 05:15:36
问题 I am playing around with DynamicMethod and aim to do the following: I have an Action from which I obtain the IL code as bytes using GetILAsByteArray() . From this bytes I would like to create a Dynamic method and execute is. Here an example of what I am trying to do: class Program { static void Main(string[] args) { //Create action and execute Action<string> myAction = s => { Console.WriteLine("Hello " + s); }; myAction("World"); //Get IL bytes byte[] ilBytes = myAction.GetMethodInfo()

Performance and caching of generated code in Scala

 ̄綄美尐妖づ 提交于 2020-01-04 05:14:28
问题 I need to generate an implementation of a trait at runtime, then execute a known method on an instance of the trait. In this example I'm running A 's a method: import reflect.runtime._, universe._, tools.reflect.ToolBox package p { trait A { def a: String } val tb = currentMirror.mkToolBox() val d: A = tb.eval(q"""class C extends p.A { def a = "foo" }; new C""").asInstanceOf[A] println(d.a) // "foo" } A few questions around this: The use case is very performance-sensitive (running generated

IllegalArgumentException: the error message does not make sense

孤街浪徒 提交于 2020-01-04 05:01:11
问题 I am trying to debug a Java application that is relying on Reflection. Right now the error I get is the following: java.lang.IllegalArgumentException: Can not set int field DataStructures.StackAr.topOfStack to java.lang.Integer at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146) at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150) at sun.reflect.UnsafeFieldAccessorImpl.ensureObj

Why does the outer class appear twice in the generic type name? [duplicate]

≯℡__Kan透↙ 提交于 2020-01-04 04:46:27
问题 This question already has answers here : How can I determine the type of a generic field in Java? (6 answers) Closed 2 years ago . Q: Why does the name of the containing class appear twice? Context: I'm generating code and the goal is to get the declaration of the field, as written in the source (fully qualified is fine, but I need the type parameter): test.Foo.Bar<java.lang.String> package test; import java.lang.reflect.Field; import java.lang.reflect.Type; public class Foo { public static

Generating a list of child classes with reflection in .NET 3.5

限于喜欢 提交于 2020-01-04 04:31:09
问题 At runtime, I would like to specify a parent class, and then the program would generate a list of all children classes (of however many generations). For example, if I had Entity as a parent, and Item:Entity and Actor:Entity , there would be two strings, "Actor" and "Item". I see that System.Reflection.TypeInfo is exactly what I am looking for. However, it appears this is exclusive to .NET 4.5, and my environment is unfortunately stuck at 3.5. Is there an alternative way to do this in .NET 3

Find the location in code of a system.out.println

我只是一个虾纸丫 提交于 2020-01-04 03:55:20
问题 Lets say I'm working in a very large project, and have noticed an empty print line, so I'm assuming there is a System.out.println(""); located somewhere in the code. How would I go about trying to figure out where it is, short of just searching the entire project for all occurrences of System.out.println? 回答1: You could implement your own PrintStream and use System.setOut to replace the default stdout. Then either put a debugging marker inside the class (if an empty string is printed), or

Find the location in code of a system.out.println

霸气de小男生 提交于 2020-01-04 03:55:12
问题 Lets say I'm working in a very large project, and have noticed an empty print line, so I'm assuming there is a System.out.println(""); located somewhere in the code. How would I go about trying to figure out where it is, short of just searching the entire project for all occurrences of System.out.println? 回答1: You could implement your own PrintStream and use System.setOut to replace the default stdout. Then either put a debugging marker inside the class (if an empty string is printed), or

Invoking reflected case class constructor in Scala

妖精的绣舞 提交于 2020-01-04 03:47:22
问题 I can get the default constructor of a case class via static reflection thusly: val symbol = currentMirror.classSymbol(myObj.getClass).typeSignature.typeSymbol.asClass val ctor = symbol.primaryConstructor From here I can do nifty things like introspect details of its fields, etc. But how can I now invoke the constructor method? I could dig into myObj's class, but if it has multiple constructors, is there a straightforward way to match the right constructor to the one I got from

Delphi use reflection in a class procedure for the getting dynamic class type

无人久伴 提交于 2020-01-04 03:37:15
问题 I want use reflection on the current class inside a class procedure/function (static method). How can I do without using the "Self" keyword? And without harcode the class name: this procedure should be override in the descendants. class procedure AAA.SetTableAndSequence; var c : TRttiContext; t : TRttiType; begin c := TRttiContext.Create; try t := c.GetType(Self.ClassType); ... finally c.Free; end; end; 回答1: You can use ClassInfo and GetType: class procedure AAA.SetTableAndSequence; var c: