}
if (a1.isSymbol()) { // No argument constructor
clazz = Class.forName(((SymbolTerm) a1).name());
c = clazz.getDeclaredConstructor();
if (c == null) {
throw new ExistenceException(this, 1, "constructor", a1, "");
}
c.setAccessible(true);
instance = c.newInstance();
if (!a2.unify(toPrologTerm(instance), engine.trail)) {
return engine.fail();
}
return cont;
}
// Parameterized constructor
clazz = Class.forName(((StructureTerm) a1).name());
arity = ((StructureTerm) a1).arity();
constrs = clazz.getDeclaredConstructors();
if (constrs.length == 0) {
throw new ExistenceException(this, 1, "constructor", a1, "");
}
pArgs = ((StructureTerm) a1).args();
jArgs = new Object[arity];
for (int i = 0; i < arity; i++) {
pArgs[i] = pArgs[i].dereference();
if (!pArgs[i].isJavaObject()) {
pArgs[i] = new JavaObjectTerm(pArgs[i]);
}
jArgs[i] = pArgs[i].toJava();
}
for (Constructor constr : constrs) {
if (checkParameterTypes(constr.getParameterTypes(), pArgs)) {
try {
c = constr;
c.setAccessible(true);
instance = c.newInstance(jArgs);
break; // Succeeds to create new instance
} catch (Exception e) {
c = null; // Back to loop
}
}
}
if (c == null) {
throw new ExistenceException(this, 1, "constructor", a1, "");
}
if (!a2.unify(toPrologTerm(instance), engine.trail)) {
return engine.fail();
}
return cont;