Collection<JMethod> extractMethods( TreeLogger logger, SourceBuilder<ModelMagic> sb, GeneratorContext ctx, JClassType type) throws UnableToCompleteException { assert type.isInterface() != null; ModelMagic models = sb.getPayload(); Map<String, JMethod> uniqueMethods = new LinkedHashMap<String, JMethod>(); JMethod[] existing = type.getInheritableMethods(); Set<? extends JClassType> hierarchy = type.getFlattenedSupertypeHierarchy(); if (ModelGeneratorGwt.allAbstract(existing)) { // still an interface; grab our root type. existing = models.getRootType(logger, ctx).getInheritableMethods(); } for (JMethod method : existing) { if (!method.isAbstract()) uniqueMethods.put(xapi.dev.model.ModelGeneratorGwt.toSignature(method), method); } boolean debug = logger.isLoggable(Type.DEBUG); for (JClassType next : hierarchy) { if (next.isInterface() != null) { sb.getClassBuffer().addInterfaces(next.getQualifiedSourceName()); for (JMethod method : next.getMethods()) { String sig = ModelGeneratorGwt.toSignature(method); if (!uniqueMethods.containsKey(sig)) uniqueMethods.put(sig, method); } } else { for (JMethod method : next.getMethods()) { String sig = ModelGeneratorGwt.toSignature(method); if (uniqueMethods.containsKey(sig)) { if (debug) logger.log( Type.WARN, "Found multiple model methods for " + type.getName() + "::" + sig + "; preferring method from " + uniqueMethods.get(sig).getEnclosingType().getName()); } else { uniqueMethods.put(sig, method); } } } } return uniqueMethods.values(); }
public void build( TreeLogger logger, SourceBuilder<ModelMagic> builder, GeneratorContext ctx, JClassType type) throws UnableToCompleteException { ModelMagic models = builder.getPayload(); ModelGenerator generator = new ModelGenerator(builder); // Step one; determine if we already have a concrete type or not. // TODO if JClassType is final, we need to wrap it using a delegate model. JClassType concrete; // ClassBuffer cb = builder.getClassBuffer(); JClassType root = models.getRootType(logger, ctx); if (type.isInterface() == null && type != root) { concrete = type; generator.setSuperClass(type.getQualifiedSourceName()); } else { // We have an interface on our hands; search for an existing or // buildable model. // search for a supertype to inherit. Anything that extends Model, // does not implement any method we do implement, preferred type // being the one that implements the most interfaces possible final JClassType model; try { model = ctx.getTypeOracle().getType(Model.class.getName()); } catch (NotFoundException e) { logger.log( Type.ERROR, "Cannot load " + Model.class.getName() + "; " + "make sure you have xapi-gwt-model:sources.jar on classpath."); throw new UnableToCompleteException(); } concrete = model; for (JClassType supertype : concrete.getFlattenedSupertypeHierarchy()) { // Only interfaces explicitly extending Model become concrete. if (ModelGeneratorGwt.canBeSupertype(type, supertype)) { // prefer the concrete type with the most methods in common. concrete = supertype; } } if (concrete == null || concrete == model) { concrete = models.getRootType(logger, ctx); generator.setSuperClass(concrete.getQualifiedSourceName()); } else { // We have to make sure this concrete supertype is created. if (!models.hasModel(concrete.getQualifiedSourceName())) { // Concrete type is not cached. Build it now. RebindResult result = ModelGeneratorGwt.execImpl(logger, ctx, concrete.getQualifiedSourceName()); generator.setSuperClass(result.getResultTypeName()); } } } // This will probably become jsni, if we can avoid jso interface sickness... generator.createFactory(type.getQualifiedSourceName()); HasModelFields fieldMap = new HasModelFields(); fieldMap.setDefaultSerializable(type.getAnnotation(Serializable.class)); for (JMethod method : methods.keySet()) { if (!toGenerate.contains(ModelGeneratorGwt.toSignature(method))) { logger.log( Type.TRACE, "Skipping method defined in supertype: " + method.getJsniSignature()); continue; } Annotation[] annos = methods.get(method); String methodName = method.getName(); String returnType = method.getReturnType().getQualifiedSourceName(); String params = ModelGeneratorGwt.typeToParameterString(method.getParameterTypes()); // TODO: check imports if we are safe to use simple name. returnType = method.getReturnType().getSimpleSourceName(); IsType returns = X_Source.binaryToSource(method.getReturnType().getQualifiedBinaryName()); IsType[] parameters = ModelGeneratorGwt.toTypes(method.getParameterTypes()); GetterFor getter = method.getAnnotation(GetterFor.class); if (getter != null) { String name = getter.value(); if (name.length() == 0) { name = ModelUtil.stripGetter(method.getName()); } ModelField field = fieldMap.getOrMakeField(name); field.setType(returnType); assert parameters.length == 0 : "A getter method cannot have parameters. " + "Generated code requires using getter methods without args. You provided " + method.getJsniSignature(); grabAnnotations(logger, models, fieldMap, method, annos, type); field.addGetter(returns, methodName); continue; } SetterFor setter = method.getAnnotation(SetterFor.class); if (setter != null) { String name = setter.value(); if (name.length() == 0) name = ModelUtil.stripSetter(method.getName()); grabAnnotations(logger, models, fieldMap, method, annos, type); continue; } if (method.getAnnotation(DeleterFor.class) != null) { implementAction(logger, generator, method, models, annos); continue; } // No annotation. We have to guess the type. boolean isVoid = method.getReturnType().isPrimitive() == JPrimitiveType.VOID; boolean isGetter = methodName.startsWith("get") || methodName.startsWith("is") || methodName.startsWith("has"); boolean isSetter, isAction; if (isGetter) { assert !isVoid : "Cannot have a void return type with method name " + methodName + "; getter prefixes get(), is() and has() must return a type."; isSetter = false; isAction = false; } else { isSetter = methodName.startsWith("set") || methodName.startsWith("add") || methodName.startsWith("put") || methodName.startsWith("rem") || methodName.startsWith("remove"); if (isSetter) { isAction = false; } else { isAction = true; } } if (isVoid) { // definitely a setter / action method. if (isSetter) { MethodBuffer mb = generator.createMethod(returnType, methodName, params); implementSetter(logger, mb, method, models, fieldMap, annos); } else if (isAction) { implementAction(logger, generator, method, models, annos); } else { MethodBuffer mb = generator.createMethod(returnType, methodName, params); implementException(logger, mb, method); } } else { if (isGetter) { String name = ModelUtil.stripGetter(method.getName()); ModelField field = fieldMap.getOrMakeField(name); field.setType(returnType); field.addGetter(returns, methodName); grabAnnotations(logger, models, fieldMap, method, annos, type); } else if (isSetter) { MethodBuffer mb = generator.createMethod(returnType, methodName, params); implementSetter(logger, mb, method, models, fieldMap, annos); } else if (isAction) { implementAction(logger, generator, method, models, annos); } else { MethodBuffer mb = generator.createMethod(returnType, methodName, params); implementException(logger, mb, method); } } } generator.generateModel( X_Source.toType(builder.getPackage(), builder.getClassBuffer().getSimpleName()), fieldMap); }
public ProxyCreator(JClassType serviceIntf) { assert (serviceIntf.isInterface() != null); this.serviceIntf = serviceIntf; }
private static boolean isInterfaceType(JClassType type, Class<?> class1) { return type.isInterface() != null && isType(type, class1); }
protected List<BeanResolver> getIntrospectableTypes(TreeLogger logger, TypeOracle oracle) { ArrayList<BeanResolver> results = new ArrayList<BeanResolver>(); HashSet<BeanResolver> resolvers = new HashSet<BeanResolver>(); HashSet<String> found = new HashSet<String>(); try { JClassType[] types = oracle.getTypes(); for (JClassType type : types) { if (!found.contains(type.getQualifiedSourceName()) && isIntrospectable(logger, type) && (type.isInterface() == null)) { found.add(type.getQualifiedSourceName()); resolvers.add(new BeanResolver(logger, type)); } } // Do a crazy assed sort to make sure least // assignable types are at the bottom of the list results.addAll(resolvers); results.addAll(this.getFileDeclaredTypes(logger, oracle)); boolean swap = true; // .print("Ordering "+results.size()+" by heirarchy "); while (swap) { // .print("."); swap = false; for (int i = results.size() - 1; i >= 0; i--) { BeanResolver type = (BeanResolver) results.get(i); for (int j = i - 1; j >= 0; j--) { BeanResolver check = (BeanResolver) results.get(j); if (type.getType().isAssignableTo(check.getType())) { results.set(i, check); results.set(j, type); type = check; swap = true; } } } } // System.out.println(); } catch (Exception e) { logger.log(TreeLogger.ERROR, "Unable to finad Introspectable types.", e); } // for(BeanResolver rs:results){ // logger.log(TreeLogger.ERROR, rs.toString()); // } // System.out.println("Found "+results.size()+" introspectable types."); for (BeanResolver resolver : results) { logger.log( TreeLogger.Type.INFO, "Introspectable: " + resolver.getType().getQualifiedSourceName()); } logger.log(TreeLogger.Type.INFO, "Found introspectable types: " + results.size()); return results; }