public Operation exec(Prolog engine) { engine.requireFeature(Prolog.Feature.JAVA_REFLECTION, this, arg1); engine.setB0(); Term a1, a2, a3; a1 = arg1; a2 = arg2; a3 = arg3; Class clazz = null; Object instance = null; Field field = null; Object value = null; try { // 1st. argument (atom or java term) a1 = a1.dereference(); if (a1.isVariable()) { throw new PInstantiationException(this, 1); } else if (a1.isSymbol()) { // class clazz = Class.forName(((SymbolTerm) a1).name()); } else if (a1.isJavaObject()) { // instance instance = ((JavaObjectTerm) a1).object(); clazz = ((JavaObjectTerm) a1).getClazz(); } else { throw new IllegalTypeException(this, 1, "atom_or_java", a1); } // 2nd. argument (atom) a2 = a2.dereference(); if (a2.isVariable()) { throw new PInstantiationException(this, 2); } else if (!a2.isSymbol()) { throw new IllegalTypeException(this, 2, "atom", a2); } field = clazz.getDeclaredField(((SymbolTerm) a2).name()); // 3rd. argument (term) a3 = a3.dereference(); if (a3.isJavaObject()) value = a3.toJava(); else value = a3; field.setAccessible(true); field.set(instance, value); return cont; } catch (ClassNotFoundException e) { // Class.forName throw new JavaException(this, 1, e); } catch (NoSuchFieldException e) { // Class.getField(..) throw new JavaException(this, 2, e); } catch (SecurityException e) { // Class.getField(..) throw new JavaException(this, 2, e); } catch (NullPointerException e) { // Class.getField(..) throw new JavaException(this, 2, e); } catch (IllegalAccessException e) { // Field.get(..) throw new JavaException(this, 2, e); } catch (IllegalArgumentException e) { // Field.get(..) throw new JavaException(this, 2, e); } }
public Predicate exec(Prolog engine) { engine.setB0(); Term a1; a1 = arg1; a1 = a1.dereference(); if (!a1.isJavaObject()) throw new IllegalTypeException(this, 1, "java", a1); Object obj = ((JavaObjectTerm) a1).object(); if (!(obj instanceof PRED_$begin_exception_1)) throw new SystemException( "a1 must be an object of PRED_$begin_exception_1: " + this.toString()); PRED_$begin_exception_1 p = ((PRED_$begin_exception_1) obj); p.outOfScope = true; engine.trail.push(new OutOfScope(p)); return cont; }