开发者

Why is my Java generics code not syntactically correct?

Could you please explain why this code is not syntactically correct?

private void addEditor(final Class<? extends FieldEditor> fieldEditorClass, final Composite parent, final PropertyKey propertyKey, final String displayName){
    final Composite composite = new Composite(parent, SWT.NULL);
    composite.setLayout(new GridLayout());
    composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
    final FieldEditor fieldEditor = new >>fieldEditorClass<< (propertyKey.toString(), displayName, composite);
    initializeFieldEditor(fieldEditor);     
}

on line 5, the part b开发者_Go百科etween >> and << is underlined in red and it says "can not be resolved to a type".

I hope you can see what I am trying to achieve here. By passing a SomeEditor.class to this method I then want to create an object of this class and initialise it. How can I fix the problem on line 5?


Basically, I want to parametrise the concrete FieldEditor class being instantiated by this code. I have several methods in my code that are essentially identical, except that each instantiates a different class of FieldEditor.


You need to instantiate the class using e.g. Reflection. See this answer on SO.


fieldEditorClass is variable name not a class name. So you can't call new fieldEditorClass (...) on it

You could try

 fieldEditorClass.getClass().getDeclaredConstructor(Class<?>... parameterTypes);

And then run newInstance(Object... initargs) on the gained constructor


By passing a SomeEditor.class to this method I then want to create an object of this class and initialise it.

Sounds like you want a FieldEditor factory to me. A simple example of how I'd do it might look like this:

package generics;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Factory<T extends MyClass>
{
   private Class<T> clazz;

   public T create(String s) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException
   {
      Constructor<T> constructor = clazz.getDeclaredConstructor(String.class);

      return constructor.newInstance(s);
   }
}

As you can see, you didn't provide enough code to sort out your problem. Substitute your class and parameters and it should work.

UPDATE:

Here's the specific case for your code:

package generics;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class FieldEditorFactory<T extends FieldEditor>
{
    private Class<FieldEditor> clazz;

    public FieldEditorFactory(Class<FieldEditor> clazz)
    {
        this.clazz = clazz;
    }

    public T create(String displayName, PropertyKey propertyKey, Composite parent) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException
    {
        Composite composite = new Composite(parent, SWT.NULL);
        composite.setLayout(new GridLayout());
        composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));

        Constructor<T> constructor = (Constructor<T>) clazz.getDeclaredConstructor(String.class, PropertyKey.class, Composite.class);

        // Why on earth wouldn't the constructor return you a fully-initialized instance?
        // I wouldn't design it with this method.
        //initializeFieldEditor(fieldEditor);

        return constructor.newInstance(displayName, propertyKey, parent);
    }
}


There is rarely any need to use reflection. It is very much better to use just the bog standard language features. A factory is a perfect fit here.

public interface FieldEditorFactory<T extends FieldEditor> {
    T newFieldEditor(
         String propertyKeyName, String displayName, Composite composite
    );
}

(I have added generics, although your example doesn't need them.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜