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)); }
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)); }
@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 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 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; }