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; }
@Override public MetaField getDeclaredField(final String name) { JClassType type = getEnclosedMetaObject().isClassOrInterface(); if (type == null) { if ("length".equals(name) && getEnclosedMetaObject().isArray() != null) { return new MetaField.ArrayLengthMetaField(this); } return null; } JField field = type.findField(name); while (field == null && (type = type.getSuperclass()) != null && !type.getQualifiedSourceName().equals("java.lang.Object")) { JField superTypeField = type.findField(name); if (!superTypeField.isPrivate()) { field = superTypeField; } for (final JClassType interfaceType : type.getImplementedInterfaces()) { field = interfaceType.findField(name); } } if (field == null) { throw new RuntimeException("no such field: " + name + " in class: " + this); } return new GWTField(oracle, field); }
/** * @param srcWriter * @param type * @param parentVariable * @param added * @param iocContainerVariable * @param configurations */ private static void injectFields( SourcePrinter srcWriter, JClassType type, String parentVariable, Set<String> added, String iocContainerVariable, Map<String, IocConfig<?>> configurations) { for (JField field : type.getFields()) { String fieldName = field.getName(); if (!added.contains(fieldName)) { added.add(fieldName); JType fieldType = field.getType(); if ((fieldType.isPrimitive() == null)) { String injectionExpression = getFieldInjectionExpression(field, iocContainerVariable, configurations); if (injectionExpression != null) { if (JClassUtils.isPropertyVisibleToWrite(type, field, false)) { if (JClassUtils.hasSetMethod(field, type)) { String setterMethodName = "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); JMethod method = type.findMethod(setterMethodName, new JType[] {field.getType()}); if (method.getAnnotation(Inject.class) == null) // Annotated methods are handled apart { srcWriter.println( fieldType.getQualifiedSourceName() + " field_" + fieldName + " = " + injectionExpression + ";"); srcWriter.println( parentVariable + "." + setterMethodName + "(field_" + fieldName + ");"); } } else { srcWriter.println( parentVariable + "." + fieldName + " = " + injectionExpression + ";"); } } else { throw new IoCException( "IoC Error Field [" + field.getName() + "] from class [" + type.getQualifiedSourceName() + "] is not a writeable property."); } } } } } }
/** * @param dataClass * @return */ private String[] extractIdentifiers(JClassType dataClass) { List<String> ids = new ArrayList<String>(); JField[] fields = JClassUtils.getDeclaredFields(dataClass); for (JField field : fields) { if (field.getAnnotation(DataObjectIdentifier.class) != null) { if (field.isPublic()) { ids.add(field.getName()); } else { ids.add(JClassUtils.getGetterMethod(field.getName(), dataClass) + "()"); } } } return ids.toArray(new String[ids.size()]); }
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)); }
@SuppressWarnings("deprecation") private static String getFieldInjectionExpression( JField field, String iocContainerVariable, Map<String, IocConfig<?>> configurations) { Inject inject = field.getAnnotation(Inject.class); if (inject != null) { JType fieldType = field.getType(); if (!field.isStatic()) { if (fieldType.isClassOrInterface() != null) { String fieldTypeName = fieldType.getQualifiedSourceName(); IocConfigImpl<?> iocConfig = (IocConfigImpl<?>) configurations.get(fieldTypeName); if (iocConfig != null) { if (inject .scope() .equals(org.cruxframework.crux.core.client.ioc.Inject.Scope.DEFAULT)) { return iocContainerVariable + ".get" + fieldTypeName.replace('.', '_') + "(" + Scope.class.getCanonicalName() + "." + iocConfig.getScope().name() + ", null)"; } return iocContainerVariable + ".get" + fieldTypeName.replace('.', '_') + "(" + Scope.class.getCanonicalName() + "." + getScopeName(inject.scope()) + ", " + EscapeUtils.quote(inject.subscope()) + ")"; } else { return "GWT.create(" + fieldTypeName + ".class)"; } } else { throw new IoCException( "Error injecting field [" + field.getName() + "] from type [" + field.getEnclosingType().getQualifiedSourceName() + "]. Primitive fields can not be handled by ioc container."); } } else { throw new IoCException( "Error injecting field [" + field.getName() + "] from type [" + field.getEnclosingType().getQualifiedSourceName() + "]. Static fields can not be handled by ioc container."); } } return null; }
protected String writeSerializationPolicyFile( TreeLogger logger, GeneratorContextExt ctx, SerializableTypeOracle serializationSto, SerializableTypeOracle deserializationSto) throws UnableToCompleteException { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStreamWriter osw = new OutputStreamWriter( baos, SerializationPolicyLoader.SERIALIZATION_POLICY_FILE_ENCODING); TypeOracle oracle = ctx.getTypeOracle(); PrintWriter pw = new PrintWriter(osw); JType[] serializableTypes = unionOfTypeArrays( serializationSto.getSerializableTypes(), deserializationSto.getSerializableTypes(), new JType[] {serviceIntf}); for (int i = 0; i < serializableTypes.length; ++i) { JType type = serializableTypes[i]; String binaryTypeName = SerializationUtils.getRpcTypeName(type); pw.print(binaryTypeName); pw.print(", " + Boolean.toString(deserializationSto.isSerializable(type))); pw.print(", " + Boolean.toString(deserializationSto.maybeInstantiated(type))); pw.print(", " + Boolean.toString(serializationSto.isSerializable(type))); pw.print(", " + Boolean.toString(serializationSto.maybeInstantiated(type))); pw.print(", " + typeStrings.get(type)); /* * Include the serialization signature to bump the RPC file name if * obfuscated identifiers are used. */ pw.print(", " + SerializationUtils.getSerializationSignature(oracle, type)); pw.print('\n'); /* * Emit client-side field information for classes that may be enhanced * on the server. Each line consists of a comma-separated list * containing the keyword '@ClientFields', the class name, and a list of * all potentially serializable client-visible fields. */ if ((type instanceof JClassType) && ((JClassType) type).isEnhanced()) { JField[] fields = ((JClassType) type).getFields(); JField[] rpcFields = new JField[fields.length]; int numRpcFields = 0; for (JField f : fields) { if (f.isTransient() || f.isStatic() || f.isFinal()) { continue; } rpcFields[numRpcFields++] = f; } pw.print(SerializationPolicyLoader.CLIENT_FIELDS_KEYWORD); pw.print(','); pw.print(binaryTypeName); for (int idx = 0; idx < numRpcFields; idx++) { pw.print(','); pw.print(rpcFields[idx].getName()); } pw.print('\n'); } } // Closes the wrapped streams. pw.close(); byte[] serializationPolicyFileContents = baos.toByteArray(); String serializationPolicyName = Util.computeStrongName(serializationPolicyFileContents); String serializationPolicyFileName = SerializationPolicyLoader.getSerializationPolicyFileName(serializationPolicyName); OutputStream os = ctx.tryCreateResource(logger, serializationPolicyFileName); if (os != null) { os.write(serializationPolicyFileContents); GeneratedResource resource = ctx.commitResource(logger, os); /* * Record which proxy class created the resource. A manifest will be * emitted by the RpcPolicyManifestLinker. */ emitPolicyFileArtifact(logger, ctx, resource.getPartialPath()); } else { if (logger.isLoggable(TreeLogger.TRACE)) { logger.log( TreeLogger.TRACE, "SerializationPolicy file for RemoteService '" + serviceIntf.getQualifiedSourceName() + "' already exists; no need to rewrite it.", null); } } return serializationPolicyName; } catch (UnsupportedEncodingException e) { logger.log( TreeLogger.ERROR, SerializationPolicyLoader.SERIALIZATION_POLICY_FILE_ENCODING + " is not supported", e); throw new UnableToCompleteException(); } catch (IOException e) { logger.log(TreeLogger.ERROR, null, e); throw new UnableToCompleteException(); } }