/** * Returns the a reference to the {@link Label} with the given name. * * @param name * the name of the label. * * @return the {@link LabelReference} found, can not be null. * * @throws OutOfScopeException * if label with the given name can not be found. */ public LabelReference getLabel(final String name) { checkThread(); Label found = null; Context ctx = this; do { if (ctx.labels != null) { found = ctx.labels.get(name); } } while (found == null && (ctx = ctx.parent) != null); if (found == null) { throw new OutOfScopeException("Label not found: " + name); } return found.getReference(); }
@Test public void testInvokeOnUndefinedVariable() { try { // injector undefined StatementBuilder.create() .loadVariable("injector") .invoke("provide", Refs.get("param"), Refs.get("param2")) .toJavaString(); fail("expected OutOfScopeException"); } catch (OutOfScopeException oose) { // expected assertTrue("Wrong exception thrown", oose.getMessage().contains("injector")); } }
@Test public void testInvokeWithUndefinedVariable() { try { // param2 undefined StatementBuilder.create() .declareVariable("obj", Object.class) .declareVariable("param", String.class) .loadVariable("obj") .invoke("undefinedMethod", Variable.get("param"), Variable.get("param2")) .toJavaString(); fail("expected OutOfScopeException"); } catch (OutOfScopeException oose) { // expected assertTrue(oose.getMessage().contains("param2")); } }
/** * Returns the a reference to the {@link Label} with the given name. * * @param name * the name of the label. * * @return the {@link LabelReference} found, can not be null. * * @throws OutOfScopeException * if label with the given name can not be found. */ public LabelReference getLabel(final String name) { checkThread(); Label found = null; Context ctx = this; do { if (ctx.labels != null) { found = ctx.labels.get(name); } } while (found == null && (ctx = ctx.parent) != null); if (found == null) { throw new OutOfScopeException("Label not found: " + name); } return found.getReference(); }
private VariableReference getVariable(final String name, final boolean mustBeClassMember) { checkThread(); Variable found = null; Context ctx = this; do { if (ctx.variables != null) { final Variable var = ctx.variables.get(name); found = (mustBeClassMember && var != null && !variables.containsKey(var.getName())) ? null : var; } } while (found == null && (ctx = ctx.parent) != null); if (found == null) { missingSymbols.add(name); if (GenUtil.isPermissiveMode()) { return Variable.create(name, Object.class).getReference(); } else { throw new OutOfScopeException((mustBeClassMember) ? "this." + name : name + " not found.\nScope:\n" + this); } } return found.getReference(); }
private VariableReference getVariable(final String name, final boolean mustBeClassMember) { checkThread(); Variable found = null; Context ctx = this; do { if (ctx.variables != null) { final Variable var = ctx.variables.get(name); found = (mustBeClassMember && var != null && !variables.containsKey(var.getName())) ? null : var; } } while (found == null && (ctx = ctx.parent) != null); if (found == null) { missingSymbols.add(name); if (GenUtil.isPermissiveMode()) { return Variable.create(name, Object.class).getReference(); } else { throw new OutOfScopeException((mustBeClassMember) ? "this." + name : name + " not found.\nScope:\n" + this); } } return found.getReference(); }
public static Statement generate(final Context context, final Object o) { if (o instanceof VariableReference) { return context.getVariable(((VariableReference) o).getName()); } else if (o instanceof Variable) { final Variable v = (Variable) o; if (context.isScoped(v)) { return v.getReference(); } else { if (context.isPermissiveMode()) { return v.getReference(); } else { throw new OutOfScopeException("variable cannot be referenced from this scope: " + v.getName()); } } } else if (o instanceof Statement) { ((Statement) o).generate(context); return (Statement) o; } else { return LiteralFactory.getLiteral(context, o); } }
public static Statement generate(final Context context, final Object o) { if (o instanceof VariableReference) { return context.getVariable(((VariableReference) o).getName()); } else if (o instanceof Variable) { final Variable v = (Variable) o; if (context.isScoped(v)) { return v.getReference(); } else { if (context.isPermissiveMode()) { return v.getReference(); } else { throw new OutOfScopeException("variable cannot be referenced from this scope: " + v.getName()); } } } else if (o instanceof Statement) { ((Statement) o).generate(context); return (Statement) o; } else { return LiteralFactory.getLiteral(context, o); } }