public ReflectionCacheSourceCreator (TreeLogger logger, GeneratorContext context, JClassType type) { this.logger = logger; this.context = context; this.type = type; this.packageName = type.getPackage().getName(); this.simpleName = type.getSimpleSourceName() + "Generated"; logger.log(Type.INFO, type.getQualifiedSourceName()); }
if (c != null && (isVisible(c.getSuperclass()))) superClass = c.getSuperclass().getErasedType().getQualifiedSourceName() + ".class"; String assignables = null; String interfaces = null; if (c != null && c.getFlattenedSupertypeHierarchy() != null) { assignables = "new HashSet<Class>(Arrays.asList("; boolean used = false; for (JType i : c.getFlattenedSupertypeHierarchy()) { if (c != null && c.getImplementedInterfaces() != null) { interfaces = "new HashSet<Class>(Arrays.asList("; boolean used = false; for (JType i : c.getImplementedInterfaces()) { if (!isVisible(i) || i.equals(t)) continue; if (used) interfaces += ", "; if (c.isEnum() != null) pb(varName + ".isEnum = true;"); if (c.isArray() != null) { pb(varName + ".isArray = true;"); pb(varName + ".isAbstract = true;"); // Arrays are _always_ abstract } else { pb(varName + ".isStatic = " + c.isStatic() + ";"); pb(varName + ".isAbstract = " + c.isAbstract() + ";"); if (c.isMemberType()) pb(varName + ".isMemberClass = true;"); if (c.isInterface() != null) pb(varName + ".isInterface = true;"); if (c.isAnnotation() != null) pb(varName + ".isAnnotation = true;");
private String getElementTypes (JField f) { StringBuilder b = new StringBuilder(); JParameterizedType params = f.getType().isParameterized(); if (params != null) { JClassType[] typeArgs = params.getTypeArgs(); b.append("new Class[] {"); for (JClassType typeArg : typeArgs) { if (typeArg.isWildcard() != null) b.append("null"); else if (!isVisible(typeArg)) b.append("null"); else if (typeArg.isClassOrInterface() != null) b.append(typeArg.isClassOrInterface().getQualifiedSourceName()).append(".class"); else if (typeArg.isParameterized() != null) b.append(typeArg.isParameterized().getQualifiedBinaryName()).append(".class"); else b.append("null"); b.append(", "); } b.append("}"); return b.toString(); } return "null"; }
private void createProxy (JClassType type) { ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(type.getPackage().getName(), type.getSimpleSourceName() + "Proxy"); PrintWriter printWriter = context.tryCreate(logger, packageName, simpleName); if (printWriter == null) { return; } SourceWriter writer = composer.createSourceWriter(context, printWriter); writer.commit(logger); }
public String getOrMakeMethodFactory(TreeLogger logger, GeneratorContext ctx, com.google.gwt.core.ext.typeinfo.JMethod method, com.google.gwt.core.ext.typeinfo.JType classType, ReflectionManifest manifest, boolean declaredOnly) throws UnableToCompleteException { // get cached manifest for this type String clsName = classType.getQualifiedSourceName(); TypeOracle oracle = ctx.getTypeOracle(); String name = method.getName(); JClassType cls = oracle.findType(clsName); if (cls == null) { logger.log(Type.ERROR, "Unable to find enclosing class "+clsName); throw new UnableToCompleteException(); } String methodFactoryName = getMethodFactoryName(cls, name, method.getParameters()); JClassType factory; String pkgName = method.getEnclosingType().getPackage().getName(); factory = oracle.findType(pkgName, methodFactoryName); if (factory == null) { return generateMethodFactory(logger, ctx, method, methodFactoryName, manifest); } else return (pkgName.length()==0?"":pkgName+".")+ methodFactoryName; }
@Override public String generate (TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { TypeOracle oracle = context.getTypeOracle(); assert (oracle != null); JClassType type = oracle.findType(typeName); if (type == null) { logger.log(ERROR, "Couldn't find type '" + typeName + "'"); throw new UnableToCompleteException(); } if (type.isInterface() == null) { logger.log(ERROR, "Type '" + typeName + "' must be an interface"); throw new UnableToCompleteException(); } ReflectionCacheSourceCreator source = new ReflectionCacheSourceCreator(logger, context, type); return source.create(); } }
@Override public String createAssignment(TreeLogger logger, ResourceContext context, JMethod method) throws UnableToCompleteException { JClassType gwtType = context.getGeneratorContext().getTypeOracle().findType( GWT.class.getName()); assert gwtType != null; return gwtType.getQualifiedSourceName() + ".create(" + method.getReturnType().getQualifiedSourceName() + ".class)"; } }
private void writeCommandDefinition(TreeLogger logger, SourceWriter srcWriter, JMethod method, JParameter callbackParameter) throws UnableToCompleteException { srcWriter.print("CommandDefinition commandDefinition = new CommandDefinition("); srcWriter.print("\"%s\", ", this.serviceType.getQualifiedSourceName()); srcWriter.print("\"%s\", ", method.getName()); if (callbackParameter != null) { JParameterizedType parameterizedCallback = callbackParameter.getType().isParameterized(); if (parameterizedCallback != null) { srcWriter.print("\"%s\" ", parameterizedCallback.getTypeArgs()[0].getQualifiedSourceName()); } else { logger.branch(TreeLogger.ERROR, "Callback argument type for method " + method.getName() + " is not parametrized", null); throw new UnableToCompleteException(); } } else { srcWriter.print("\"%s\" ", method.getReturnType().getQualifiedSourceName()); } for (JParameter parameter : method.getParameters()) { if (!parameter.equals(callbackParameter)) { srcWriter.print(", \"%s\"", parameter.getType().getQualifiedSourceName()); } } srcWriter.println(");"); }
if ( null != mappedType.isGenericType() && (!isSerializers || !typeOracle.isEnumSupertype( mappedType )) ) { nbParam = mappedType.isGenericType().getTypeParameters().length; for ( JMethod method : classType.getMethods() ) { if ( method.isStatic() && null != method.getReturnType().isClassOrInterface() && classType.isAssignableTo( method.getReturnType().isClassOrInterface() ) && method.getParameters().length == nbParam && method.isPublic() ) { MapperType[] parameters = getParameters( mappedType, method, isSerializers ); for ( JConstructor constructor : classType.getConstructors() ) { if ( constructor.isPublic() && constructor.getParameters().length == nbParam ) { MapperType[] parameters = getParameters( mappedType, constructor, isSerializers ); logger.log( Type.ERROR, "Cannot instantiate the custom serializer/deserializer " + classType.getQualifiedSourceName() + ". " + "Check the page https://github.com/nmorel/gwt-jackson/wiki/Custom-serializers-and-deserializers for more details." ); throw new UnableToCompleteException();
public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { typeOracle = context.getTypeOracle(); try { // get classType and save instance variables JClassType classType = typeOracle.getType(typeName); packageName = classType.getPackage().getName(); className = classType.getSimpleSourceName() + "Impl"; // Generate class source code generateClass(logger, context); } catch (Exception e) { // record sendNowWith logger that Map generation threw an exception logger.log(TreeLogger.ERROR, "Error generating icon factory", e); } // return the fully qualifed name of the class generated return packageName + "." + className; }
public Builder setDeclaredMethod(JMethod method) { toReturn.methodName = method.getName(); JClassType returnClass = method.getReturnType().isClassOrInterface(); toReturn.interfaceName = returnClass.getQualifiedSourceName(); toReturn.packageName = returnClass.getPackage().getName(); toReturn.simpleSourceName = returnClass.getName().replace('.', '_') + "Impl"; toReturn.dialect = returnClass.isAnnotationPresent(JsonRpcService.class) ? Dialect.JSON_RPC : Dialect.STANDARD; return this; }
/** * Given a JMethod, find the a ResourceGenerator class that will be able to * provide an implementation of the method. */ private Class<? extends ResourceGenerator> findResourceGenerator( TreeLogger logger, JMethod method) throws UnableToCompleteException { JClassType resourceType = method.getReturnType().isClassOrInterface(); assert resourceType != null; ResourceGeneratorType annotation = resourceType.findAnnotationInTypeHierarchy(ResourceGeneratorType.class); if (annotation == null) { logger.log(TreeLogger.ERROR, "No @" + ResourceGeneratorType.class.getName() + " was specifed for type " + resourceType.getQualifiedSourceName() + " or its supertypes"); throw new UnableToCompleteException(); } return annotation.value(); }
ResourceContext context, JMethod method, String[] defaultSuffixes) throws UnableToCompleteException { logger = logger.branch(TreeLogger.DEBUG, "Finding resources"); Source resourceAnnotation = method.getAnnotation(Source.class); URL[] toReturn; if (defaultSuffixes != null) { for (String extension : defaultSuffixes) { if (logger.isLoggable(TreeLogger.SPAM)) { logger.log(TreeLogger.SPAM, "Trying default extension " + extension); getPathRelativeToPackage(method.getEnclosingType().getPackage(), method.getName() + extension), locale); getPathRelativeToPackage(method.getEnclosingType().getPackage(), resource), locale); throw new UnableToCompleteException();
if (type.getQualifiedSourceName().contains("-")) { nesting--; return; JField[] fields = c.getFields(); if (fields != null) { for (JField field : fields) { gatherTypes(c.getSuperclass(), types); JClassType[] interfaces = c.getImplementedInterfaces(); if (interfaces != null) { for (JClassType i : interfaces) { gatherTypes(i.getErasedType(), types); JMethod[] methods = c.getMethods(); if (methods != null) { for (JMethod m : methods) { gatherTypes(m.getReturnType().getErasedType(), types); if (m.getParameterTypes() != null) { for (JType p : m.getParameterTypes()) { JClassType[] inner = c.getNestedTypes(); if (inner != null) { for (JClassType i : inner) { gatherTypes(i.getErasedType(), types);
logger = logger.branch(TreeLogger.DEBUG, "Computing CSS class replacements"); replacementsByClassAndMethod.put(type, replacements); for (JMethod method : type.getOverridableMethods()) { String name = method.getName(); if (ignoredMethods.contains(name)) { continue; ClassName classNameOverride = method.getAnnotation(ClassName.class); if (classNameOverride != null) { name = classNameOverride.value(); if (method.getEnclosingType() == type) { Shared shared = type.getAnnotation(Shared.class); if (shared != null) { replacementsForSharedMethods.put(method, obfuscatedClassName); if (logger.isLoggable(TreeLogger.SPAM)) { logger.log(TreeLogger.SPAM, "Mapped " + type.getQualifiedSourceName() + "." + name + " to " + obfuscatedClassName);
public String getOrMakeFieldFactory(TreeLogger logger, GeneratorContext ctx, com.google.gwt.core.ext.typeinfo.JField field, com.google.gwt.core.ext.typeinfo.JType classType, ReflectionManifest manifest, boolean declaredOnly) throws UnableToCompleteException { // get cached manifest for this type String clsName = classType.getQualifiedSourceName(); TypeOracle oracle = ctx.getTypeOracle(); String name = field.getName(); JClassType cls = oracle.findType(clsName); if (cls == null) { logger.log(Type.ERROR, "Unable to find enclosing class "+clsName); throw new UnableToCompleteException(); } String fieldFactoryName = getFieldFactoryName(cls, name); JClassType factory; String pkgName = field.getEnclosingType().getPackage().getName(); factory = oracle.findType(pkgName, fieldFactoryName); if (factory == null) { return generateFieldFactory(logger, ctx, field, fieldFactoryName, manifest); } else return (pkgName.length()==0?"":pkgName+".")+ fieldFactoryName; }
@Override public String createAssignment(TreeLogger logger, ResourceContext context, JMethod method) throws UnableToCompleteException { JParameterizedType returnType = method.getReturnType().isParameterized(); assert returnType != null; JClassType args[] = returnType.getTypeArgs(); assert args.length == 1; ClassType override = method.getAnnotation(ClassType.class); JClassType toCreate; if (override != null) { toCreate = context.getGeneratorContext().getTypeOracle().findType( override.value().getName().replace('$', '.')); assert toCreate != null; } else { toCreate = args[0]; } JClassType gwtType = context.getGeneratorContext().getTypeOracle().findType( GWT.class.getName()); assert gwtType != null; return "new " + returnType.getParameterizedQualifiedSourceName() + "() {\n public " + toCreate.getQualifiedSourceName() + " create() {\n return " + gwtType.getQualifiedSourceName() + ".create(" + toCreate.getQualifiedSourceName() + ".class);}\n" + "public String getName() { return \"" + method.getName() + "\";}}"; } }
private JClassType getBeanType(TreeLogger logger, JClassType validator, JClassType gwtSpecificInterface) throws UnableToCompleteException { if (gwtSpecificInterface instanceof JParameterizedType) { JParameterizedType paramType = (JParameterizedType) gwtSpecificInterface; return paramType.getTypeArgs()[0]; } logger.log(TreeLogger.ERROR, validator.getQualifiedSourceName() + " must implement " + GwtSpecificValidator.class.getCanonicalName() + " with a one generic parameter.", null); throw new UnableToCompleteException(); }
protected String toStringExpression(JParameter arg) throws UnableToCompleteException { Attribute attribute = getAnnotation(arg, Attribute.class); if (attribute != null) { if (arg.getType().isClass().getField(attribute.value()) != null && arg.getType().isClass().getField(attribute.value()).isPublic()) { return "(" + arg.getName() + "." + attribute.value() + "+ \"\")"; } String publicGetter = "get" + attribute.value().substring(0, 1).toUpperCase() + attribute.value().substring(1); for (JMethod jMethod : arg.getType().isClass().getMethods()) { if (jMethod.getName().equals(publicGetter)) { return "(" + arg.getName() + "." + publicGetter + "()" + "+ \"\")"; } } getLogger().log(ERROR, "Neither public argument " + attribute.value() + " nor public getter " + publicGetter + " found!"); throw new UnableToCompleteException(); } return toStringExpression(arg.getType(), arg.getName()); }
if (m.isMethod() != null) { stub.isMethod = true; stub.returnType = getType(m.isMethod().getReturnType()); stub.isStatic = m.isMethod().isStatic(); stub.isAbstract = m.isMethod().isAbstract(); stub.isNative = m.isMethod().isAbstract(); stub.isFinal = m.isMethod().isFinal(); } else { if (m.isPrivate() || m.isDefaultAccess()) { logger.log(Type.INFO, "Skipping non-visible constructor for class " + c.getName()); continue; if (m.getEnclosingType().isFinal() && !m.isPublic()) { logger.log(Type.INFO, "Skipping non-public constructor for final class" + c.getName()); continue; for (JParameter p : m.getParameters()) { stub.parameterTypes.add(getType(p.getType())); stub.jnsi += p.getType().getErasedType().getJNISignature(); String paramName = (p.getName() + "__" + p.getType().getErasedType().getJNISignature()).replaceAll( "[/;\\[\\]]", "_"); String paramInstantiation = "new Parameter(\"" + p.getName() + "\", " + getType(p.getType()) + ", \""