Skip to main content

Posts

Showing posts with the label reflection

Modifying final fields in Java

Let's start with a simple test case: import java.lang.reflect.Field; public class Test { private final int primitiveInt = 42; private final Integer wrappedInt = 42; private final String stringValue = "42"; public int getPrimitiveInt() { return this.primitiveInt; } public int getWrappedInt() { return this.wrappedInt; } public String getStringValue() { return this.stringValue; } public void changeField(String name, Object value) throws IllegalAccessException, NoSuchFieldException { Field field = Test.class.getDeclaredField(name); field.setAccessible(true); field.set(this, value); System.out.println("reflection: " + name + " = " + field.get(this)); } public static void main(String[] args) throws IllegalAccessException, NoSuchFieldException { Test test = new Test(); test.changeField("primitiveInt", 84); System.out.println("direct: primitiveInt = " + test.getPrimitiveInt()); test.

Invoking variable-arity methods with Java reflection?

I would like to understand what might be going on with invoking variable-arity methods using Java reflection. Let's say we have a simple method: void doAllTheThings(Object ... things) { // ...which does something with all the things... } And we want to invoke it dynamically, so we grab the method through reflection: Method doItAll = Superklass.getDeclaredMethod("doAllTheThings", Object[].class); And pass in an array: Object[] allTheThings = new Object[] { "abc", true, 15 }; doItAll.invoke(allTheThings); Now, this doesn't seem to work quite like my intuition had figured; in particular, I seem to be getting various shades of IllegalArgumentException when I try invoking a method with varargs like this. There's clearly something I'm missing here. My guess is this is related somehow to how the variables get marshalled into the varargs. I've found this four year old blog post which seems to be talking about the same issue , but am unable

Instantiate a type based on json and metadata using lift-json

I would like to deserialise Scala case classes that have been serialised using lift-json. The problem I am having is, I don't know how to invoke the generic method extractOpt[A] method below: someString:String = {...} JsonParser.parse(someString).extractOpt[A] The type of [A] is going to depend on metadata, for example the class name of [A] but for the life of me I can't work out how to make the call using reflection. In c# I would just be able to set the generic type for a call on extractOpt[A] using reflection. I fear my problems are something to do with Java type erasure. I am going to have a lot of case classes so I really want to avoid having to create some kind of hand crafted map from {metadata} -> classOf[]. I have full control over what the metadata associated with someString is. If it helps understand why I have this issue, I am implementing event sourcing, and all my [A] types are going to be persisted events. Any ideas what I can do?

Is there a way to block a class from being reflected upon?

I am making a cipher class while teaching myself about java's security api. This class is going to have some sensitive stuff in it, such as the type of encryption and the like. All of this can be reflectively retrieved it some one had the motivation. I have used reflection to bypass private variables and methods before (not proud of it), so I know that can be done. Is there a way to wholly prevent reflection from working on an entire class - or even parts of it, or does that go against java's - more specifically the security api - design?