SourceInfo info = implmeth.getSourceInfo(); JMethod bridgeMethod = new JMethod(info, implmeth.getName(), curClass.type, typeMap .get(jdtBridgeMethod.returnType), false, false, implmeth.isFinal(), implmeth .getAccess()); typeMap.setMethod(jdtBridgeMethod, bridgeMethod); bridgeMethod.setBody(new JMethodBody(info)); curClass.type.addMethod(bridgeMethod); bridgeMethod.setSynthetic(); int paramIdx = 0; List<JParameter> implParams = implmeth.getParams(); for (TypeBinding jdtParamType : jdtBridgeMethod.parameters) { JParameter param = implParams.get(paramIdx++); new JParameter(param.getSourceInfo(), param.getName(), paramType, true, false, bridgeMethod); bridgeMethod.addParam(newParam); bridgeMethod.addThrownException((JClassType) typeMap.get(exceptionReference.erasure())); bridgeMethod.freezeParamTypes(); for (int i = 0; i < bridgeMethod.getParams().size(); i++) { JParameter param = bridgeMethod.getParams().get(i); JParameterRef paramRef = new JParameterRef(info, param); call.addArg(maybeCast(implParams.get(i).getType(), paramRef)); JMethodBody body = (JMethodBody) bridgeMethod.getBody(); if (bridgeMethod.getType() == JPrimitiveType.VOID) {
private void flowInto(JMethod method) { if (method.isExternal()) { assert errorsFound; return; JType originalReturnType = translate(method.getOriginalReturnType()); List<JType> originalParamTypes = new ArrayList<JType>(method.getOriginalParamTypes().size()); for (JType originalParamType : method.getOriginalParamTypes()) { originalParamTypes.add(translate(originalParamType)); JType returnType = translate(method.getType()); List<JClassType> thrownExceptions = new ArrayList<JClassType>(method.getThrownExceptions().size()); for (JClassType thrownException : method.getThrownExceptions()) { thrownExceptions.add(translate(thrownException)); method.resolve(originalReturnType, originalParamTypes, returnType, thrownExceptions); if (method.isStatic()) { staticInitialize(method.getEnclosingType()); } else if (method.canBePolymorphic()) { String signature = method.getSignature(); if (!virtualMethodsLive.contains(signature)) { virtualMethodsLive.add(signature); if (pending != null) { for (JMethod p : pending) { assert instantiatedTypes.contains(p.getEnclosingType()); flowInto(p);
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 static boolean isGetMagicClass(JMethod target) { return (target.getName().equals("magicClass") && target.getEnclosingType().getName().equals(GwtReflect.class.getName())) || (target.getName().equals("enhanceClass") && target.getEnclosingType().getName().endsWith(ReflectionUtilJava.MAGIC_CLASS_SUFFIX)) ; }
/** * @param methodProvider * @return */ public boolean isThrowStatement(final JExpression methodProvider) { if (methodProvider instanceof JMethodCall) { final JMethod method = ((JMethodCall)methodProvider).getTarget(); return method.getType().getName().equals(GwtReflect.class.getName()) && method.getName().equals("doThrow"); } return false; }
public JMethodCall getFieldProvider(final TreeLogger logger, final ReflectionGeneratorContext ctx, final com.google.gwt.core.ext.typeinfo.JField field, final JClassLiteral classLit, final boolean declaredOnly) throws UnableToCompleteException { final String clsName = classLit.getRefType().getName(); final ReflectionManifest manifest = ReflectionManifest.getReflectionManifest(logger, clsName, ctx.getGeneratorContext()); final String factoryCls = getOrMakeFieldFactory(logger, ctx, field, field.getEnclosingType(), manifest, declaredOnly); ctx.finish(logger); final UnifyAstView ast = ctx.getAst(); final JDeclaredType factory = ast.searchForTypeBySource(factoryCls); // pull out the static accessor method for (final JMethod factoryMethod : factory.getMethods()) { if (factoryMethod.isStatic() && factoryMethod.getName().equals("instantiate")) { return new JMethodCall(factoryMethod.getSourceInfo(), null, factoryMethod); } } logger.log(Type.ERROR, "Unable to find static initializer for Field subclass "+factoryCls); throw new UnableToCompleteException(); }
JMethod magicClass = null; for (JMethod method : gwtReflect.getMethods()) { if (method.getName().equals("magicClass")) { magicClass = method; break; JMethod newMethod = new JMethod(methodSource, "enhanceAll", methodCall.getTarget().getEnclosingType(), ast.getProgram().getTypeVoid(), false, true, true, AccessModifier.PUBLIC); newMethod.setOriginalTypes(ast.getProgram().getTypeVoid(), methodCall.getTarget().getOriginalParamTypes()); JMethodBody body = new JMethodBody(methodSource); newMethod.setBody(body); JBlock block = body.getBlock(); for (Entry<JClassType,ReflectionStrategy> type : retained.entrySet()) { block.addStmt(invoke.makeStatement()); block.addStmts(((JMethodBody)methodCall.getTarget().getBody()).getStatements()); methodCall.getTarget().getEnclosingType().addMethod(newMethod); JMethodCall call = new JMethodCall(methodSource, null, newMethod);
final JDeclaredType lazyProvider = ast.searchForTypeBySource(SingletonProvider.class.getName()); for (final JMethod method : lazyProvider.getMethods()) { if (method.getName().equals("get")) { final String targetName = "singleton" + type.getShortName().replaceAll("[$]", "_"); for (final JMethod existingMethod : enclosing.getMethods()) { if (existingMethod.getName().equals(existingMethod)) { getSingleton = existingMethod; info = getSingleton.getSourceInfo(); logger.log(logLevel(), "Reusing generated method " + getSingleton.toSource()); break; getSingleton = new JMethod(info, targetName, enclosing, type, false, true, true, AccessModifier.PRIVATE); getSingleton.setBody(body); getSingleton.freezeParamTypes(); getSingleton.setSynthetic(); info.addCorrelation(info.getCorrelator().by(getSingleton)); if (logger.isLoggable(Type.DEBUG)) { logger = logger.branch(Type.DEBUG, "Generated magic singleton: "); for (final String str : getSingleton.toSource().split("\n")) { logger.branch(Type.DEBUG, str);
/** * TODO(scottb): move to UnifyAst and only for non-abstract classes. */ private void implementGetClass(JDeclaredType type) { JMethod method = type.getMethods().get(2); assert ("getClass".equals(method.getName())); SourceInfo info = method.getSourceInfo(); if ("com.google.gwt.lang.Array".equals(type.getName())) { /* * Don't implement, fall through to Object.getClass(). Array emulation code * in com.google.gwt.lang.Array invokes Array.getClass() and expects to get the * class literal for the actual runtime type of the array (e.g. Foo[].class) and * not Array.class. */ type.getMethods().remove(2); } else { implementMethod(method, new JClassLiteral(info, type)); } }
public JMethod getThrowMethod(final TreeLogger logger, final UnifyAstView ast) { if (doThrow == null) { final JDeclaredType gwtReflect = ast.searchForTypeBySource(GwtReflect.class.getName()); for (final JMethod method : gwtReflect.getMethods()) { if (method.getName().equals("doThrow")) { doThrow = method; break; } } } return doThrow; } }
@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); } }
public com.google.gwt.dev.jjs.ast.JMethod findGetterFor(UnifyAstView ast, String memberGetter) throws UnableToCompleteException { com.google.gwt.dev.jjs.ast.JMethod getter = getters.get(memberGetter); if (getter == null) { initMethod(ast); JDeclaredType type = ast.translate((JDeclaredType) initMethod.getOriginalReturnType()); for (com.google.gwt.dev.jjs.ast.JMethod method : type.getMethods()) { if (method.getName().endsWith(memberGetter)) { getters.put(memberGetter, method); return method; } } ast.error(type, "Type "+type.getName()+" does not have member getter method "+memberGetter); throw new UnableToCompleteException(); } return getter; }
@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 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 JMethod translate(JMethod method) { if (!method.isExternal()) { return method; } JDeclaredType enclosingType = method.getEnclosingType(); String sig = enclosingType.getName() + '.' + method.getSignature(); JMethod newMethod = methodMap.get(sig); if (newMethod != null) { return newMethod; } enclosingType = translate(enclosingType); if (enclosingType.isExternal()) { assert errorsFound; return method; } mapApi(enclosingType); // Now the method should be there. method = methodMap.get(sig); if (method == null) { // TODO: error logging throw new NoSuchMethodError(sig); } assert !method.isExternal(); return method; } @Override
createSyntheticMethod(info, "$clinit", mapClass, JPrimitiveType.VOID, false, true, true, AccessModifier.PRIVATE); JBlock clinitBlock = ((JMethodBody) clinit.getBody()).getBlock(); clinitBlock.addStmt(declStmt); SourceInfo info = method.getSourceInfo(); JParameterRef nameRef = new JParameterRef(info, method.getParams().get(0)); JMethodCall call = new JMethodCall(info, null, typeMap.get(valueOfBinding)); call.addArgs(mapRef, nameRef);
private void processNativeMethod(MethodDeclaration x) { JMethod method = curMethod.method; JsniMethod jsniMethod = jsniMethods.get(x); assert jsniMethod != null; SourceInfo info = method.getSourceInfo(); JsFunction jsFunction = jsniMethod.function(); JsniMethodBody body = new JsniMethodBody(info); method.setBody(body); jsFunction.setFromJava(true); body.setFunc(jsFunction); // Resolve locals, params, and JSNI. JsParameterResolver localResolver = new JsParameterResolver(jsFunction); localResolver.accept(jsFunction); JsniResolver jsniResolver = new JsniResolver(body); jsniResolver.accept(jsFunction); }
private void writeEnumValuesMethod(JEnumType type, JMethod method, JField valuesField) { // return $VALUES; JFieldRef valuesRef = new JFieldRef(method.getSourceInfo(), null, valuesField, type); implementMethod(method, valuesRef); } }
@Override public void endVisit(JClassLiteral x, Context ctx) { JType refType = translate(x.getRefType()); x.resolve(refType); // ImplementClassLiteralsAsFields: rescue enumType.values()/valueOf(). if (refType instanceof JArrayType) { JType leafType = ((JArrayType) refType).getLeafType(); if (leafType instanceof JReferenceType) { refType = leafType; } } if (refType instanceof JClassType) { JClassType classType = (JClassType) refType; JEnumType enumType = classType.isEnumOrSubclass(); if (enumType != null) { for (JMethod method : enumType.getMethods()) { if (method.isStatic()) { if (method.getSignature().startsWith("values()")) { flowInto(method); } else if (method.getSignature().startsWith("valueOf(Ljava/lang/String;)")) { flowInto(method); } } } } } }