reflection

Java Reflection: How to get the name of a variable?

Using Java Reflection, is it possible to get the name of a local variable? For example, if I have this: Foo b = new Foo(); Foo a = new Foo(); Foo r = new Foo(); is it possible to implement a method that can find the names of those variables, like so: public void baz(Foo... foos) { for (Foo foo: foos) { // Print the name ...

ILMerge and Custom Attributes on classes

I merged some dll files together and now my code that would find classes in one of the merged dll files cannot see the custom attributes I had on them. It can find all the classes fine but the attributes are missing. Any ideas how I can remedy this? Edit: Further investigating show that the attributes are on the class but the Guid for t...

EnumSet for pre-1.5 fake enums?

Recently I've been doing a lot of these enum Thing { /* etc etc */ static final Set<Thing> allThings = EnumSet.allOf(Thing.class); } I want something similar in pre 1.5 Java, i.e. I want something like: final class Thing { private Thing(); // control instances within class static final Thing instance0 = new Thing...

Data mapping code or reflection code?

Getting data from a database table to an object in code has always seemed like mundane code. There are two ways I have found to do it: 1) have a code generator that reads a database table and creates the class and controller to map the datafields to the class fields or 2) use reflection to take the database field and find it on the cla...

Get Integral Value of Boxed Enum

Hey All, I have decided it is impossible to do the following (equivalent) enum operations via reflection - as the Enum class has no operators, and nor does the emitted code expose any operators: object boxedEnum = MyEnum.Flag1 | MyEnum.Flag2; boxedEnum &= ~MyEnum.Flag2; // Remove the flag. So I am currently doing the following (equiv...

override java final methods via reflection or other means?

This question arise while trying to write test cases. Foo is a class within the framework library which I dont have source access to. public class Foo{ public final Object getX(){ ... } } my applications will public class Bar extends Foo{ public int process(){ Object value = getX(); ... } } The unit test case is ...

Get event parameters via reflection

Hi, I can't work out how to get the parameter types for an event. For instance, I can only see using a MethodInfo to get parameters, but I have either an EventInfo or a FieldInfo. What I want is to be able to get 'Boolean' from this: Public Event EventName(ByVal sender As Object, ByVal value As Boolean) I could theoretically try so...

Determine if object derives from collection type

I want to determine if a generic object type ("T") method type parameter is a collection type. I would typically be sending T through as a Generic.List but it could be any collection type as this is used in a helper function. Would I be best to test if it implements IEnumerable<T>? If so, what would the code look like? Update 14:17 G...

Implementing an Interface on a dynamic type with events

I am taking in an interface, looping through the .GetEvents() return array and attempting to implement the event on my dynamic type. At the point when I try to call TypeBuilder.CreateType(), I am greeted with this lovely error: "Application method on type from assembly is overriding a method that has been overridden." If I comment ...

Getting the name of the parameter passed into a method

Duplicate: http://stackoverflow.com/questions/742350/determine-the-name-of-the-variable-used-as-a-parameter-to-a-method Is there any way to retrieve the name of a parameter that was passed into a method e.g. int someParameter = 1; Method(someParameter); public void Method(int parameter) { // I want the name of 'parameter' which wi...

SetValue on PropertyInfo instance error "Object does not match target type" c#

Been using a Copy method with this code in it in various places in previous projects (to deal with objects that have same named properties but do not derive from a common base class or implement a common interface). New place of work, new codebase - now it's failing at the SetValue with "Object does not match target type" even on very s...

Using the Web Application version number from an assembly (ASP.NET/C#)

How do I obtain the version number of the calling web application in a referenced assembly? I've tried using System.Reflection.Assembly.GetCallingAssembly().GetName() but it just gives me the dynamically compiled assembly (returning a version number of 0.0.0.0). ...

Accessing information in the AssemblyInfo.cs using Reflection in a Web Site

I have created a DLL that will gather information from the AssemblyInfo.cs. In the class constructor I am using Reflection to get the top-most application that is running. public class AppInfo() { public AppInfo() { System.Reflection.Assembly assembly = System.Reflection.Assembly.GetEntryAssembly(); ...

Creating a factory for which can support both real object & a NullObject

I want to create a factory which will create a smack XMPPConnection. The factory should return the real XMPPConnection or a NullObject if the connection could not be made. However smack's XMPPConnection is a concrete class. It does not implement any interfaces, so I can't use java dynamic proxy API to proxy the sucker. I could extend t...

On-the-fly fields from Static Reflection?

I've been studying up on static reflection with LINQ expressions - very cool! One thought I had - is it possible for one class to 'generate' fields on one class based on static reflection done on another class? I'm thinking specifically of the Builder pattern I've seen here many times. I would like to do a fluent-nhibernate-style proper...

How to disable Java security manager?

Is there any way to completely disable Java security manager? I'm experimenting with source code of db4o. It uses reflection to persist objects and it seems that security manager doesn't allow reflection to read and write private or protected fields. My code: public static void main(String[] args) throws IOException { System.out....

Is there a way to call a method when any property of a class is set?

So what I'm trying to do is call a single propertyWasSet() function when any property within a C# class is set (conversely, propertyWasGot() when it is get). I would also like to know which property's 'get' was invoked. I would like to maintain a dictonary of properties that are 'set', and check upon the 'get' action if they have been s...

Reflecting on interfaces of a type in C#

How can I reflect on the interfaces implemented by a given type (of class or interface) in .NET? As the type being reflected on will be generic (both in the implementation sense as well as the semantic sense), it would preferable to do this without having to hardcode an Assembly name, though I realise that I can get this name from the ...

.NET Reflection on unloadable types

I have a library which contains unloadable type (whenever I try to access them I get a TypeLoadException). Therefore, I can't use the System.Reflection namespace to inspect those types. Is there some other way to reflect on those types. Of course, I don't expect to be able to use any methods, I just want to see basic info such as method ...

IllegalArgumentException: object is not an instance of declaring class

How is it possible that the "foo" exception is not thrown, but a subsequent call of invoke() throws the below exception? if (method.getDeclaringClass() != object.getClass()) throw new RuntimeException("foo"); method.invoke(object); Thrown exception: java.lang.IllegalArgumentException: object is not an instance of declaring clas...