reflection

Java NoSuchFieldError when using Reflection

怎甘沉沦 提交于 2021-01-28 06:41:50
问题 I'm trying to modify a public static final String[] field I made in ClassA, and then modify it in ClassB using reflection. However I get a NoSuchFieldException. java.lang.NoSuchFieldException: test at java.lang.Class.getField(Unknown Source) at packageA.ClassA.<init>(ClassA.java:17) ClassA is located in packageA and ClassB is located in packageB if that matters. Class A , creates the field and calls ClassB : package packageA; import packageB.ClassB; public class ClassA { // Create final

Get dictionary name by string in C#

喜夏-厌秋 提交于 2021-01-28 06:10:34
问题 I have a class which contains 4 dictionaries. public class FishInformation { public int FishId { get; set; } public Dictionary<string,int> DicGroup { get; set; } public Dictionary<string, int> DicEvent { get; set; } public Dictionary<string, int> DicAudience { get; set; } public Dictionary<string, int> DicType { get; set; } } I want get the dictionary name by a string and add items to it. So this question suggests using System.Reflection to do so. this is the code I tried: FishInformation

Type aliases screw up type tags?

∥☆過路亽.° 提交于 2021-01-28 03:52:38
问题 Why don't type tags work with type aliases. E.g. given trait Foo object Bar { def apply[A](implicit tpe: reflect.runtime.universe.TypeTag[A]): Bar[A] = ??? } trait Bar[A] I would like to use an alias within the following method, because I need to type A around two dozen times: def test { type A = Foo implicit val fooTpe = reflect.runtime.universe.typeOf[A] // no funciona Bar[A] // no funciona } Next try: def test { type A = Foo implicit val fooTpe = reflect.runtime.universe.typeOf[Foo] // ok

How to get the list of annotations of a class and its superclass

烈酒焚心 提交于 2021-01-28 03:30:27
问题 I'm writing a method supposed to retrieve all annotations of a specific method declaring class and its superclasses. By using the method getAnnotations() on the declaring class, the resulting table contains only the declaring class annotations and the superclass annotations are ignored. If I remove the annotations of the declaring class, then the superclass annotation are present. What am I missing here? The simplified method retrieving the annotations : public void check(Method invokedMethod

Retrieve annotations from KType

对着背影说爱祢 提交于 2021-01-28 03:24:37
问题 I have a simple TYPE_USE annotation: @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) public @interface Cool { } And the following sample Kotlin class: class Item( id: Long? = null, var names: List<@Cool String> = emptyList()) Is there any way to extract the annotation using Java reflection? Item.class.getMethod("getName").getAnnotatedReturnType() loses the annotations, same with getting the field. Can I even get the annotation from Kotlin? Item:

Can a method variable be accessed using reflection?

ⅰ亾dé卋堺 提交于 2021-01-28 01:50:42
问题 How can I access a variable defined inside a method by using reflection? I have to create criteria in Hibernate. The database is attribute based. Consider a movie. A movie can have many languages and many genres. e.g. Movieid Property Value 1 Language Hindi 1 Language English 1 genre action 2 genre comedy Let's assume there are a thousand records of different movies. The user selects a set of genres(action) and languages(hindi, english) to filter the result. Now, I have to create criteria in

HasThis & ExplicitThis calling conventions

时光毁灭记忆、已成空白 提交于 2021-01-27 18:40:35
问题 I come across HasThis and ExplicitThis calling conventions on .NET Framework reference source, and thus I begin to wonder: When are they set by compiler? Are there any examples using this combination of calling conventions (in "real world" managed program)? MSDN has described them as: ExplicitThis Specifies that the signature is a function-pointer signature, representing a call to an instance or virtual method (not a static method). If ExplicitThis is set, HasThis must also be set. The first

Translating a MethodInfo object obtained from an interface type, to the corresponding MethodInfo object on an implementing type in C#?

本小妞迷上赌 提交于 2021-01-27 17:05:22
问题 The question I have is this: If I have the MethodInfo object, for a method, obtained from an interface type, and I also have the Type object for a class that implements this interface, but it implements the said method with an explicit implementation, how do I correctly obtain the corresponding MethodInfo object for the implementing method in that class? The reason I need to do this is that implementing methods can have some attributes applied to them, and I need to find these through

Python code reflection and modification

别来无恙 提交于 2021-01-27 14:50:35
问题 Perhaps I'm not using the proper term, but I'm looking to take a block of Python code (in Python), get the token tree for the code, make some kind of modification, and re-assemble it to Python. For instance, consider this block of code: def x(y): b = 2 if y == b: foo(y) I would like to be able to take this and programmatically do this: def x(y): b = 2 if y == b: bar(y) I can't imagine that there's not a library that does something like this. Thanks in advance. EDIT Perhaps I wasn't entirely

CodeDom : compile partial class

大憨熊 提交于 2021-01-27 14:42:26
问题 I'm attempting to compile code in a text file to change a value in a TextBox on the main form of a WinForms application. Ie. add another partial class with method to the calling form. The form has one button (button1) and one TextBox (textBox1). The code in the text file is: this.textBox1.Text = "Hello World!!"; And the code: namespace WinFormCodeCompile { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //