private void implementMagicMethod(JMethod method, JExpression returnValue) { JMethodBody body = (JMethodBody) method.getBody(); JBlock block = body.getBlock(); SourceInfo info; if (block.getStatements().size() > 0) { info = block.getStatements().get(0).getSourceInfo(); } else { info = method.getSourceInfo(); } block.clear(); block.addStmt(new JReturnStatement(info, returnValue)); }
@Override public void endVisit(Initializer x, MethodScope scope) { try { JBlock block = pop(x.block); if (block != null) { curMethod.body.getBlock().addStmt(block); } popMethodInfo(); } catch (Throwable e) { throw translateException(x, e); } }
for (SyntheticArgumentBinding arg : nestedBinding.enclosingInstances) { JBinaryOperation asg = assignSyntheticField(info, arg); block.addStmt(asg.makeStatement()); for (SyntheticArgumentBinding arg : nestedBinding.outerLocalVariables) { JBinaryOperation asg = assignSyntheticField(info, arg); block.addStmt(asg.makeStatement()); block.addStmt(constructorCall); JMethod initMethod = curClass.type.getInitMethod(); JMethodCall initCall = new JMethodCall(info, makeThisRef(info), initMethod); block.addStmt(initCall.makeStatement()); block.addStmts(statements); popMethodInfo(); } catch (Throwable e) {
private JField createEnumValuesField(JEnumType type) { // $VALUES = new E[]{A,B,B}; JArrayType enumArrayType = new JArrayType(type); JField valuesField = new JField(type.getSourceInfo(), "$VALUES", type, enumArrayType, true, Disposition.FINAL); type.addField(valuesField); SourceInfo info = type.getSourceInfo(); List<JExpression> initializers = new ArrayList<JExpression>(); for (JEnumField field : type.getEnumList()) { JFieldRef fieldRef = new JFieldRef(info, null, field, type); initializers.add(fieldRef); } JNewArray newExpr = JNewArray.createInitializers(info, enumArrayType, initializers); JFieldRef valuesRef = new JFieldRef(info, null, valuesField, type); JDeclarationStatement declStmt = new JDeclarationStatement(info, valuesRef, newExpr); JBlock clinitBlock = ((JMethodBody) type.getClinitMethod().getBody()).getBlock(); /* * HACKY: the $VALUES array must be initialized immediately after all of * the enum fields, but before any user initialization (which might rely * on $VALUES). The "1 + " is the statement containing the call to * Enum.$clinit(). */ int insertionPoint = 1 + type.getEnumList().size(); assert clinitBlock.getStatements().size() >= initializers.size() + 1; clinitBlock.addStmt(insertionPoint, declStmt); return valuesField; }
@Override public boolean visit(JConstructor x, Context ctx) { if (pastRebound) { for (JMethod m : x.getEnclosingType().getMethods()) { if (m.getName().equals("createNewTestCase")) { for (JStatement statement : ((JMethodBody)m.getBody()).getBlock().getStatements()) { if (statement instanceof JIfStatement) { pastConstructor = true; ((JIfStatement)statement).getThenStmt().traverse(this, ctx); return false; } } } } } return super.visit(x, ctx); }
@Override public void endVisit(MethodDeclaration x, ClassScope scope) { try { if (x.isNative()) { processNativeMethod(x); } else { List<JStatement> statements = pop(x.statements); curMethod.body.getBlock().addStmts(statements); } popMethodInfo(); } catch (Throwable e) { throw translateException(x, e); } }
@Override public void endVisit(SynchronizedStatement x, BlockScope scope) { try { JBlock block = pop(x.block); JExpression expression = pop(x.expression); block.addStmt(0, expression.makeStatement()); push(block); } catch (Throwable e) { throw translateException(x, e); } }
JExpression invoke = MagicClassInjector.injectMagicClass(logger, call, magicClass, context, ast); if (invoke != null) block.addStmt(invoke.makeStatement()); block.addStmts(((JMethodBody)methodCall.getTarget().getBody()).getStatements()); methodCall.getTarget().getEnclosingType().addMethod(newMethod); JMethodCall call = new JMethodCall(methodSource, null, newMethod);
@Override public boolean visit(JConstructor x, Context ctx) { if (pastRebound) { for (JMethod m : x.getEnclosingType().getMethods()) { if (m.getName().equals("createNewTestCase")) { for (JStatement statement : ((JMethodBody)m.getBody()).getBlock().getStatements()) { if (statement instanceof JIfStatement) { pastConstructor = true; ((JIfStatement)statement).getThenStmt().traverse(this, ctx); return false; } } } } } return super.visit(x, ctx); }
private void implementMethod(JMethod method, JExpression returnValue) { JMethodBody body = (JMethodBody) method.getBody(); JBlock block = body.getBlock(); SourceInfo info; if (block.getStatements().size() > 0) { info = block.getStatements().get(0).getSourceInfo(); } else { info = method.getSourceInfo(); } block.clear(); block.addStmt(new JReturnStatement(info, returnValue)); }
assert (JProgram.isClinit(clinitMethod)); final JMethodCall doClinit = new JMethodCall(clinit.getSourceInfo(), null, clinitMethod); body.getBlock().addStmt(doClinit.makeStatement()); body.getBlock().addStmt(value); if (logger.isLoggable(Type.DEBUG)) { logger = logger.branch(Type.DEBUG, "Generated magic singleton: ");
@Override public void endVisit(FieldDeclaration x, MethodScope scope) { try { JExpression initialization = pop(x.initialization); JField field = typeMap.get(x.binding); if (field instanceof JEnumField) { // An enum field must be initialized! assert (initialization instanceof JNewInstance); } if (initialization != null) { SourceInfo info = makeSourceInfo(x); JExpression instance = null; if (!x.isStatic()) { instance = makeThisRef(info); } // JDeclarationStatement's ctor sets up the field's initializer. JStatement decl = new JDeclarationStatement(info, new JFieldRef(info, instance, field, curClass.type), initialization); // will either be init or clinit curMethod.body.getBlock().addStmt(decl); } popMethodInfo(); } catch (Throwable e) { throw translateException(x, e); } }
JMethodCall superClinitCall = new JMethodCall(myClinit.getSourceInfo(), null, superClinit); JMethodBody body = (JMethodBody) myClinit.getBody(); body.getBlock().addStmt(0, superClinitCall.makeStatement());
true, AccessModifier.PRIVATE); JBlock clinitBlock = ((JMethodBody) clinit.getBody()).getBlock(); clinitBlock.addStmt(declStmt);