Создать конструктор и новый экземпляр dexmaker

В настоящее время я пытаюсь создать класс с использованием DexMaker:

public class ClassHandler {
Context context;
final DexMaker dexMaker = new DexMaker();
public ClassHandler(Context context)
{
    this.context = context;
}
public void createObjectClass(String objectName, List<Element> attributes)
{
    TypeId<?> myObject = TypeId.get("L" +objectName +";");
    dexMaker.declare(myObject, objectName+ ".generated", Modifier.PUBLIC, TypeId.OBJECT);
     generateZeroConstuctor(myObject);
    generateAttributes(myObject, attributes );
}
public void generateZeroConstuctor(TypeId<?> declaringType){
    MethodId<?, ?> constructor = declaringType.getConstructor();
    Code constructorCode = dexMaker.declare(constructor, Modifier.PUBLIC);
    constructorCode.returnVoid();
}

public void generateAttributes(TypeId<?> declaringType, List<Element> attributes)
{
    for (Element attribute: attributes)
    {
        if (attribute.getAttribute("type").equals("string")) {
            FieldId field = declaringType.getField(TypeId.STRING, attribute.getAttribute("name"));
        }
        else if (attribute.getAttribute("type").equals("phonenumber")) {
            FieldId field = declaringType.getField(TypeId.STRING, attribute.getAttribute("name"));
        }
    }
}

public Class<?> loadClass(String className){
    File outputDir = new File(context.getFilesDir(),".");
    ClassLoader loader = null;
    try {
        loader = dexMaker.generateAndLoad(MainActivity.class.getClassLoader(), outputDir);
        Class<?> xClass = null;
        xClass = loader.loadClass(className);
        return xClass;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

}введите код сюда

Но когда я пытаюсь создать новый экземпляр для этого класса, я всегда получаю сообщение об ошибке:java.lang.NoSuchMethodException: init []

    Class<?> myClass = classHandler.loadClass(objectName);
    try {
        Object myObject = myClass.getConstructor();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }enter code here

Я создал конструктор выше, но я не уверен в этом.

Любая идея?

Спасибо про

0 ответов

Другие вопросы по тегам