@Override public boolean visit(JClassLiteral x, Context ctx) { result = (JClassType)x.getRefType(); return false; }
/** * 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)); } }
@Override public JExpression injectMagic(final TreeLogger logger, final JMethodCall methodCall, final JMethod currentMethod, final Context context, final UnifyAstView ast) throws UnableToCompleteException { final JClassLiteral clazz = ReflectionUtilAst.extractClassLiteral(logger, methodCall, 0, ast); final JType type = clazz.getRefType(); if (type == null) { logger.log(Type.WARN, "ClassLiteral with null reftype: "+clazz.toSource()); return null; } return get(type.getName(), new ReflectionGeneratorContext( logger, clazz, methodCall, currentMethod, context, ast)); }
@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); } } } } } }
/** * 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(); }
final List<JExpression> args = methodCall.getArgs(); List<JExpression> emptyDims = Lists.create(), sizedDims; JType type = ast.translate(clazz.getRefType()); classLiteral = new JClassLiteral(info.makeChild(), cur); final List<JExpression> dims = Lists.addAll(sizedDims, emptyDims); final JNewArray newArr = new JNewArray(info, (JArrayType)type, dims, null, classLiteral); return new JMethodCall(info, null, registerArray, newArr, new JClassLiteral(info.makeChild(), type));
/** * 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(); }
final JType type = cur = ast.translate(clazz.getRefType()); final JIntLiteral size = ReflectionUtilAst.extractImmutableNode(logger, JIntLiteral.class, methodCall.getArgs().get(1), ast, false); cur = ((JArrayType) cur).getElementType(); classLit = new JClassLiteral(info.makeChild(), cur); return new JMethodCall(info, null, registerArray, newArr, new JClassLiteral(info.makeChild(), arrayType));
@Override public boolean visit(JClassLiteral x, Context ctx) { result = (JClassType)x.getRefType(); return false; }
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(); }
List<JExpression> args = methodCall.getArgs(); List<JExpression> emptyDims = Lists.create(), sizedDims; JType type = clazz.getRefType(); while (cur instanceof JArrayType) { JClassLiteral classLit = new JClassLiteral(info.makeChild(), cur); classLiterals = Lists.add(classLiterals, classLit); cur = ((JArrayType) cur).getElementType();
@Override public JExpression injectMagic(TreeLogger logger, JMethodCall methodCall, JMethod currentMethod, Context context, UnifyAstView ast) throws UnableToCompleteException { JClassLiteral clazz = ReflectionUtilAst.extractClassLiteral(logger, methodCall, 0, ast); JType type = clazz.getRefType(); if (type == null) { logger.log(Type.WARN, "ClassLiteral with null reftype: "+clazz.toSource()); return null; } return get(type.getName(), new ReflectionGeneratorContext( logger, clazz, methodCall, currentMethod, context, ast)); }
@Override public void endVisit(ClassLiteralAccess x, BlockScope scope) { try { SourceInfo info = makeSourceInfo(x); JType type = typeMap.get(x.targetType); push(new JClassLiteral(info, type)); } catch (Throwable e) { throw translateException(x, e); } }
public static IsQualified generatedAnnotationProviderName( final JClassLiteral classLit, final JClassLiteral annoLit) { final JType refType = classLit.getRefType(); return new IsQualified(refType.getPackageName(), toFlatSimpleName(refType)+"__"+toFlatName(annoLit.getRefType().getName())); }
String srcName = SourceUtil.toSourceName(params.getClazz().getRefType().getName()); JClassType type = params.getTypeOracle().findType(srcName); if (method.isStatic() && method.getName().equals("enhanceClass")) { JMethodCall call = new JMethodCall(method.getSourceInfo().makeChild(SourceOrigin.UNKNOWN), null, method); call.addArg(params.getClazz().makeStatement().getExpr()); return call;
Context context, UnifyAstView ast) throws UnableToCompleteException { JClassLiteral clazz = ReflectionUtilAst.extractClassLiteral(logger, methodCall, 0, ast); JType cur, type = cur =clazz.getRefType(); JIntLiteral size = ReflectionUtilAst.extractImmutableNode(logger, JIntLiteral.class, methodCall.getArgs().get(1), ast, false); cur = arrayType; while (cur instanceof JArrayType) { JClassLiteral classLit = new JClassLiteral(info.makeChild(), cur); classLits = Lists.add(classLits, classLit); cur = ((JArrayType) cur).getElementType();
arg = new JClassLiteral(type.getSourceInfo(), type);
@Override public JExpression injectMagic(TreeLogger logger, JMethodCall methodCall, JMethod currentMethod, Context context, UnifyAstView ast) throws UnableToCompleteException { if (methodCall.getArgs().size() != 1) { logger.log(Type.ERROR, "X_Reflect.magicClass accepts one and only one argument: a class literal."); throw new UnableToCompleteException(); } if (!(methodCall.getArgs().get(0) instanceof JClassLiteral)) { logger.log(Type.ERROR, "X_Reflect.magicClass accepts one and only one argument: a class literal." + " You sent a " + methodCall.getArgs().get(0).getClass() + " : " + methodCall.getArgs().get(0).toSource()); throw new UnableToCompleteException(); } JClassLiteral clazz = (JClassLiteral)methodCall.getArgs().get(0); return mappedClasses.get(clazz.getRefType().getName(), new ReflectionGeneratorContext(logger, clazz, methodCall, currentMethod, context, ast)); }
final String srcName = SourceUtil.toSourceName(params.getClazz().getRefType().getName()); final JClassType type = params.getTypeOracle().findType(srcName); if (method.isStatic() && method.getName().equals("enhanceClass")) { final JMethodCall call = new JMethodCall(method.getSourceInfo().makeChild(SourceOrigin.UNKNOWN), null, ast.translate(method)); call.addArg(params.getClazz().makeStatement().getExpr());
call.addArg(new JClassLiteral(methodSource.makeChild(SourceOrigin.UNKNOWN), asType)); JExpression invoke = MagicClassInjector.injectMagicClass(logger, call, magicClass, context, ast); if (invoke != null)