private Set<String> getEditorFieldCleanupExpressions() { Set<String> result = new LinkedHashSet<>(); for (JClassType typeCandidate : model.getEditorType().getFlattenedSupertypeHierarchy()) { JClassType classType = typeCandidate.isClass(); if (classType != null) { for (JField field : classType.getFields()) { JClassType fieldClassOrInterfaceType = field.getType().isClassOrInterface(); if (fieldClassOrInterfaceType != null // field type assignable to HasCleanup .. && fieldClassOrInterfaceType.isAssignableTo(hasCleanupType) // .. but not assignable to Model && !fieldClassOrInterfaceType.isAssignableTo(baseModelType)) { result.add( String.format( "getEditor().%s", //$NON-NLS-1$ field.getName())); } } } } return result; }
private void generateMethodParamToHeaderCodeForSimpleType( SourcePrinter srcWriter, String builderVarName, String headerName, JType parameterType, String parameterexpression, String parameterCheckExpression) { JClassType jClassType = parameterType.isClassOrInterface(); srcWriter.println("if (" + parameterCheckExpression + "){"); if (jClassType != null) { if (jClassType.isAssignableTo(stringType)) { srcWriter.println( builderVarName + ".setHeader(" + EscapeUtils.quote(headerName) + ", URL.encodePathSegment(" + parameterexpression + "));"); } else if (jClassType.isAssignableTo(dateType)) { srcWriter.println( builderVarName + ".setHeader(" + EscapeUtils.quote(headerName) + ", URL.encodePathSegment(" + "Long.toString(" + parameterexpression + ".getTime())));"); } else { srcWriter.println( builderVarName + ".setHeader(" + EscapeUtils.quote(headerName) + ", URL.encodePathSegment(" + "\"\"+" + parameterexpression + "));"); } } else { srcWriter.println( builderVarName + ".setHeader(" + EscapeUtils.quote(headerName) + ", URL.encodePathSegment(" + "\"\"+" + parameterexpression + "));"); } srcWriter.println("}"); }
// BEGIN MODIFICATION private FieldManager getFieldManager( TypeOracle oracle, MortalLogger logger, PropertyOracle propertyOracle, boolean useLazyWidgetBuilder) throws UnableToCompleteException { // Find ginjector FieldManager fieldManager; try { String ginjectorClassName = propertyOracle.getConfigurationProperty("gin.ginjector").getValues().get(0); JClassType ginjectorClass = oracle.findType(ginjectorClassName); if (ginjectorClass == null || !ginjectorClass.isAssignableTo(oracle.findType(Ginjector.class.getCanonicalName()))) { logger.die( "The configuration property 'gin.ginjector' is '%s' " + " which doesn't identify a type inheriting from 'Ginjector'.", ginjectorClassName); } fieldManager = new GinFieldManager(oracle, logger, ginjectorClass, useLazyWidgetBuilder); } catch (BadPropertyValueException e) { logger.warn( "The configuration property 'gin.ginjector' was not found, it is required to use " + "gin injection for UiBinder fields. If you don't need this consider using " + "UiBinder.gwt.xml instead of GinUiBinder.gwt.xml in your module."); fieldManager = new FieldManager(oracle, logger, useLazyWidgetBuilder); } return fieldManager; }
@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; }
private boolean isOverlayArrayType(JClassType type) { for (JClassType arrayType : OVERLAY_ARRAY_TYPES) { if (type.isAssignableTo(arrayType)) { return true; } } return false; }
/** * @param className * @return */ private String getInstantiationClass(String className) { if (className.endsWith("Async")) { String serviceInterface = className.substring(0, className.length() - 5); JClassType type = context.getGeneratorContext().getTypeOracle().findType(serviceInterface); if (type != null && type.isAssignableTo(remoteServiceType)) { return type.getQualifiedSourceName(); } } return className; }
private boolean isQueryParamListType(JClassType type) { if (type.isParameterized() == null) { return false; } for (JClassType listType : QUERY_PARAM_LIST_TYPES) { if (type.isAssignableTo(listType)) { return true; } } return false; }
private void checkSerializable(TreeLogger logger, JClassType type) throws UnableToCompleteException { JClassType javaSerializable = type.getOracle().findType(Serializable.class.getName()); boolean serializable = type.isAssignableTo(javaSerializable); if (!serializable) { boolean abortCompile = "true".equals(System.getProperty(FAIL_IF_NOT_SERIALIZABLE)); logger.log( abortCompile ? Type.ERROR : Type.WARN, type + " is used in RPC or shared state but does not implement " + Serializable.class.getName() + ". Communication will work but the Application on server side cannot be serialized if it refers to objects of this type. " + "If the system property " + FAIL_IF_NOT_SERIALIZABLE + " is set to \"true\", this causes the compilation to fail instead of just emitting a warning."); if (abortCompile) { throw new UnableToCompleteException(); } } }
/** @return */ protected void initializeController(View view) { controllerName = templateParser.getTemplateController(view, baseIntf.getQualifiedSourceName(), device); String controllerClassName = ClientControllers.getController(controllerName, device); if (controllerClassName == null) { throw new CruxGeneratorException( "Error generating invoker. Controller [" + controllerName + "] not found."); } controllerClass = context.getTypeOracle().findType(controllerClassName); if (controllerClass == null) { String message = "Controller class [" + controllerName + "] , declared on view [" + view.getId() + "], could not be loaded. " + "\n Possible causes:" + "\n\t 1. Check if any type or subtype used by controller refers to another module and if this module is inherited in the .gwt.xml file." + "\n\t 2. Check if your controller or its members belongs to a client package." + "\n\t 3. Check the versions of all your modules."; throw new CruxGeneratorException(message); } Controller controllerAnnot = controllerClass.getAnnotation(Controller.class); if (controllerAnnot == null) { throw new CruxGeneratorException( "DeviceAdaptive implementation class [" + controllerClass.getQualifiedSourceName() + "] is not a valid Controller. It must be annotated with @Controller annotation."); } if (!controllerClass.isAssignableTo(deviceAdaptiveControllerClass)) { throw new CruxGeneratorException( "DeviceAdaptive implementation class [" + controllerClass.getQualifiedSourceName() + "] must externds the base class DeviceAdaptiveController."); } }
/** * @param srcWriter * @param className */ private void generateContainerInstatiationMethod(SourcePrinter srcWriter, String className) { try { srcWriter.println( "public " + className + " get" + className.replace('.', '_') + "(" + Scope.class.getCanonicalName() + " scope, String subscope){"); JClassType type = JClassUtils.getType(context.getGeneratorContext().getTypeOracle(), className); IocConfigImpl<?> iocConfig = (IocConfigImpl<?>) configurations.get(className); Class<?> providerClass = iocConfig.getProviderClass(); if (providerClass != null) { srcWriter.println( className + " result = _getScope(scope).getValue(GWT.create(" + providerClass.getCanonicalName() + ".class), " + EscapeUtils.quote(className) + ", subscope, "); generateFieldsPopulationCallback(srcWriter, type); srcWriter.println(");"); } else if (iocConfig.getToClass() != null) { srcWriter.println( className + " result = _getScope(scope).getValue(new " + IocProvider.class.getCanonicalName() + "<" + className + ">(){"); srcWriter.println("public " + className + " get(){"); srcWriter.println( "return GWT.create(" + iocConfig.getToClass().getCanonicalName() + ".class);"); srcWriter.println("}"); srcWriter.println("}, " + EscapeUtils.quote(className) + ", subscope, "); generateFieldsPopulationCallback(srcWriter, type); srcWriter.println(");"); } else { srcWriter.println( className + " result = _getScope(scope).getValue(new " + IocProvider.class.getCanonicalName() + "<" + className + ">(){"); srcWriter.println("public " + className + " get(){"); String instantiationClass = getInstantiationClass(className); JClassType instantiationType = context.getGeneratorContext().getTypeOracle().findType(instantiationClass); if (instantiationType == null) { throw new CruxGeneratorException("Can not found type: " + instantiationClass); } if (instantiationType.isAssignableTo(remoteServiceType) && ConfigurationFactory.getConfigurations() .sendCruxViewNameOnClientRequests() .equals("true")) { srcWriter.println(className + " ret = GWT.create(" + instantiationClass + ".class);"); srcWriter.println( "((" + ServiceDefTarget.class.getCanonicalName() + ")ret).setRpcRequestBuilder(new " + CruxRpcRequestBuilder.class.getCanonicalName() + "(getBoundCruxViewId()));"); srcWriter.println("return ret;"); } else { srcWriter.println("return GWT.create(" + instantiationClass + ".class);"); } srcWriter.println("}"); srcWriter.println("}, " + EscapeUtils.quote(className) + ", subscope, "); generateFieldsPopulationCallback(srcWriter, type); srcWriter.println(");"); } if (type.isAssignableTo(viewBindableType)) { srcWriter.println( "if (scope != " + Scope.class.getCanonicalName() + "." + Scope.SINGLETON.name() + " && result.getBoundCruxViewId() == null){"); srcWriter.println("result.bindCruxView(this.getBoundCruxViewId());"); srcWriter.println("}"); } srcWriter.println("return result;"); srcWriter.println("}"); } catch (NotFoundException e) { throw new IoCException("IoC Error Class [" + className + "] not found.", e); } }
private String encodeDecodeExpression( JType type, String expression, Style style, String encoderMethod, String mapMethod, String setMethod, String listMethod) throws UnableToCompleteException { if (null != type.isEnum()) { if (encoderMethod.equals("encode")) { return encodeDecodeExpression( STRING_TYPE, expression + ".name()", style, encoderMethod, mapMethod, setMethod, listMethod); } else { return type.getQualifiedSourceName() + ".valueOf(" + encodeDecodeExpression( STRING_TYPE, expression, style, encoderMethod, mapMethod, setMethod, listMethod) + ")"; } } String encoderDecoder = getEncoderDecoder(type, logger); if (encoderDecoder != null) { return encoderDecoder + "." + encoderMethod + "(" + expression + ")"; } JClassType clazz = type.isClassOrInterface(); if (isCollectionType(clazz)) { JParameterizedType parameterizedType = type.isParameterized(); if (parameterizedType == null || parameterizedType.getTypeArgs() == null) { error("Collection types must be parameterized."); } JClassType[] types = parameterizedType.getTypeArgs(); if (clazz.isAssignableTo(MAP_TYPE)) { if (types.length != 2) { error("Map must define two and only two type parameters"); } if (types[0] != STRING_TYPE) { error("Map's first type parameter must be of type String"); } encoderDecoder = getEncoderDecoder(types[1], logger); if (encoderDecoder != null) { return mapMethod + "(" + expression + ", " + encoderDecoder + ", " + JSON_CLASS + ".Style." + style.name() + ")"; } } else if (clazz.isAssignableTo(SET_TYPE)) { if (types.length != 1) { error("Set must define one and only one type parameter"); } encoderDecoder = getEncoderDecoder(types[0], logger); if (encoderDecoder != null) { return setMethod + "(" + expression + ", " + encoderDecoder + ")"; } } else if (clazz.isAssignableTo(LIST_TYPE)) { if (types.length != 1) { error("List must define one and only one type parameter"); } encoderDecoder = getEncoderDecoder(types[0], logger); info("type encoder for: " + types[0] + " is " + encoderDecoder); if (encoderDecoder != null) { return listMethod + "(" + expression + ", " + encoderDecoder + ")"; } } } error("Do not know how to encode/decode " + type); return null; }
/** 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("}"); }
@Override public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { // make sure it is an interface TypeOracle oracle = context.getTypeOracle(); propertyAccessInterface = oracle.findType(Name.getSourceNameForClass(PropertyAccess.class)); modelKeyProviderInterface = oracle.findType(Name.getSourceNameForClass(ModelKeyProvider.class)); valueProviderInterface = oracle.findType(Name.getSourceNameForClass(ValueProvider.class)); labelProviderInterface = oracle.findType(Name.getSourceNameForClass(LabelProvider.class)); JClassType toGenerate = oracle.findType(typeName).isInterface(); if (toGenerate == null) { logger.log(TreeLogger.ERROR, typeName + " is not an interface type"); throw new UnableToCompleteException(); } if (!toGenerate.isAssignableTo(propertyAccessInterface)) { logger.log(Type.ERROR, "This isn't a PropertyAccess subtype..."); throw new UnableToCompleteException(); } // Get the name of the new type String packageName = toGenerate.getPackage().getName(); String simpleSourceName = toGenerate.getName().replace('.', '_') + "Impl"; PrintWriter pw = context.tryCreate(logger, packageName, simpleSourceName); if (pw == null) { return packageName + "." + simpleSourceName; } // start making the class, with basic imports ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName, simpleSourceName); factory.addImplementedInterface(typeName); SourceWriter sw = factory.createSourceWriter(context, pw); // for each method, for (JMethod m : toGenerate.getOverridableMethods()) { TreeLogger l = logger.branch(Type.DEBUG, "Building method: " + m.getReadableDeclaration()); // no support for params at this time if (m.getParameters().length != 0) { l.log(Type.ERROR, "Method " + m.toString() + " must not have parameters."); throw new UnableToCompleteException(); } // ask for the types that provide the property data JClassType ret = m.getReturnType().isClassOrInterface(); final AbstractCreator c; if (ret.isAssignableTo(valueProviderInterface)) { c = new ValueProviderCreator(context, l, m); } else if (ret.isAssignableTo(modelKeyProviderInterface)) { c = new ModelKeyProviderCreator(context, l, m); } else if (ret.isAssignableTo(labelProviderInterface)) { c = new LabelProviderCreator(context, l, m); } else { logger.log(Type.ERROR, "Method uses a return type that cannot be generated"); throw new UnableToCompleteException(); } c.create(); // build the method // public ValueProvider<T, V> name() { return NameValueProvider.instance; // } sw.println("public %1$s %2$s() {", m.getReturnType().getQualifiedSourceName(), m.getName()); sw.indentln("return %1$s;", c.getInstanceExpression()); sw.println("}"); } sw.commit(logger); return factory.getCreatedClassName(); }
boolean isCollectionType(JClassType clazz) { return clazz != null && (clazz.isAssignableTo(SET_TYPE) || clazz.isAssignableTo(LIST_TYPE) || clazz.isAssignableTo(MAP_TYPE)); }
private void generateMethodParamToCookieCodeForSimpleType( SourcePrinter srcWriter, String cookieName, JType parameterType, String parameterexpression, String parameterCheckExpression) { JClassType jClassType = parameterType.isClassOrInterface(); if (jClassType != null) { if (jClassType.isAssignableTo(stringType)) { srcWriter.println( Cookies.class.getCanonicalName() + ".setCookie(" + EscapeUtils.quote(cookieName) + ", " + "(" + parameterCheckExpression + "?" + parameterexpression + ":\"\"), new " + Date.class.getCanonicalName() + "(2240532000000L), null, \"/\", false);"); } else if (jClassType.isAssignableTo(dateType)) { srcWriter.println( Cookies.class.getCanonicalName() + ".setCookie(" + EscapeUtils.quote(cookieName) + ", " + "(" + parameterCheckExpression + "?Long.toString(" + parameterexpression + ".getTime()):\"\"), new " + Date.class.getCanonicalName() + "(2240532000000L), null, \"/\", false);"); } else { srcWriter.println( Cookies.class.getCanonicalName() + ".setCookie(" + EscapeUtils.quote(cookieName) + ", " + "(" + parameterCheckExpression + "?(\"\"+" + parameterexpression + "):\"\"), new " + Date.class.getCanonicalName() + "(2240532000000L), null, \"/\", false);"); } } else { srcWriter.println( Cookies.class.getCanonicalName() + ".setCookie(" + EscapeUtils.quote(cookieName) + ", " + "(" + parameterCheckExpression + "?(\"\"+" + parameterexpression + "):\"\"), new " + Date.class.getCanonicalName() + "(2240532000000L), null, \"/\", false);"); } }
private void writeMethodImpl(JMethod method) throws UnableToCompleteException { boolean returnRequest = false; if (method.getReturnType() != JPrimitiveType.VOID) { if (!method.getReturnType().getQualifiedSourceName().equals(Request.class.getName()) && !method .getReturnType() .getQualifiedSourceName() .equals(JsonpRequest.class.getName())) { getLogger() .log( ERROR, "Invalid rest method. Method must have void, Request or JsonpRequest return types: " + method.getReadableDeclaration()); throw new UnableToCompleteException(); } else { returnRequest = true; } } Json jsonAnnotation = source.getAnnotation(Json.class); final Style classStyle = jsonAnnotation != null ? jsonAnnotation.style() : Style.DEFAULT; Options classOptions = source.getAnnotation(Options.class); Options options = method.getAnnotation(Options.class); p(method.getReadableDeclaration(false, false, false, false, true) + " {").i(1); { String restMethod = getRestMethod(method); LinkedList<JParameter> args = new LinkedList<JParameter>(Arrays.asList(method.getParameters())); // the last arg should be the callback. if (args.isEmpty()) { getLogger() .log( ERROR, "Invalid rest method. Method must declare at least a callback argument: " + method.getReadableDeclaration()); throw new UnableToCompleteException(); } JParameter callbackArg = args.removeLast(); JClassType callbackType = callbackArg.getType().isClassOrInterface(); JClassType methodCallbackType = METHOD_CALLBACK_TYPE; if (callbackType == null || !callbackType.isAssignableTo(methodCallbackType)) { getLogger() .log( ERROR, "Invalid rest method. Last argument must be a " + methodCallbackType.getName() + " type: " + method.getReadableDeclaration()); throw new UnableToCompleteException(); } JClassType resultType = getCallbackTypeGenericClass(callbackType); String pathExpression = null; Path pathAnnotation = method.getAnnotation(Path.class); if (pathAnnotation != null) { pathExpression = wrap(pathAnnotation.value()); } JParameter contentArg = null; HashMap<String, JParameter> queryParams = new HashMap<String, JParameter>(); HashMap<String, JParameter> formParams = new HashMap<String, JParameter>(); HashMap<String, JParameter> headerParams = new HashMap<String, JParameter>(); for (JParameter arg : args) { PathParam paramPath = arg.getAnnotation(PathParam.class); if (paramPath != null) { if (pathExpression == null) { getLogger() .log( ERROR, "Invalid rest method. Invalid @PathParam annotation. Method is missing the @Path annotation: " + method.getReadableDeclaration()); throw new UnableToCompleteException(); } pathExpression = pathExpression(pathExpression, arg, paramPath); // .replaceAll(Pattern.quote("{" + paramPath.value() + "}"), // "\"+com.google.gwt.http.client.URL.encodePathSegment(" + toStringExpression(arg) + // ")+\""); if (arg.getAnnotation(Attribute.class) != null) { // allow part of the arg-object participate in as PathParam and the object goes over the // wire contentArg = arg; } continue; } QueryParam queryParam = arg.getAnnotation(QueryParam.class); if (queryParam != null) { queryParams.put(queryParam.value(), arg); continue; } FormParam formParam = arg.getAnnotation(FormParam.class); if (formParam != null) { formParams.put(formParam.value(), arg); continue; } HeaderParam headerParam = arg.getAnnotation(HeaderParam.class); if (headerParam != null) { headerParams.put(headerParam.value(), arg); continue; } if (!formParams.isEmpty()) { getLogger() .log( ERROR, "You can not have both @FormParam parameters and a content parameter: " + method.getReadableDeclaration()); throw new UnableToCompleteException(); } if (contentArg != null) { getLogger() .log( ERROR, "Invalid rest method. Only one content parameter is supported: " + method.getReadableDeclaration()); throw new UnableToCompleteException(); } contentArg = arg; } String acceptTypeBuiltIn = null; if (callbackType.equals(TEXT_CALLBACK_TYPE)) { acceptTypeBuiltIn = "CONTENT_TYPE_TEXT"; } else if (callbackType.equals(JSON_CALLBACK_TYPE)) { acceptTypeBuiltIn = "CONTENT_TYPE_JSON"; } else if (callbackType.isAssignableTo(OVERLAY_CALLBACK_TYPE)) { acceptTypeBuiltIn = "CONTENT_TYPE_JSON"; } else if (callbackType.equals(XML_CALLBACK_TYPE)) { acceptTypeBuiltIn = "CONTENT_TYPE_XML"; } p("final " + METHOD_CLASS + " __method ="); p("getResource()"); if (pathExpression != null) { p(".resolve(" + pathExpression + ")"); } for (Map.Entry<String, JParameter> entry : queryParams.entrySet()) { String expr = entry.getValue().getName(); JClassType type = entry.getValue().getType().isClassOrInterface(); if (type != null && isQueryParamListType(type)) { p( ".addQueryParams(" + wrap(entry.getKey()) + ", " + toIteratedStringExpression(entry.getValue()) + ")"); } else { p( ".addQueryParam(" + wrap(entry.getKey()) + ", " + toStringExpression(entry.getValue().getType(), expr) + ")"); } } // example: .get() p("." + restMethod + "();"); // Handle JSONP specific configuration... JSONP jsonpAnnotation = method.getAnnotation(JSONP.class); final boolean isJsonp = restMethod.equals(METHOD_JSONP) && jsonpAnnotation != null; if (isJsonp) { if (returnRequest && !method .getReturnType() .getQualifiedSourceName() .equals(JsonpRequest.class.getName())) { getLogger() .log( ERROR, "Invalid rest method. JSONP method must have void or JsonpRequest return types: " + method.getReadableDeclaration()); throw new UnableToCompleteException(); } if (jsonpAnnotation.callbackParam().length() > 0) { p( "((" + JSONP_METHOD_CLASS + ")__method).callbackParam(" + wrap(jsonpAnnotation.callbackParam()) + ");"); } if (jsonpAnnotation.failureCallbackParam().length() > 0) { p( "((" + JSONP_METHOD_CLASS + ")__method).failureCallbackParam(" + wrap(jsonpAnnotation.failureCallbackParam()) + ");"); } } else { if (returnRequest && !method.getReturnType().getQualifiedSourceName().equals(Request.class.getName())) { getLogger() .log( ERROR, "Invalid rest method. Non JSONP method must have void or Request return types: " + method.getReadableDeclaration()); throw new UnableToCompleteException(); } } // configure the dispatcher if (options != null && options.dispatcher() != Dispatcher.class) { // use the dispatcher configured for the method. p("__method.setDispatcher(" + options.dispatcher().getName() + ".INSTANCE);"); } else { // use the default dispatcher configured for the service.. p("__method.setDispatcher(this.dispatcher);"); } // configure the expected statuses.. if (options != null && options.expect().length != 0) { // Using method level defined expected status p("__method.expect(" + join(options.expect(), ", ") + ");"); } else if (classOptions != null && classOptions.expect().length != 0) { // Using class level defined expected status p("__method.expect(" + join(classOptions.expect(), ", ") + ");"); } // configure the timeout if (options != null && options.timeout() >= 0) { // Using method level defined value p("__method.timeout(" + options.timeout() + ");"); } else if (classOptions != null && classOptions.timeout() >= 0) { // Using class level defined value p("__method.timeout(" + classOptions.timeout() + ");"); } if (jsonpAnnotation == null) { Produces producesAnnotation = findAnnotationOnMethodOrEnclosingType(method, Produces.class); if (producesAnnotation != null) { p( "__method.header(" + RESOURCE_CLASS + ".HEADER_ACCEPT, " + wrap(producesAnnotation.value()[0]) + ");"); } else { // set the default accept header.... if (acceptTypeBuiltIn != null) { p( "__method.header(" + RESOURCE_CLASS + ".HEADER_ACCEPT, " + RESOURCE_CLASS + "." + acceptTypeBuiltIn + ");"); } else { p( "__method.header(" + RESOURCE_CLASS + ".HEADER_ACCEPT, " + RESOURCE_CLASS + ".CONTENT_TYPE_JSON);"); } } Consumes consumesAnnotation = findAnnotationOnMethodOrEnclosingType(method, Consumes.class); if (consumesAnnotation != null) { p( "__method.header(" + RESOURCE_CLASS + ".HEADER_CONTENT_TYPE, " + wrap(consumesAnnotation.value()[0]) + ");"); } // and set the explicit headers now (could override the accept header) for (Map.Entry<String, JParameter> entry : headerParams.entrySet()) { String expr = entry.getValue().getName(); p( "__method.header(" + wrap(entry.getKey()) + ", " + toStringExpression(entry.getValue().getType(), expr) + ");"); } } if (!formParams.isEmpty()) { p(FORM_POST_CONTENT_CLASS + " __formPostContent = new " + FORM_POST_CONTENT_CLASS + "();"); for (Map.Entry<String, JParameter> entry : formParams.entrySet()) { p( "__formPostContent.addParameter(" + wrap(entry.getKey()) + ", " + toFormStringExpression(entry.getValue(), classStyle) + ");"); } p("__method.form(__formPostContent.getTextContent());"); } if (contentArg != null) { if (contentArg.getType() == STRING_TYPE) { p("__method.text(" + contentArg.getName() + ");"); } else if (contentArg.getType() == JSON_VALUE_TYPE) { p("__method.json(" + contentArg.getName() + ");"); } else if (contentArg.getType().isClass() != null && isOverlayArrayType(contentArg.getType().isClass())) { p("__method.json(new " + JSON_ARRAY_CLASS + "(" + contentArg.getName() + "));"); } else if (contentArg.getType().isClass() != null && contentArg.getType().isClass().isAssignableTo(OVERLAY_VALUE_TYPE)) { p("__method.json(new " + JSON_OBJECT_CLASS + "(" + contentArg.getName() + "));"); } else if (contentArg.getType() == DOCUMENT_TYPE) { p("__method.xml(" + contentArg.getName() + ");"); } else { JClassType contentClass = contentArg.getType().isClass(); if (contentClass == null) { contentClass = contentArg.getType().isClassOrInterface(); if (!locator.isCollectionType(contentClass)) { getLogger().log(ERROR, "Content argument must be a class."); throw new UnableToCompleteException(); } } jsonAnnotation = contentArg.getAnnotation(Json.class); Style style = jsonAnnotation != null ? jsonAnnotation.style() : classStyle; // example: // .json(Listings$_Generated_JsonEncoder_$.INSTANCE.encode(arg0) // ) p( "__method.json(" + locator.encodeExpression(contentClass, contentArg.getName(), style) + ");"); } } List<AnnotationResolver> annotationResolvers = getAnnotationResolvers(context, getLogger()); getLogger() .log( TreeLogger.DEBUG, "found " + annotationResolvers.size() + " additional AnnotationResolvers"); for (AnnotationResolver a : annotationResolvers) { getLogger() .log( TreeLogger.DEBUG, "(" + a.getClass().getName() + ") resolve `" + source.getName() + "#" + method.getName() + "´ ..."); final Map<String, String[]> addDataParams = a.resolveAnnotation(getLogger(), source, method, restMethod); if (addDataParams != null) { for (String s : addDataParams.keySet()) { final StringBuilder sb = new StringBuilder(); final List<String> classList = Arrays.asList(addDataParams.get(s)); sb.append("["); for (int i = 0; i < classList.size(); ++i) { sb.append("\\\"").append(classList.get(i)).append("\\\""); if ((i + 1) < classList.size()) { sb.append(","); } } sb.append("]"); getLogger() .log(TreeLogger.DEBUG, "add call with (\"" + s + "\", \"" + sb.toString() + "\")"); p("__method.addData(\"" + s + "\", \"" + sb.toString() + "\");"); } } } if (acceptTypeBuiltIn != null) { // TODO: shouldn't we also have a cach in here? p(returnRequest(returnRequest, isJsonp) + "__method.send(" + callbackArg.getName() + ");"); } else if (isJsonp) { p(returnRequest(returnRequest, isJsonp) + "((" + JSONP_METHOD_CLASS + ")__method).send(new " + ABSTRACT_ASYNC_CALLBACK_CLASS + "<" + resultType.getParameterizedQualifiedSourceName() + ">((" + JSONP_METHOD_CLASS + ")__method, " + callbackArg.getName() + ") {") .i(1); { p("protected " + resultType.getParameterizedQualifiedSourceName() + " parseResult(" + JSON_VALUE_CLASS + " result) throws Exception {") .i(1); { if (resultType.getParameterizedQualifiedSourceName().equals("java.lang.Void")) { p("return (java.lang.Void) null;"); } else { p("try {").i(1); { if (resultType.isAssignableTo(locator.LIST_TYPE)) { p("result = new " + JSON_ARRAY_CLASS + "(result.getJavaScriptObject());"); } jsonAnnotation = method.getAnnotation(Json.class); Style style = jsonAnnotation != null ? jsonAnnotation.style() : classStyle; p("return " + locator.decodeExpression(resultType, "result", style) + ";"); } i(-1).p("} catch (Throwable __e) {").i(1); { p( "throw new " + RESPONSE_FORMAT_EXCEPTION_CLASS + "(\"Response was NOT a valid JSON document\", __e);"); } i(-1).p("}"); } } i(-1).p("}"); } i(-1).p("});"); } else { p("try {").i(1); { p(returnRequest(returnRequest, isJsonp) + "__method.send(new " + ABSTRACT_REQUEST_CALLBACK_CLASS + "<" + resultType.getParameterizedQualifiedSourceName() + ">(__method, " + callbackArg.getName() + ") {") .i(1); { p("protected " + resultType.getParameterizedQualifiedSourceName() + " parseResult() throws Exception {") .i(1); { if (resultType.getParameterizedQualifiedSourceName().equals("java.lang.Void")) { p("return (java.lang.Void) null;"); } else { p("try {").i(1); { jsonAnnotation = method.getAnnotation(Json.class); Style style = jsonAnnotation != null ? jsonAnnotation.style() : classStyle; p( "return " + locator.decodeExpression( resultType, JSON_PARSER_CLASS + ".parse(__method.getResponse().getText())", style) + ";"); } i(-1).p("} catch (Throwable __e) {").i(1); { p( "throw new " + RESPONSE_FORMAT_EXCEPTION_CLASS + "(\"Response was NOT a valid JSON document\", __e);"); } i(-1).p("}"); } } i(-1).p("}"); } i(-1).p("});"); } i(-1).p("} catch (" + REQUEST_EXCEPTION_CLASS + " __e) {").i(1); { p(callbackArg.getName() + ".onFailure(__method,__e);"); if (returnRequest) { p("return null;"); } } i(-1).p("}"); } } i(-1).p("}"); }
private void writeMethodImpl(JMethod method) throws UnableToCompleteException { if (method.getReturnType().isPrimitive() != JPrimitiveType.VOID) { error( "Invalid rest method. Method must have void return type: " + method.getReadableDeclaration()); } Json jsonAnnotation = source.getAnnotation(Json.class); final Style classStyle = jsonAnnotation != null ? jsonAnnotation.style() : Style.DEFAULT; Options classOptions = source.getAnnotation(Options.class); Options options = method.getAnnotation(Options.class); p(method.getReadableDeclaration(false, false, false, false, true) + " {").i(1); { String restMethod = getRestMethod(method); LinkedList<JParameter> args = new LinkedList<JParameter>(Arrays.asList(method.getParameters())); // the last arg should be the callback. if (args.isEmpty()) { error( "Invalid rest method. Method must declare at least a callback argument: " + method.getReadableDeclaration()); } JParameter callbackArg = args.removeLast(); JClassType callbackType = callbackArg.getType().isClassOrInterface(); JClassType methodCallbackType = METHOD_CALLBACK_TYPE; if (callbackType == null || !callbackType.isAssignableTo(methodCallbackType)) { error( "Invalid rest method. Last argument must be a " + methodCallbackType.getName() + " type: " + method.getReadableDeclaration()); } JClassType resultType = getCallbackTypeGenericClass(callbackType); String pathExpression = null; Path pathAnnotation = method.getAnnotation(Path.class); if (pathAnnotation != null) { pathExpression = wrap(pathAnnotation.value()); } JParameter contentArg = null; HashMap<String, JParameter> queryParams = new HashMap<String, JParameter>(); HashMap<String, JParameter> headerParams = new HashMap<String, JParameter>(); for (JParameter arg : args) { PathParam paramPath = arg.getAnnotation(PathParam.class); if (paramPath != null) { if (pathExpression == null) { error( "Invalid rest method. Invalid @PathParam annotation. Method is missing the @Path annotation: " + method.getReadableDeclaration()); } pathExpression = pathExpression.replaceAll( Pattern.quote("{" + paramPath.value() + "}"), "\"+" + toStringExpression(arg) + "+\""); continue; } QueryParam queryParam = arg.getAnnotation(QueryParam.class); if (queryParam != null) { queryParams.put(queryParam.value(), arg); continue; } HeaderParam headerParam = arg.getAnnotation(HeaderParam.class); if (headerParam != null) { headerParams.put(headerParam.value(), arg); continue; } if (contentArg != null) { error( "Invalid rest method. Only one content parameter is supported: " + method.getReadableDeclaration()); } contentArg = arg; } String acceptTypeBuiltIn = null; if (callbackType.equals(TEXT_CALLBACK_TYPE)) { acceptTypeBuiltIn = "CONTENT_TYPE_TEXT"; } else if (callbackType.equals(JSON_CALLBACK_TYPE)) { acceptTypeBuiltIn = "CONTENT_TYPE_JSON"; } else if (callbackType.isAssignableTo(OVERLAY_CALLBACK_TYPE)) { acceptTypeBuiltIn = "CONTENT_TYPE_JSON"; } else if (callbackType.equals(XML_CALLBACK_TYPE)) { acceptTypeBuiltIn = "CONTENT_TYPE_XML"; } p("final " + METHOD_CLASS + " __method ="); p("this.resource"); if (pathExpression != null) { p(".resolve(" + pathExpression + ")"); } for (Map.Entry<String, JParameter> entry : queryParams.entrySet()) { String expr = entry.getValue().getName(); p( ".addQueryParam(" + wrap(entry.getKey()) + ", " + toStringExpression(entry.getValue().getType(), expr) + ")"); } // example: .get() p("." + restMethod + "();"); // Handle JSONP specific configuration... JSONP jsonpAnnotation = method.getAnnotation(JSONP.class); if (restMethod.equals(METHOD_JSONP) && jsonpAnnotation != null) { if (jsonpAnnotation.callbackParam().length() > 0) { p( "((" + JSONP_METHOD_CLASS + ")__method).callbackParam(" + wrap(jsonpAnnotation.callbackParam()) + ");"); } if (jsonpAnnotation.failureCallbackParam().length() > 0) { p( "((" + JSONP_METHOD_CLASS + ")__method).failureCallbackParam(" + wrap(jsonpAnnotation.failureCallbackParam()) + ");"); } } // configure the dispatcher if (options != null && options.dispatcher() != Dispatcher.class) { // use the dispatcher configured for the method. p("__method.setDispatcher(" + options.dispatcher().getName() + ".INSTANCE);"); } else { // use the default dispatcher configured for the service.. p("__method.setDispatcher(this.dispatcher);"); } // configure the expected statuses.. if (options != null && options.expect().length != 0) { // Using method level defined expected status p("__method.expect(" + join(options.expect(), ", ") + ");"); } else if (classOptions != null && classOptions.expect().length != 0) { // Using class level defined expected status p("__method.expect(" + join(classOptions.expect(), ", ") + ");"); } // configure the timeout if (options != null && options.timeout() >= 0) { // Using method level defined value p("__method.timeout(" + options.timeout() + ");"); } else if (classOptions != null && classOptions.timeout() >= 0) { // Using class level defined value p("__method.timeout(" + classOptions.timeout() + ");"); } Produces producesAnnotation = findAnnotationOnMethodOrEnclosingType(method, Produces.class); if (producesAnnotation != null) { p( "__method.header(" + RESOURCE_CLASS + ".HEADER_ACCEPT, " + wrap(producesAnnotation.value()[0]) + ");"); } else { // set the default accept header.... if (acceptTypeBuiltIn != null) { p( "__method.header(" + RESOURCE_CLASS + ".HEADER_ACCEPT, " + RESOURCE_CLASS + "." + acceptTypeBuiltIn + ");"); } else { p( "__method.header(" + RESOURCE_CLASS + ".HEADER_ACCEPT, " + RESOURCE_CLASS + ".CONTENT_TYPE_JSON);"); } } Consumes consumesAnnotation = findAnnotationOnMethodOrEnclosingType(method, Consumes.class); if (consumesAnnotation != null) { p( "__method.header(" + RESOURCE_CLASS + ".HEADER_CONTENT_TYPE, " + wrap(consumesAnnotation.value()[0]) + ");"); } // and set the explicit headers now (could override the accept header) for (Map.Entry<String, JParameter> entry : headerParams.entrySet()) { String expr = entry.getValue().getName(); p( "__method.header(" + wrap(entry.getKey()) + ", " + toStringExpression(entry.getValue().getType(), expr) + ");"); } if (contentArg != null) { if (contentArg.getType() == STRING_TYPE) { p("__method.text(" + contentArg.getName() + ");"); } else if (contentArg.getType() == JSON_VALUE_TYPE) { p("__method.json(" + contentArg.getName() + ");"); } else if (contentArg.getType().isClass() != null && isOverlayArrayType(contentArg.getType().isClass())) { p("__method.json(new " + JSON_ARRAY_CLASS + "(" + contentArg.getName() + "));"); } else if (contentArg.getType().isClass() != null && contentArg.getType().isClass().isAssignableTo(OVERLAY_VALUE_TYPE)) { p("__method.json(new " + JSON_OBJECT_CLASS + "(" + contentArg.getName() + "));"); } else if (contentArg.getType() == DOCUMENT_TYPE) { p("__method.xml(" + contentArg.getName() + ");"); } else { JClassType contentClass = contentArg.getType().isClass(); if (contentClass == null) { error("Content argument must be a class."); } jsonAnnotation = contentArg.getAnnotation(Json.class); Style style = jsonAnnotation != null ? jsonAnnotation.style() : classStyle; // example: // .json(Listings$_Generated_JsonEncoder_$.INSTANCE.encode(arg0) // ) p( "__method.json(" + locator.encodeExpression(contentClass, contentArg.getName(), style) + ");"); } } if (acceptTypeBuiltIn != null) { p("__method.send(" + callbackArg.getName() + ");"); } else { p("try {").i(1); { p("__method.send(new " + ABSTRACT_REQUEST_CALLBACK_CLASS + "<" + resultType.getParameterizedQualifiedSourceName() + ">(__method, " + callbackArg.getName() + ") {") .i(1); { p("protected " + resultType.getParameterizedQualifiedSourceName() + " parseResult() throws Exception {") .i(1); { if (resultType.getParameterizedQualifiedSourceName().equals("java.lang.Void")) { p("return (java.lang.Void) new java.lang.Object();"); } else { p("try {").i(1); { jsonAnnotation = method.getAnnotation(Json.class); Style style = jsonAnnotation != null ? jsonAnnotation.style() : classStyle; p( "return " + locator.decodeExpression( resultType, JSON_PARSER_CLASS + ".parse(__method.getResponse().getText())", style) + ";"); } i(-1).p("} catch (Throwable __e) {").i(1); { p( "throw new " + RESPONSE_FORMAT_EXCEPTION_CLASS + "(\"Response was NOT a valid JSON document\", __e);"); } i(-1).p("}"); } } i(-1).p("}"); } i(-1).p("});"); } i(-1).p("} catch (" + REQUEST_EXCEPTION_CLASS + " __e) {").i(1); { p(callbackArg.getName() + ".onFailure(__method,__e);"); } i(-1).p("}"); } } i(-1).p("}"); }