@Override public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { this.logger = logger; typeOracle = context.getTypeOracle(); try { // find XulEventSource implementors implementingTypes = new ArrayList<JClassType>(); JClassType eventSourceType = typeOracle.getType("org.pentaho.ui.xul.XulEventSource"); for (JClassType type : typeOracle.getTypes()) { if (type.isAssignableTo(eventSourceType)) { implementingTypes.add(type); } } // 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 to logger that Map generation threw an exception logger.log(TreeLogger.ERROR, "Error generating BindingContext!!!", e); } // return the fully qualifed name of the class generated return packageName + "." + className; }
/** * Add the implicit root types that are needed to make RPC work. These would be {@link String} and * {@link IncompatibleRemoteServiceException}. */ private static void addRequiredRoots( TreeLogger logger, TypeOracle typeOracle, SerializableTypeOracleBuilder stob) throws NotFoundException { logger = logger.branch(TreeLogger.DEBUG, "Analyzing implicit types"); // String is always instantiable. JClassType stringType = typeOracle.getType(String.class.getName()); stob.addRootType(logger, stringType); // IncompatibleRemoteServiceException is always serializable JClassType icseType = typeOracle.getType(IncompatibleRemoteServiceException.class.getName()); stob.addRootType(logger, icseType); }
private JClassType getType(TypeOracle oracle, Class cls) { try { return oracle.getType(cls.getName()); } catch (NotFoundException e) { throw new RuntimeException(e); } }
public static MetaClass newInstance(final TypeOracle oracle, final String type) { try { return newUncachedInstance(oracle, oracle.getType(type)); } catch (NotFoundException e) { return null; } }
@Override public String generate(TreeLogger logger, GeneratorContext genCtx, String fqInterfaceName) throws UnableToCompleteException { TypeOracle oracle = genCtx.getTypeOracle(); ResourceOracle resourceOracle = genCtx.getResourcesOracle(); JClassType interfaceType; try { interfaceType = oracle.getType(fqInterfaceName); } catch (NotFoundException e) { throw new RuntimeException(e); } DesignTimeUtils designTime; if (DesignTimeUtilsImpl.isDesignTime(fqInterfaceName)) { designTime = new DesignTimeUtilsImpl(); } else { designTime = DesignTimeUtilsStub.EMPTY; } String implName = interfaceType.getName().replace('.', '_') + "Impl"; implName = designTime.getImplName(implName); String packageName = interfaceType.getPackage().getName(); PrintWriterManager writers = new PrintWriterManager(genCtx, logger, packageName); PrintWriter printWriter = writers.tryToMakePrintWriterFor(implName); if (printWriter != null) { generateOnce( interfaceType, implName, printWriter, logger, oracle, resourceOracle, genCtx.getPropertyOracle(), writers, designTime); } return packageName + "." + implName; }
/** * Generate an implementation for the given type. * * @param logger error logger * @param context generator context * @param typeName target type name * @return generated class name * @throws UnableToCompleteException */ @Override public final String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { assert CURRENCY_LIST.equals(typeName); TypeOracle typeOracle = context.getTypeOracle(); PropertyOracle propertyOracle = context.getPropertyOracle(); LocaleUtils localeUtils = LocaleUtils.getInstance(logger, propertyOracle, context); GwtLocale locale = localeUtils.getCompileLocale(); Set<GwtLocale> runtimeLocales = localeUtils.getRuntimeLocales(); JClassType targetClass; try { targetClass = typeOracle.getType(typeName); } catch (NotFoundException e) { logger.log(TreeLogger.ERROR, "No such type", e); throw new UnableToCompleteException(); } if (runtimeLocales.isEmpty()) { return generateLocaleTree(logger, context, targetClass, locale); } CachedGeneratorContext cachedContext = new CachedGeneratorContext(context); return generateRuntimeSelection(logger, cachedContext, targetClass, locale, runtimeLocales); }
/** Adds a root type for each type that appears in the RemoteService interface methods. */ private static void addRemoteServiceRootTypes( TreeLogger logger, TypeOracle typeOracle, SerializableTypeOracleBuilder typesSentFromBrowser, SerializableTypeOracleBuilder typesSentToBrowser, JClassType remoteService) throws NotFoundException, UnableToCompleteException { logger = logger.branch( TreeLogger.DEBUG, "Analyzing '" + remoteService.getParameterizedQualifiedSourceName() + "' for serializable types", null); JMethod[] methods = remoteService.getOverridableMethods(); JClassType exceptionClass = typeOracle.getType(Exception.class.getName()); JClassType rteType = typeOracle.getType(RpcTokenException.class.getName()); JClassType rpcTokenClass = typeOracle.getType(RpcToken.class.getName()); RpcTokenImplementation tokenClassToUse = remoteService.findAnnotationInTypeHierarchy(RpcTokenImplementation.class); if (tokenClassToUse != null) { // only include serializer for the specified class literal JClassType rpcTokenType = typeOracle.getType(tokenClassToUse.value()); if (rpcTokenType.isAssignableTo(rpcTokenClass)) { typesSentFromBrowser.addRootType(logger, rpcTokenType); typesSentToBrowser.addRootType(logger, rteType); } else { logger.branch( TreeLogger.ERROR, "RPC token class " + tokenClassToUse.value() + " must implement " + RpcToken.class.getName(), null); throw new UnableToCompleteException(); } } else { JClassType[] rpcTokenSubclasses = rpcTokenClass.getSubtypes(); for (JClassType rpcTokenSubclass : rpcTokenSubclasses) { typesSentFromBrowser.addRootType(logger, rpcTokenSubclass); } if (rpcTokenSubclasses.length > 0) { typesSentToBrowser.addRootType(logger, rteType); } } TreeLogger validationLogger = logger.branch(TreeLogger.DEBUG, "Analyzing methods:", null); for (JMethod method : methods) { TreeLogger methodLogger = validationLogger.branch(TreeLogger.DEBUG, method.toString(), null); JType returnType = method.getReturnType(); if (returnType != JPrimitiveType.VOID) { TreeLogger returnTypeLogger = methodLogger.branch( TreeLogger.DEBUG, "Return type: " + returnType.getParameterizedQualifiedSourceName(), null); typesSentToBrowser.addRootType(returnTypeLogger, returnType); } JParameter[] params = method.getParameters(); for (JParameter param : params) { TreeLogger paramLogger = methodLogger.branch(TreeLogger.DEBUG, "Parameter: " + param.toString(), null); JType paramType = param.getType(); typesSentFromBrowser.addRootType(paramLogger, paramType); } JType[] exs = method.getThrows(); if (exs.length > 0) { TreeLogger throwsLogger = methodLogger.branch(TreeLogger.DEBUG, "Throws:", null); for (JType ex : exs) { if (!exceptionClass.isAssignableFrom(ex.isClass())) { throwsLogger = throwsLogger.branch( TreeLogger.WARN, "'" + ex.getQualifiedSourceName() + "' is not a checked exception; only checked exceptions may be used", null); } typesSentToBrowser.addRootType(throwsLogger, ex); } } } }
private void createFindMethod(SourceWriter sourceWriter) { // We create more than one findMethod as the body of one method would be too large. This is the // int that we // increment to add to the name // i.e. findMethod0() int methodNum = 0; // This int keeps track of how many methods are generated. When it gets to 200 we create a new // findMethodX() // and chain it to the previous. int methodCount = 0; sourceWriter.println( "private GwtBindingMethod findOrCreateMethod(String obj, String methodName){ "); sourceWriter.indent(); sourceWriter.println("GwtBindingMethod newMethod;"); // dummy first condition, rest are "else if". Keeps us from having conditional logic. sourceWriter.println("if(false){ }"); for (JClassType type : implementingTypes) { // close last method, chain it to a new one. if (methodCount > 200) { sourceWriter.println("return findOrCreateMethod" + (methodNum) + "(obj, methodName);"); sourceWriter.println("}"); sourceWriter.println( "private GwtBindingMethod findOrCreateMethod" + (methodNum++) + "(String obj, String methodName){ "); sourceWriter.println("GwtBindingMethod newMethod;"); // dummy first condition, rest are "else if". Keeps us from having conditional logic. sourceWriter.println("if(false){ }"); methodCount = 0; } String keyRoot = generateTypeKey(type); // if(type.isAbstract()){ // System.out.println("abstract"); // continue; // } // determine if there are any methods that are bindable before writing out conditional for // class JClassType loopType = type; boolean hasBindableMethods = false; JClassType eventSourceType = null; try { eventSourceType = typeOracle.getType("org.pentaho.ui.xul.XulEventSource"); } catch (NotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // CHECKSTYLE IGNORE Indentation FOR NEXT 1 LINES outer: while (loopType.getSuperclass() != null && loopType.getSimpleSourceName().equals("Object") == false && loopType.isAssignableTo(eventSourceType)) { for (JMethod m : loopType.getMethods()) { if (m.isPublic() && m.getAnnotation(Bindable.class) != null) { hasBindableMethods = true; break outer; } } loopType = loopType.getSuperclass(); } if (hasBindableMethods == false) { continue; } sourceWriter.println("else if(obj.equals(\"" + type.getQualifiedSourceName() + "\")){ "); try { loopType = type; sourceWriter.indent(); // Loop over class heirarchy and generate methods for every object that is declared a // XulEventSource while (loopType.getSuperclass() != null && loopType.getSimpleSourceName().equals("Object") == false && loopType.isAssignableTo(eventSourceType)) { String superName = generateTypeKey(loopType); boolean first = true; for (JMethod m : loopType.getMethods()) { methodCount++; if (!m.isPublic() || m.getAnnotation(Bindable.class) == null) { continue; } sourceWriter.println( (first ? "" : "else ") + "if(methodName.equals(\"" + m.getName() + "\")){ "); if (first) { first = false; } sourceWriter.indent(); String methodName = m.getName(); // check to see if we've already processed this classes' method. Point to that class // instead. if (generatedMethods.contains((superName + "_" + methodName).toLowerCase()) && type != loopType) { sourceWriter.println("return findOrCreateMethod(\"" + superName + "\", methodName);"); } else { // See if it's already been created and cached. If so, return that. String keyName = (keyRoot + "_" + methodName).toLowerCase(); sourceWriter.println( "GwtBindingMethod found = wrappedTypes.get(\"" + keyName + "\");"); sourceWriter.println("if(found != null){"); sourceWriter.indent(); sourceWriter.println("return found;"); sourceWriter.outdent(); sourceWriter.println("} else {"); sourceWriter.indent(); // Not cached, create a new instance and put it in the cache. sourceWriter.println("newMethod = new GwtBindingMethod(){"); sourceWriter.println( "public Object invoke(Object obj, Object[] args) throws XulException { "); sourceWriter.indent(); sourceWriter.println("try{"); sourceWriter.println( loopType.getQualifiedSourceName() + " target = (" + loopType.getQualifiedSourceName() + ") obj;"); JParameter[] params = m.getParameters(); String argList = ""; int pos = 0; for (JParameter param : params) { if (pos > 0) { argList += ", "; } argList += "(" + getTypeName(param.getType()) + ") args[" + pos + "]"; pos++; } if (isVoidReturn(m.getReturnType())) { sourceWriter.println("target." + methodName + "(" + argList + ");"); sourceWriter.println("return null;"); } else { sourceWriter.println( "return " + boxReturnType(m) + " target." + methodName + "(" + argList + ");"); } sourceWriter.println( "}catch(Exception e){ e.printStackTrace(); throw new XulException(\"error with " + type.getQualifiedSourceName() + "\"+e.getMessage());}"); sourceWriter.println("}"); sourceWriter.outdent(); sourceWriter.println("};"); // Add it to the HashMap cache as type and decendant type if available. sourceWriter.println("wrappedTypes.put((\"" + keyName + "\"), newMethod);"); if (keyRoot.equals(superName) == false) { sourceWriter.println("wrappedTypes.put((\"" + keyName + "\"), newMethod);"); } generatedMethods.add((keyRoot + "_" + methodName).toLowerCase()); generatedMethods.add((superName + "_" + methodName).toLowerCase()); sourceWriter.println("return newMethod;"); sourceWriter.outdent(); sourceWriter.println("}"); } sourceWriter.outdent(); sourceWriter.println("}"); } // go up a level in the heirarchy and check again. loopType = loopType.getSuperclass(); } sourceWriter.outdent(); sourceWriter.println("}"); } catch (Exception e) { // record to logger that Map generation threw an exception logger.log(TreeLogger.ERROR, "PropertyMap ERROR!!!", e); } } sourceWriter.outdent(); // This is the end of the line, if not found return null. sourceWriter.println("return null;"); sourceWriter.println("}"); }