Skip to content
Advertisement

Tag: reflection

Casting to array type using reflection

I find myself in a situation where it seems I would need to be able to cast an Object that is an array of some other, non-primitive type, into its concrete array type, to pass to a generic. The same thing is trivial to get to work with non-array types: desiredType.cast(o) yields an object of the correct type. Would someone

Get Values of variables under an Enum

I want to write into a file/print the constants of an enum, as well as the values of their variables. For example, here is what I thought of doing: However, I am not fully sure on how to do such a thing, as I only recently began working with reflection. This is the code that I currently have. Here is

using Constructor without “new” word in java

So I’m trying to learn to use Reflection in java(so i think its called), so I did a little project to make an object without the optimal Constructor pattern with the “new” word. Unfortunately, it shows me an error for the class type array for the constructor. Here is my project: SomeClass.java: } Student.java: } The error: What is the

Automatically delegating all methods of a java class

Say I have a class with many of public methods: Now I would like to create a wrapper class which would delegate all the methods to wrapped instance (delegate): Now if MyClass has a lot of methods I would need to override each of them which is more or less the same code which just “delegates”. I was wondering if

Access a private field for a junit test

I am trying to initialize a private field from a class in order to unit test its methods. For that I am using reflection but I am always getting an IllegalArgumentException and I don’t understand what I am doing wrong. My code looks something like this: I get this error when I am trying to run the test: I also

JSONPath resolver for Java objects

How can I get a value from an Java object instead from a JSON string by applying a JSONPath expression? I receive a Java object that is created from a JSON string (via Jackson, no way to influence it): I further have some JSONPath expressions as Java Strings reflecting values in the object (they might be much more complex): I

Accessing non-visible classes with reflection

I am trying to get an instance of a non-visible class, AKA package private class, using reflection. I was wondering if there was a way to switch the modifiers to make it public and then access it using Class.forName. When I try that now it stops me saying I can’t do it. Unfortunately there is no setAccesible method of the

Advertisement