private JMethod createSyntheticMethodFromBinding(SourceInfo info, MethodBinding binding, String[] paramNames) { JMethod method = typeMap.createMethod(info, binding, paramNames); assert !method.isExternal(); method.setBody(new JMethodBody(info)); typeMap.setMethod(binding, method); return method; }
@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); } }
JMethodBody body = new JMethodBody(methodSource); newMethod.setBody(body); JBlock block = body.getBlock(); for (Entry<JClassType,ReflectionStrategy> type : retained.entrySet()) { JClassType cls = type.getKey(); block.addStmt(invoke.makeStatement()); block.addStmts(((JMethodBody)methodCall.getTarget().getBody()).getStatements()); methodCall.getTarget().getEnclosingType().addMethod(newMethod); JMethodCall call = new JMethodCall(methodSource, null, newMethod);
final JMethodBody body = new JMethodBody(info); getSingleton = new JMethod(info, targetName, enclosing, type, false, true, true, AccessModifier.PRIVATE); 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 onUnifyAstStart(TreeLogger logger, UnifyAstView ast, UnifyVisitor visitor, Queue<JMethod> todo) { for (final JMethod method : ast.getProgram().getEntryMethods()) { if (method.getBody() instanceof JMethodBody) { JMethodBody body = (JMethodBody) method.getBody(); // obtain the entry point EntryPointFinder finder = findEntryPoint(logger); body.traverse(finder, finder.getContext()); // find a default strategy if (finder.result == null) { strategy = MagicClassGenerator.class.getAnnotation(ReflectionStrategy.class); } else { com.google.gwt.core.ext.typeinfo.JClassType type = ast.getTypeOracle().findType(finder.result.getName()); strategy = type.getAnnotation(ReflectionStrategy.class); if (strategy == null) { strategy = type.getPackage().getAnnotation(ReflectionStrategy.class); if (strategy == null) { // Nothing on the entry point or it's package; // use a default instance of the ReflectionStrategy annotation strategy = MagicClassGenerator.class.getAnnotation(ReflectionStrategy.class); } } } assert strategy != null; } } }
JMethodBody java = (JMethodBody)method; ArrayList<JReturnStatement> returns = new ArrayList<JReturnStatement>(); for (JStatement statement : java.getStatements()) { if (statement instanceof JReturnStatement) returns.add((JReturnStatement)statement);
final JMethodBody body = new JMethodBody(methodSource); newMethod.setBody(body); final JBlock block = body.getBlock(); for (final Entry<JClassType, ReflectionStrategy> type : retained.entrySet()) { final JClassType cls = type.getKey(); .getStatements()); methodCall.getTarget().getEnclosingType().addMethod(newMethod); final JMethodCall call = new JMethodCall(methodSource, null, newMethod);
.getAccess()); typeMap.setMethod(jdtBridgeMethod, bridgeMethod); bridgeMethod.setBody(new JMethodBody(info)); curClass.type.addMethod(bridgeMethod); bridgeMethod.setSynthetic(); body.getBlock().addStmt(call.makeStatement()); } else { body.getBlock().addStmt(new JReturnStatement(info, call));
@Override public void onUnifyAstStart(final TreeLogger logger, final UnifyAstView ast, final UnifyVisitor visitor, final Queue<JMethod> todo) { for (final JMethod method : ast.getProgram().getEntryMethods()) { if (method.getBody() instanceof JMethodBody) { final JMethodBody body = (JMethodBody) method.getBody(); // obtain the entry point final EntryPointFinder finder = findEntryPoint(logger); body.traverse(finder, finder.getContext()); // find a default strategy if (finder.result == null) { strategy = MagicClassGenerator.class.getAnnotation(ReflectionStrategy.class); } else { final com.google.gwt.core.ext.typeinfo.JClassType type = ast.getTypeOracle().findType(finder.result.getName()); strategy = type.getAnnotation(ReflectionStrategy.class); if (strategy == null) { strategy = type.getPackage().getAnnotation(ReflectionStrategy.class); if (strategy == null) { // Nothing on the entry point or it's package; // use a default instance of the ReflectionStrategy annotation strategy = MagicClassGenerator.class.getAnnotation(ReflectionStrategy.class); } } } assert strategy != null; } } }
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 JMethod createSyntheticMethod(SourceInfo info, String name, JDeclaredType enclosingType, JType returnType, boolean isAbstract, boolean isStatic, boolean isFinal, AccessModifier access) { JMethod method = new JMethod(info, name, enclosingType, returnType, isAbstract, isStatic, isFinal, access); method.freezeParamTypes(); method.setSynthetic(); method.setBody(new JMethodBody(info)); enclosingType.addMethod(method); return method; }
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)); }
JConstructor method = (JConstructor) typeMap.get(x.binding); assert !method.isExternal(); JMethodBody body = new JMethodBody(method.getSourceInfo()); method.setBody(body); pushMethodInfo(new MethodInfo(method, body, x.scope));
@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(MethodDeclaration x, ClassScope scope) { try { JMethod method = typeMap.get(x.binding); assert !method.isExternal(); JMethodBody body = null; if (!method.isNative()) { body = new JMethodBody(method.getSourceInfo()); method.setBody(body); } pushMethodInfo(new MethodInfo(method, body, x.scope)); // Map user arguments. Iterator<JParameter> it = method.getParams().iterator(); if (x.arguments != null) { for (Argument argument : x.arguments) { curMethod.locals.put(argument.binding, it.next()); } } x.statements = reduceToReachable(x.statements); return true; } catch (Throwable e) { throw translateException(x, e); } }
@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); } }
clinit.setBody(new JMethodBody(SourceOrigin.UNKNOWN));
@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; }
@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); } }