protected void generateProxyMethods( SourceWriter w, SerializableTypeOracle serializableTypeOracle, TypeOracle typeOracle, Map<JMethod, JMethod> syncMethToAsyncMethMap) { JMethod[] syncMethods = serviceIntf.getOverridableMethods(); for (JMethod syncMethod : syncMethods) { JMethod asyncMethod = syncMethToAsyncMethMap.get(syncMethod); assert (asyncMethod != null); JClassType enclosingType = syncMethod.getEnclosingType(); JParameterizedType isParameterizedType = enclosingType.isParameterized(); if (isParameterizedType != null) { JMethod[] methods = isParameterizedType.getMethods(); for (int i = 0; i < methods.length; ++i) { if (methods[i] == syncMethod) { /* * Use the generic version of the method to ensure that the server * can find the method using the erasure of the generic signature. */ syncMethod = isParameterizedType.getBaseType().getMethods()[i]; } } } generateProxyMethod(w, serializableTypeOracle, typeOracle, syncMethod, asyncMethod); } }
private static void genericTypeToObject(JClassType type, StringBuilder sb) { sb.append("{\\\"name\\\":\\\"").append(type.getQualifiedSourceName() + "\\\""); sb.append(", \\\"types\\\":"); if (type.isParameterized() != null) { sb.append("["); JClassType[] inTypes = type.isParameterized().getTypeArgs(); for (int i = 0; i < inTypes.length; i++) { genericTypeToObject(inTypes[i], sb); if (i < inTypes.length - 1) { sb.append(", "); } } sb.append("]"); } else { sb.append("null"); } sb.append("}"); }
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 emitBody(SourceWriter w) throws NotFoundException { JClassType baseClass = context_.getTypeOracle().getType("org.rstudio.core.client.js.JsObjectInjector"); JClassType c = baseType_.asParameterizationOf(baseClass.isGenericType()); JType typeToInject = c.isParameterized().getTypeArgs()[0]; w.print("public native final void injectObject("); w.print(typeToInject.getQualifiedSourceName()); w.println(" value) /*-{"); w.indent(); w.println(baseExpression_ + " = {"); w.indent(); JMethod[] methods = typeToInject.isClassOrInterface().getMethods(); for (int i = 0; i < methods.length; i++) { JMethod method = methods[i]; final JParameter[] jParameters = method.getParameters(); StringBuilder argString = new StringBuilder(); for (int j = 0; j < jParameters.length; j++) { argString.append("_").append(j); if (j < jParameters.length - 1) argString.append(", "); } w.println(method.getName() + ": function(" + argString + ") {"); w.indent(); if (!method.getReturnType().getQualifiedSourceName().equals("void")) w.print("return "); w.print("value.@"); w.print(typeToInject.getQualifiedSourceName()); w.print("::"); w.print(method.getName()); w.print("("); for (JParameter param : jParameters) w.print(param.getType().getJNISignature()); w.print(")("); w.print(argString.toString()); w.println(");"); w.outdent(); w.print("}"); w.println((i < methods.length - 1) ? "," : ""); } w.outdent(); w.println("};"); w.outdent(); w.println("}-*/;"); }
public JParameterizedType asParameterizationOf(JGenericType type) { Set<JClassType> supertypes = getFlattenedSuperTypeHierarchy(this); for (JClassType supertype : supertypes) { JParameterizedType isParameterized = supertype.isParameterized(); if (isParameterized != null && isParameterized.getBaseType() == type) { return isParameterized; } JRawType isRaw = supertype.isRawType(); if (isRaw != null && isRaw.getBaseType() == type) { return isRaw.asParameterizedByWildcards(); } } return null; }
public String init( TypeOracle oracle, JField targetWidgetField, JType targetType, JField targetEntityMember, JField targetEntityField, String variable, List<JField> fields) { JClassType widgetCollectionType = targetWidgetField.getType().isClassOrInterface(); JClassType entityCollectionType = targetEntityMember.getType().isClassOrInterface(); JParameterizedType paramaterizedType = widgetCollectionType.isParameterized(); if (paramaterizedType == null) { throw new RuntimeException( "cannot generateGetField mappers for collection of widgets (the collection is not properly parameterized: eg. List<Widget>)"); } JClassType widgetType = paramaterizedType.getTypeArgs()[0]; varName = targetEntityField.getType().isClassOrInterface().getName() + targetWidgetField.getName() + "Mapper"; if (compiledTemplate == null) { InputStream istream = this.getClass().getResourceAsStream("CollectionFMGenerator.mv"); compiledTemplate = TemplateCompiler.compileTemplate(istream, null); } Map vars = new HashMap(); vars.put("typeOracle", oracle); vars.put("targetWidgetField", targetWidgetField); vars.put("targetEntityField", targetEntityField); vars.put("targetEntityMember", targetEntityMember); vars.put("widgetType", widgetType); vars.put("entityCollectionType", entityCollectionType); vars.put("widgetCollectionType", widgetCollectionType); vars.put("varName", varName); return String.valueOf(TemplateRuntime.execute(compiledTemplate, vars)); }
/** Examines a type to see if it can be transported. */ private boolean validateTransportableType( RequestMethod.Builder methodBuilder, JType type, boolean requireObject) throws UnableToCompleteException { JClassType transportedClass = type.isClassOrInterface(); if (transportedClass == null) { if (requireObject) { poison( "The type %s cannot be transported by RequestFactory as" + " a return type", type.getQualifiedSourceName()); return false; } else { // Primitives always ok return true; } } if (ModelUtils.isValueType(oracle, transportedClass) || splittableType.equals(transportedClass)) { // Simple values, like Integer and String methodBuilder.setValueType(true); } else if (entityProxyInterface.isAssignableFrom(transportedClass) || valueProxyInterface.isAssignableFrom(transportedClass)) { // EntityProxy and ValueProxy return types methodBuilder.setEntityType(getEntityProxyType(transportedClass)); } else if (collectionInterface.isAssignableFrom(transportedClass)) { // Only allow certain collections for now JParameterizedType parameterized = transportedClass.isParameterized(); if (parameterized == null) { poison("Requests that return collections of List or Set must be parameterized"); return false; } if (listInterface.equals(parameterized.getBaseType())) { methodBuilder.setCollectionType(CollectionType.LIST); } else if (setInterface.equals(parameterized.getBaseType())) { methodBuilder.setCollectionType(CollectionType.SET); } else { poison( "Requests that return collections may be declared with" + " %s or %s only", listInterface.getQualifiedSourceName(), setInterface.getQualifiedSourceName()); return false; } // Also record the element type in the method builder JClassType elementType = ModelUtils.findParameterizationOf(collectionInterface, transportedClass)[0]; methodBuilder.setCollectionElementType(elementType); validateTransportableType(methodBuilder, elementType, requireObject); } else if (mapInterface.isAssignableFrom(transportedClass)) { JParameterizedType parameterized = transportedClass.isParameterized(); if (parameterized == null) { poison("Requests that return Maps must be parameterized"); return false; } if (mapInterface.equals(parameterized.getBaseType())) { methodBuilder.setCollectionType(CollectionType.MAP); } else { poison( "Requests that return maps may be declared with" + " %s only", mapInterface.getQualifiedSourceName()); return false; } // Also record the element type in the method builder JClassType[] params = ModelUtils.findParameterizationOf(mapInterface, transportedClass); JClassType keyType = params[0]; JClassType valueType = params[1]; methodBuilder.setMapKeyType(keyType); methodBuilder.setMapValueType(valueType); validateTransportableType(methodBuilder, keyType, requireObject); validateTransportableType(methodBuilder, valueType, requireObject); } else { // Unknown type, fail poison("Invalid Request parameterization %s", transportedClass.getQualifiedSourceName()); return false; } methodBuilder.setDataType(transportedClass); return true; }