protected JMethodCall checkConstPool(UnifyAstView ast, JMethodCall callSite, JExpression classRef, JExpression ... args) throws UnableToCompleteException { com.google.gwt.dev.jjs.ast.JMethod initPool = getMemberPoolInit(ast); JMethodCall getMemberPool = new JMethodCall(initPool.getSourceInfo(), null, initPool); getMemberPool.addArg(classRef); ManifestMap map = manifests.get(); com.google.gwt.dev.jjs.ast.JMethod getter = map.findGetterFor(ast, memberGetter()); JMethodCall checkPool = new JMethodCall(initPool.getSourceInfo(), getMemberPool, getter); for (JExpression arg : args) { checkPool.addArg(arg); } return checkPool; }
protected JMethodCall checkConstPool(final UnifyAstView ast, final JMethodCall callSite, final JExpression classRef, final JExpression... args) throws UnableToCompleteException { final JMethod initPool = getMemberPoolInit(ast); final JMethodCall getMemberPool = new JMethodCall(initPool.getSourceInfo(), null, initPool); getMemberPool.addArg(classRef); final MemberPoolMethods map = memberPoolMethods.get(); final JMethod getter = map.findGetterFor(ast, memberGetter()); final JMethodCall checkPool = new JMethodCall(initPool.getSourceInfo(), getMemberPool, getter); for (final JExpression arg : args) { checkPool.addArg(arg); } return checkPool; }
@Override public JExpression injectMagic(TreeLogger logger, JMethodCall methodCall, JMethod enclosingMethod, Context context, UnifyAstView ast) throws UnableToCompleteException { JExpression ctorProvider = super.injectMagic(logger, methodCall, enclosingMethod, context, ast); // call the newInstance method on the result of our ctorProvider JDeclaredType ctor = ast.searchForTypeBySource(Constructor.class.getName()); for (JMethod method : ctor.getMethods()) { if (method.getName().equals("newInstance")) { JMethodCall call = new JMethodCall(method.getSourceInfo(), ctorProvider, method); call.addArg(methodCall.getArgs().get(2)); return call; } } logger.log(Type.ERROR, "Unable to implement GwtReflect.construct from "+methodCall.toSource()); throw new UnableToCompleteException(); }
@Override public JExpression injectMagic(final TreeLogger logger, final JMethodCall methodCall, final JMethod enclosingMethod, final Context context, final UnifyAstView ast) throws UnableToCompleteException { final JExpression ctorProvider = super.injectMagic(logger, methodCall, enclosingMethod, context, ast); // Find the invoker method // call the newInstance method on the result of our ctorProvider final JDeclaredType ctor = ast.searchForTypeBySource(Constructor.class.getName()); for (final JMethod method : ctor.getMethods()) { if (method.getName().equals("newInstance")) { final JMethodCall call = new JMethodCall(methodCall.getSourceInfo().makeChild(), ctorProvider, method); call.addArg(methodCall.getArgs().get(2)); return call; } } logger.log(Type.ERROR, "Unable to implement GwtReflect.construct from "+methodCall.toSource()); throw new UnableToCompleteException(); }
/** * @param ast * @param logger * @param callSite * @return * @throws UnableToCompleteException */ public JExpression throwNotFoundException(final TreeLogger logger, final JMethodCall callSite, final UnifyAstView ast) throws UnableToCompleteException { final SourceInfo sourceInfo = callSite.getSourceInfo().makeChild(); final MemberPoolMethods memberMap = memberPoolMethods.get(); final IsQualified exceptionType = getNotFoundExceptionType(); final JConstructor ctor = memberMap.findConstructor(logger, exceptionType.getQualifiedName(), exceptionType.getSimpleName()+"() <init>", ast); final JNewInstance newThrowable = new JNewInstance(sourceInfo, ctor); final JMethod throwMethod = memberMap.getThrowMethod(logger, ast); return new JMethodCall(sourceInfo, null, throwMethod, newThrowable); }
@Override public JExpression injectMagic(TreeLogger logger, JMethodCall methodCall, JMethod enclosingMethod, Context context, UnifyAstView ast) throws UnableToCompleteException { JExpression methodProvider = super.injectMagic(logger, methodCall, enclosingMethod, context, ast); if (methodCall.getArgs().size() != 5) { logger.log(Type.ERROR, "Method call provided to replace GwtReflect.invoke(" + "Class<?> cls, String name, Class<?>[] paramTypes, Object inst, Object ... params" + ") supplied incorrect number of arguments; you supplied "+methodCall.getArgs().size() +" arguments: "+methodCall.getArgs()); throw new UnableToCompleteException(); } // call the invoke method on the result of our methodProvider JDeclaredType ctor = ast.searchForTypeBySource(Method.class.getName()); for (JMethod method : ctor.getMethods()) { if (method.getName().equals("invoke")) { JMethodCall call = new JMethodCall(method.getSourceInfo(), methodProvider, method); call.addArg(methodCall.getArgs().get(3)); call.addArg(methodCall.getArgs().get(4)); return call; } } logger.log(Type.ERROR, "Unable to implement GwtReflect.invoke from "+methodCall.toSource()); throw new UnableToCompleteException(); }
private JExpression unbox(JExpression original, int implicitConversion) { int typeId = implicitConversion & TypeIds.COMPILE_TYPE_MASK; ClassScope scope = curClass.scope; BaseTypeBinding primitiveType = (BaseTypeBinding) TypeBinding.wellKnownType(scope, typeId); ReferenceBinding boxType = (ReferenceBinding) scope.boxing(primitiveType); char[] selector = CharOperation.concat(primitiveType.simpleName, VALUE); MethodBinding valueMethod = boxType.getExactMethod(selector, NO_TYPES, scope.compilationUnitScope()); assert valueMethod != null; JMethod unboxMethod = typeMap.get(valueMethod); JMethodCall call = new JMethodCall(original.getSourceInfo(), original, unboxMethod); return call; }
@Override public JExpression injectMagic(final TreeLogger logger, final JMethodCall methodCall, final JMethod enclosingMethod, final Context context, final UnifyAstView ast) throws UnableToCompleteException { final JExpression methodProvider = super.injectMagic(logger, methodCall, enclosingMethod, context, ast); if (isThrowStatement(methodProvider)) { return methodProvider; } if (methodCall.getArgs().size() != 5) { logger.log(Type.ERROR, "Method call provided to replace GwtReflect.invoke(" + "Class<?> cls, String name, Class<?>[] paramTypes, Object inst, Object ... params" + ") supplied incorrect number of arguments; you supplied "+methodCall.getArgs().size() +" arguments: "+methodCall.getArgs()); throw new UnableToCompleteException(); } // call the invoke method on the result of our methodProvider final JDeclaredType ctor = ast.searchForTypeBySource(Method.class.getName()); for (final JMethod method : ctor.getMethods()) { if (method.getName().equals("invoke")) { final JMethodCall call = new JMethodCall(method.getSourceInfo(), methodProvider, method); call.addArg(methodCall.getArgs().get(3)); call.addArg(methodCall.getArgs().get(4)); return call; } } logger.log(Type.ERROR, "Unable to implement GwtReflect.invoke from "+methodCall.toSource()); throw new UnableToCompleteException(); }
@Override public void endVisit(SwitchStatement x, BlockScope scope) { try { SourceInfo info = makeSourceInfo(x); JBlock block = popBlock(info, x.statements); JExpression expression = pop(x.expression); if (x.expression.resolvedType.isEnum()) { // synthesize a call to ordinal(). ReferenceBinding javaLangEnum = scope.getJavaLangEnum(); MethodBinding ordinal = javaLangEnum.getMethods(ORDINAL)[0]; expression = new JMethodCall(info, expression, typeMap.get(ordinal)); } push(new JSwitchStatement(info, expression, block)); } catch (Throwable e) { throw translateException(x, e); } }
/** * Implements a caching layer guarding the {@link #initialize(String, ReflectionGeneratorContext)} method * * @param key * @param params * @return */ protected JExpression get(String key, ReflectionGeneratorContext params) { //because we cache results, super dev mode recompiles need to skip the //cache if the magic class does not exist, thus we test type presence on every get(). JDeclaredType type = params.getAst().searchForTypeByBinary(params.getClazz().getRefType().getName()); String typeName = JGwtCreate.nameOf(type); String generatedName = ReflectionUtilJava.generatedMagicClassName(typeName); try { params.getAst().searchForTypeBySource(generatedName); }catch(NoClassDefFoundError e) { classEnhancers.remove(key); } if (classEnhancers.containsKey(key)) { JMethodCall previous = classEnhancers.get(key); previous = new JMethodCall(previous, previous.getInstance()); previous.addArg(params.getClazz().makeStatement().getExpr()); return previous.makeStatement().getExpr(); } JMethodCall expr = initialize(key, params); classEnhancers.put(key, expr); expr.setArg(0, params.getClazz().makeStatement().getExpr()); return expr.makeStatement().getExpr(); }
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(); }
/** * Implements a caching layer guarding the {@link #initialize(String, ReflectionGeneratorContext)} method * * @param key * @param params * @return * @throws UnableToCompleteException */ protected JExpression get(final String key, final ReflectionGeneratorContext params) throws UnableToCompleteException { //because we cache results, super dev mode recompiles need to skip the //cache if the magic class does not exist, thus we test type presence on every get(). final JDeclaredType type = params.getAst().searchForTypeByBinary(params.getClazz().getRefType().getName()); final String typeName = BinaryName.toSourceName(type.getName()); final String generatedName = ReflectionUtilJava.generatedMagicClassName(typeName); if (null == params.getAst().searchForTypeBySource(generatedName)) { classEnhancers.remove(key); } if (classEnhancers.containsKey(key)) { JMethodCall previous = classEnhancers.get(key); previous = new JMethodCall(previous, previous.getInstance()); previous.addArg(params.getClazz().makeStatement().getExpr()); return previous.makeStatement().getExpr(); } final JMethodCall expr = initialize(key, params); classEnhancers.put(key, expr); expr.setArg(0, params.getClazz().makeStatement().getExpr()); return expr.makeStatement().getExpr(); }
public JMethodCall getConstructorProvider(final TreeLogger logger, final ReflectionGeneratorContext ctx, final JConstructor ctor, final boolean declared) throws UnableToCompleteException { final JClassLiteral classLit = ctx.getClazz(); final UnifyAstView ast = ctx.getAst(); final String clsName = classLit.getRefType().getName(); final ReflectionManifest manifest = ReflectionManifest.getReflectionManifest(logger, clsName, ast.getGeneratorContext()); final String factoryCls = getOrMakeConstructorFactory(logger, ctx, ctor, ctor.getEnclosingType(), manifest, declared); ctx.finish(logger); 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 Constructor subclass "+factoryCls); throw new UnableToCompleteException(); }
public JMethodCall getFieldProvider(TreeLogger logger, UnifyAstView ast, com.google.gwt.core.ext.typeinfo.JField field, JClassLiteral classLit, boolean declaredOnly) throws UnableToCompleteException { String clsName = classLit.getRefType().getName(); ReflectionManifest manifest = ReflectionManifest.getReflectionManifest(logger, clsName, ast.getGeneratorContext()); String factoryCls = getOrMakeFieldFactory(logger, ast.getRebindPermutationOracle().getGeneratorContext(), field, field.getEnclosingType(), manifest, declaredOnly); ast.getGeneratorContext().finish(logger); JDeclaredType factory = ast.searchForTypeBySource(factoryCls); // pull out the static accessor method for (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(); }
private JExpression box(JExpression original, int implicitConversion) { int typeId = (implicitConversion & TypeIds.IMPLICIT_CONVERSION_MASK) >> 4; ClassScope scope = curClass.scope; BaseTypeBinding primitiveType = (BaseTypeBinding) TypeBinding.wellKnownType(scope, typeId); ReferenceBinding boxType = (ReferenceBinding) scope.boxing(primitiveType); MethodBinding valueOfMethod = boxType.getExactMethod(VALUE_OF, new TypeBinding[]{primitiveType}, scope .compilationUnitScope()); assert valueOfMethod != null; // Add a cast to the correct primitive type if needed. JType targetPrimitiveType = typeMap.get(primitiveType); if (original.getType() != targetPrimitiveType) { original = new JCastOperation(original.getSourceInfo(), targetPrimitiveType, original); } JMethod boxMethod = typeMap.get(valueOfMethod); JMethodCall call = new JMethodCall(original.getSourceInfo(), null, boxMethod); call.addArg(original); return call; }
public JMethodCall getMethodProvider(final TreeLogger logger, final ReflectionGeneratorContext ctx, final com.google.gwt.core.ext.typeinfo.JMethod method, 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 = getOrMakeMethodFactory(logger, ctx, method, method.getEnclosingType(), manifest, declaredOnly); ctx.finish(logger); JDeclaredType factory = ctx.getAst().searchForTypeBySource(factoryCls); factory = ctx.getAst().translate(factory); // 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 Method subclass "+factoryCls); throw new UnableToCompleteException(); }
public JMethodCall getMethodProvider(TreeLogger logger, UnifyAstView ast, com.google.gwt.core.ext.typeinfo.JMethod method, JClassLiteral classLit, boolean declaredOnly) throws UnableToCompleteException { String clsName = classLit.getRefType().getName(); ReflectionManifest manifest = ReflectionManifest.getReflectionManifest(logger, clsName, ast.getGeneratorContext()); String factoryCls = getOrMakeMethodFactory(logger, ast.getRebindPermutationOracle().getGeneratorContext(), method, method.getEnclosingType(), manifest, declaredOnly); ast.getRebindPermutationOracle().getGeneratorContext().finish(logger); JDeclaredType factory = ast.searchForTypeBySource(factoryCls); // pull out the static accessor method for (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 Method subclass "+factoryCls); throw new UnableToCompleteException(); }
public JMethodCall getConstructorProvider(TreeLogger logger, ReflectionGeneratorContext ctx, JConstructor ctor, boolean declared) throws UnableToCompleteException { // // TODO Auto-generated method stub // return null; // } // // public JMethodCall getConstructorProvider(TreeLogger logger, UnifyAstView ast, // JConstructor ctor, // JClassLiteral classLit, boolean declaredOnly) throws UnableToCompleteException { JClassLiteral classLit = ctx.getClazz(); UnifyAstView ast = ctx.getAst(); String clsName = classLit.getRefType().getName(); ReflectionManifest manifest = ReflectionManifest.getReflectionManifest(logger, clsName, ast.getGeneratorContext()); String factoryCls = getOrMakeConstructorFactory(logger, ctx, ctor, ctor.getEnclosingType(), manifest, declared); ast.getRebindPermutationOracle().getGeneratorContext().finish(logger); JDeclaredType factory = ast.searchForTypeBySource(factoryCls); // pull out the static accessor method for (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 Constructor subclass "+factoryCls); throw new UnableToCompleteException(); }
public JExpression doRebind(String clsName, ReflectionGeneratorContext params) throws UnableToCompleteException { // generate params.getLogger().log(Type.INFO, "Binding magic class for " + clsName); // JType type = params.getClazz().getRefType(); JDeclaredType type = params.getAst().searchForTypeBySource(params.getClazz().getRefType().getName()); StandardGeneratorContext ctx = params.getGeneratorContext(); Class<? extends Generator> generator = MagicClassGenerator.class; String result = ctx.runGenerator(params.getLogger(), generator, SourceUtil.toSourceName(type.getName())); ctx.finish(params.getLogger()); params.getLogger().log(Type.INFO, "Generated Class Enhancer: " + result); JDeclaredType success = params.getAst().searchForTypeBySource(result); //Okay, we've generated the correct magic class subtype; //Now pull off its static accessor method to grab our generated class. for (JMethod method : success.getMethods()) { if (method.isStatic() && method.getName().equals("enhanceClass")) { JMethodCall call = new JMethodCall(method.getSourceInfo(), null, method); call.addArg(params.getClazz().makeStatement().getExpr()); return call; } } params.getLogger().log(Type.ERROR, "Unable to load "+result+".enhanceClass()"); throw new UnableToCompleteException(); }
JMethodCall call = new JMethodCall(method.getSourceInfo().makeChild(SourceOrigin.UNKNOWN), null, method); call.addArg(params.getClazz().makeStatement().getExpr()); return call;