0

I have a method that needs a Class object which it will construct multiple times:

Class<?> clazz = MyClass.class;
register(clazz);

For some registers, I want to use anonymous classes, but for that I need a Class<?> object from it.

I would use it for registering multiple classes that are very similar, but (in this example) have a different name:

String[] nameList = { "name1", "name2" }; // and a lot more
for (final String name : nameList) {
    // How would I do the next line?
    // It is supposed to pass the Class<?> for an anonymous class overriding getName()
    register(AnAnonymousClass { 
        @Override
        public String getName() {
            return name;
        });
    }
}

Is there any way of doing this, or does my system have a design flaw? If yes, how would I achieve it then? By the way, I cannot pass the name via constructor because the constructor has to have the same parameters for all classes (because it will be constructed using reflection).

11
  • 3
    You can use the getClass() method on any object. But if you're trying to set up an anonymous inner class without creating an object of that class, offhand I'm not sure Java supports this. Maybe you can define a named local class with a name and use that. Commented Jul 25, 2014 at 21:24
  • 2
    OK, JLS §15.9.5 says anonymous classes are derived from "class instance creation expressions", so I don't think it's possible to have an anonymous class without a new object. So I think you have to give up on making the class anonymous. It doesn't gain you anything anyway. Commented Jul 25, 2014 at 21:35
  • 1
    A loop doesn't create a class on each iteration. It could create an instance on each iteration. There is only one anonymous inner class here, only an instance you create, not the class, which know what value you have given the name. Commented Jul 25, 2014 at 21:37
  • 1
    Why not using a factory pattern with one implementation based on reflection ? Thus, you could pass anonymous class as implementation of that factory. Commented Jul 25, 2014 at 21:42
  • 2
    @Frithjof I'm not clear on what your needs are, but it appears that you're trying to define new classes dynamically, at run time, and I don't think you can do that. I'm not sure what you're trying to "register" and what you're planning to do with the things you're registering. But I do think your design is unworkable; however, without more information about what you're trying to accomplish I can't give you any ideas about how to make it work. Commented Jul 25, 2014 at 21:50

1 Answer 1

2

I believe that Peter Lawrey is right in that you are not creating different classes in your loop, only instances.

Are all the classes that you need to "register" your own? If so, you could create an interface for them all that contains a copy method. Instead of registering classes and using reflection to create instances, you register an original object and copy that, like so:

    // Interface with methods that all these classes have in common 
interface MyClassInterface {
    MyClassInterface copy();
    String getName();
}

@Test
public void testMyClasses() {
    List<String> names = new ArrayList<>();

    // Example list of names
    names.add("adam");
    names.add("ben");
    names.add("carl");

    List<MyClassInterface> objects = new ArrayList<>();


    // Define specialized class with special implementation of getName
    class MyClassWithName implements MyClassInterface {
        private String name;
        public MyClassWithName(String name) {
            this.name = name;
        }

        @Override
        public MyClassInterface copy() {
            return new MyClassWithName(name);
        }

        @Override
        public String getName() {
            return name;
        }

    }

    for (final String name: names) {
        // "register" object
        objects.add(new MyClassWithName(name));
    }

    for (MyClassInterface object: objects) {
        System.out.println("original name " + object.getName());
        // Construct copies as needed
        MyClassInterface object2 = object.copy();
        System.out.println("copy name " + object2.getName());
    }

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

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.