0

I have to create objects using reflection, But I am facing a problem with how to Set the name of the instance specified in the string e.g. the string contains:

new instancename java.util.Arraylist

where instancename refers to the new instance of Arraylist, I know how to create an object through reflection but I cannot figure out a way to assign a specific name to an instance from a string. Any Help? Thanks in advance.

5
  • 1
    So, do you want to assign the created instance to a given variable name? If so, try with java.lang.reflect.Field and/or java.lang.reflect.Method Commented Mar 10, 2014 at 4:45
  • @OscarRyz I want to be able to call functions of Arraylist from instancename. e.g. instancename.add etc. What should I do? Commented Mar 10, 2014 at 4:52
  • Could you state what you want to do in pseudo Java code? Commented Mar 10, 2014 at 4:57
  • Please refer stackoverflow.com/questions/1093443/… Commented Mar 10, 2014 at 5:26
  • Oh, so you want to invoke a method of the created instance.. I'll post an example Commented Mar 11, 2014 at 14:18

4 Answers 4

1

The name of the variable that references the created object does not define the object, and can't be assigned during runtime. So to create a "name" to referece an object, and you can use this object later through your "script" (the strings you mention), you would need to implement this by yourself.

One way to do this is to have a Map<String, Object>, where you add a name and points the value (of the Map) to the object every time you create a new object (when processing strings like the example). The problem with this approach is that if you never delete this Map entries, this object will never be freed from memory by the Garbage Collector. To fix that, you should have some way in your "script" to tell when to free an object created.

Sign up to request clarification or add additional context in comments.

Comments

0

Java calls the toString method of the class whenever it wants to print a class instance name. If toString is not implemented in your class, the default toString method from the Object is called. Apache Commons Lang has a functionality called ReflectionToStringBuilder. You might want to take a look.

Comments

0

The variable name used to hold a reference is:

  • irrelevant
  • only available at compile time

It makes no sense to care what a variable name is, nor could you do anything with it even if you could define a variable at runtime, which you can't.

The thing close to what you want is to use Map:

Map<String, Object> objects = new HashMap<>();
// extract className and name from input
Object object = Class.forName(className);
objects.put(name, object);

Then pass the reference to the map back to where you need the created objects.

Comments

0

The following program creates an instance of the class you specify and then invokes any method with a single parameter of type object;

import java.lang.reflect.*;


class InvokeMethod {
    public static void main( String ... args ) throws 
        ClassNotFoundException,
        NoSuchMethodException,
        IllegalAccessException,
        InvocationTargetException, 
        InstantiationException {

        String clazz  = args[0]; // which class to create
        String method = args[1]; // which method to invoke
        String param  = args[2]; // parameter to that method

        Class c  = Class.forName(clazz);

        Method m = c.getMethod(method, Object.class);
        Object o = c.newInstance();

        Object result = m.invoke( o, param );
        System.out.printf("%s.%s(%s)=%s", clazz, method, param, result );

    }
}

example output:

$ java InvokeMethod java.util.ArrayList add  "Hello world"

java.util.ArrayList.add(Hello world)=true

$ java InvokeMethod java.lang.String valueOf  "Hello world"

java.lang.String.valueOf(Hello world)=Hello world

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.