public static ArrayList<String> getTypeNames(ArrayList<JType> params) { ArrayList<String> list = new ArrayList<String>(); for (JType param : params) { list.add(param.getName()); } return list ; }
@Override public JExpression injectMagic(TreeLogger logger, JMethodCall methodCall, JMethod currentMethod, Context context, UnifyAstView ast) throws UnableToCompleteException { // TODO throw in some kind of property-based "choose your own default" here. return currentMethod.getOriginalReturnType().getDefaultValue(); }
public static String toFlatSimpleName(final JType classLit) { final StringBuilder b = new StringBuilder(); final String[] compound = classLit.getCompoundName(); for (int i = 0; i < compound.length; i++) { if (i > 0) { b.append('_'); } b.append(compound[i]); } return b.toString(); }
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())); }
public synchronized int[] getSeedArray(final TreeLogger logger, final JClassLiteral classLit) { int[] result = arraySeeds.get(classLit); if (result == null) { final int constId = initBuffer(); result = new int[] {constId, arraySeeds.size()}; final ClassBuffer into = out.getClassBuffer(); final String typeName = classLit.getRefType().getName(); if (classLit.getRefType().getDefaultValue() != JNullLiteral.INSTANCE) { // primitive fields have defaults, and require special handling into.print(typeName+"[] array"+result[1]+" = "); into.print("("+typeName+"[])setPrimitiveArray("); into.println(typeName+".class, new "+typeName+"[0]);"); } else { into.print(typeName+"[] array"+result[1]+" = setArray("); into.println(typeName+".class, new "+typeName+"[0]);"); } arraySeeds.put(classLit, result); } return result; }
@Override public void endVisit(JExpression x, Context ctx) { assert !x.getType().isExternal() || errorsFound; }
public synchronized int[] getSeedArray(TreeLogger logger, JClassLiteral classLit) { int[] result = arraySeeds.get(classLit); if (result == null) { int constId = initBuffer(); result = new int[] {constId, arraySeeds.size()}; ClassBuffer into = out.getClassBuffer(); String typeName = classLit.getRefType().getName(); if (classLit.getRefType().getDefaultValue() != JNullLiteral.INSTANCE) { // primitive fields have defaults, and require special handling into.print(typeName+"[] array"+result[1]+" = "); into.print("("+typeName+"[])setPrimitiveArray("); into.println(typeName+".class, new "+typeName+"[0]);"); } else { into.print(typeName+"[] array"+result[1]+" = setArray("); into.println(typeName+".class, new "+typeName+"[0]);"); } arraySeeds.put(classLit, result); } return result; }
/** * @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; }
@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)); }
@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 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)); }
logger.log(Type.ERROR, "Could not generate X_Inject.instance for "+arg.getType().getName());
logger.log(logLevel(), "Found class literal "+classLit.getRefType().getName()); logger.log(logLevel(), "Searching for constructor from signature "+arg0.getClass().getName()+": "+arg0); logger.log(logLevel(), "Non-final array arg used to retrieve reflection constructor " + classLit.getRefType().getName()+"(" + ReflectionUtilAst.debug(arg0)+")"); final JClassType oracleType = ast.getTypeOracle().findType(classLit.getRefType().getName().replace('$', '.')); final JConstructor ctor = ReflectionUtilType.findConstructor(logger, oracleType, ReflectionUtilAst.getTypeNames(params), isDeclared()); logger.log(Type.ERROR, "Unable to find constructor "+classLit.getRefType().getName()+ "("+params+")."); logger.log(Type.ERROR, "Did you forget to call StandardGeneratorContext.finish()?"); return null;
logger.log(logLevel(), "Found class literal "+classLit.getRefType().getName()); logger.log(logLevel(), "Searching for constructor from signature "+arg0.getClass().getName()+": "+arg0); logger.log(logLevel(), "Non-final array arg used to retrieve reflection constructor " + classLit.getRefType().getName()+"(" + ReflectionUtilAst.debug(arg0)+")"); return checkConstPool(ast, callSite, inst, arg0); JClassType oracleType = ast.getTypeOracle().findType(classLit.getRefType().getName().replace('$', '.')); JConstructor ctor = ReflectionUtilType.findConstructor(logger, oracleType, ReflectionUtilAst.getTypeNames(params), isDeclared()); logger.log(Type.ERROR, "Unable to find constructor "+classLit.getRefType().getName()+ "("+params+")."); logger.log(Type.ERROR, "Did you forget to call StandardGeneratorContext.finish()?"); return null;
com.google.gwt.core.ext.typeinfo.JClassType uiOptions; try { uiOptions = oracle.getType(uiOptionsLiteral.getRefType().getName().replace('$', '.')); } catch (final NotFoundException e) { logger.log(Type.ERROR, "Unable to load "+uiOptionsLiteral.getRefType()+" from the type oracle"); final com.google.gwt.core.ext.typeinfo.JClassType uiType = oracle.getType(uiTypeLiteral.getRefType().getName().replace('$', '.')); provider = AutoUiGenerator.generateUiProvider(logger, ast, uiOptions, uiType); } catch (final NotFoundException e) {
@Override public 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, so we test for that state on every get(). JDeclaredType type = params.getAst().searchForTypeBySource(params.getClazz().getRefType().getName()); String typeName = JGwtCreate.nameOf(type); String generatedName = ReflectionUtilJava.generatedMagicClassName(typeName); try { params.getAst().searchForTypeBySource(generatedName); }catch(NoClassDefFoundError e) { removeValue(key); } return super.get(key, params); }
/** * 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(); }