/** * Add a {@link Variable} to the current scope. * * @param variable * the variable instance to add, must not be null. * * @return the current context with the variable added. */ public Context addVariable(final Variable variable) { checkThread(); if (variables == null) variables = new HashMap<String, Variable>(); variables.put(variable.getName(), variable); return this; }
/** * Add a {@link Variable} to the current scope. * * @param variable * the variable instance to add, must not be null. * * @return the current context with the variable added. */ public Context addVariable(final Variable variable) { checkThread(); if (variables == null) variables = new HashMap<String, Variable>(); variables.put(variable.getName(), variable); return this; }
public StatementBuilder(final Context context) { super(context); if (context != null) { context.getDeclaredVariables().stream() .filter(v -> !THIS_OR_SUPPER_PATTERN.matcher(v.getName()).matches()) .forEach(v -> appendCallElement(new DeclareVariable(v))); } appendCallElement(new ResetCallElement()); }
public StatementBuilder(final Context context) { super(context); if (context != null) { context.getDeclaredVariables().stream() .filter(v -> !THIS_OR_SUPPER_PATTERN.matcher(v.getName()).matches()) .forEach(v -> appendCallElement(new DeclareVariable(v))); } appendCallElement(new ResetCallElement()); }
@Override public String generate(Context context) { if (generatedCache != null) return generatedCache; return generatedCache = ((nullSafe) ? "if (" + collectionExpr + " != null) {\n" : "") + "for (" + getClassReference(loopVar.getType(), context) + " " + loopVar.getName() + " : " + collectionExpr + ") {" + "\n\t" + body.generate(context).replaceAll("\n", "\n\t") + "\n}" + ((nullSafe) ? "\n}" : ""); } }
@Override public String generate(Context context) { if (generatedCache != null) return generatedCache; return generatedCache = ((nullSafe) ? "if (" + collectionExpr + " != null) {\n" : "") + "for (" + getClassReference(loopVar.getType(), context) + " " + loopVar.getName() + " : " + collectionExpr + ") {" + "\n\t" + body.generate(context).replaceAll("\n", "\n\t") + "\n}" + ((nullSafe) ? "\n}" : ""); } }
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); } }