List of usage examples for com.google.gwt.core.ext.typeinfo JField isFinal
boolean isFinal();
From source file:ch.unifr.pai.twice.module.rebind.TWICEModuleGenerator.java
License:Apache License
@Override public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { // Build a new class, that implements a "paintScreen" method JClassType classType;/*from w w w . j a v a 2 s. c o m*/ try { classType = context.getTypeOracle().getType(typeName); JClassType genericClass = getGenericClass(classType); SourceWriter src = getSourceWriter(classType, context, logger); if (src != null) { src.println("@Override"); src.println("public " + Map.class.getName() + "<" + String.class.getName() + ", " + Object.class.getName() + "> getConfigurableFields(" + genericClass.getQualifiedSourceName() + " instance){"); src.println(Map.class.getName() + "<" + String.class.getName() + ", " + Object.class.getName() + "> result = new " + HashMap.class.getName() + "<" + String.class.getName() + ", " + Object.class.getName() + ">();"); for (JField f : genericClass.getFields()) { Configurable c = f.getAnnotation(Configurable.class); if (c != null && !f.isFinal() && !f.isPrivate() && !f.isProtected()) { src.println("result.put(\"" + c.value() + "\", instance." + f.getName() + ");"); } } src.println("return result;"); src.println("}"); src.println("@Override"); src.println("public void configure(" + Map.class.getName() + "<" + String.class.getName() + ", " + String.class.getName() + "> properties, " + genericClass.getQualifiedSourceName() + " instance){"); src.println("for(" + String.class.getName() + " key : properties.keySet()){"); src.println("String value = properties.get(key);"); src.println("if(key==null){"); src.println("}"); for (JField f : genericClass.getFields()) { Configurable c = f.getAnnotation(Configurable.class); if (c != null && !f.isFinal() && !f.isPrivate() && !f.isProtected()) { JPrimitiveType t = f.getType().isPrimitive(); if (t != null) { src.println("else if(key.equals(\"" + c.value() + "\")){"); switch (t) { case INT: src.println("instance." + f.getName() + "=" + Integer.class.getName() + ".parseInt(value);"); break; case BOOLEAN: src.println("instance." + f.getName() + "=" + Boolean.class.getName() + ".parseBoolean(value);"); break; case DOUBLE: src.println("instance." + f.getName() + "=" + Double.class.getName() + ".parseDouble(value);"); break; case FLOAT: src.println("instance." + f.getName() + "=" + Float.class.getName() + ".parseFloat(value);"); break; case LONG: src.println("instance." + f.getName() + "=" + Long.class.getName() + ".parseLong(value);"); break; default: throw new RuntimeException("The primitive type \"" + t.name() + "\" is not supported for configuration"); } } else if (f.getType().getQualifiedSourceName().equals(String.class.getName())) { src.println("instance." + f.getName() + "=value"); } else { throw new RuntimeException("The type \"" + f.getType().getQualifiedSourceName() + "\" is not supported for configuration"); } src.println("}"); } } src.println("}"); src.println("}"); src.println("@Override"); src.println("public " + RunAsyncCallback.class.getName() + " instantiate(final " + AsyncCallback.class.getName() + "<" + genericClass.getQualifiedSourceName() + "> callback){"); src.println("return new " + RunAsyncCallback.class.getName() + "(){"); src.println("@Override"); src.println("public void onSuccess(){"); src.println(genericClass.getQualifiedSourceName() + " module = " + GWT.class.getName() + ".create(" + genericClass.getQualifiedSourceName() + ".class);"); src.println("//start(module);"); src.println("callback.onSuccess(module);"); src.println("}"); src.println("@Override"); src.println("public void onFailure(" + Throwable.class.getName() + " reason){"); src.println("callback.onFailure(reason);"); src.println("}"); src.println("};"); src.println("}"); src.commit(logger); } return typeName + "Impl"; } catch (NotFoundException e) { e.printStackTrace(); } return null; }
From source file:com.artemis.gwtref.gen.ReflectionCacheSourceCreator.java
License:Apache License
private String createTypeGenerator(JType t) { buffer.setLength(0);// www. java 2s . c o m String varName = "t"; if (t instanceof JPrimitiveType) varName = "p"; int id = nextId(); typeNames2typeIds.put(t.getErasedType().getQualifiedSourceName(), id); pb("Type " + varName + " = new Type();"); pb(varName + ".name = \"" + t.getErasedType().getQualifiedSourceName() + "\";"); pb(varName + ".id = " + id + ";"); pb(varName + ".clazz = " + t.getErasedType().getQualifiedSourceName() + ".class;"); if (t instanceof JClassType) { JClassType c = (JClassType) t; if (isVisible(c.getSuperclass())) pb(varName + ".superClass = " + c.getSuperclass().getErasedType().getQualifiedSourceName() + ".class;"); if (c.getFlattenedSupertypeHierarchy() != null) { pb("Set<Class> " + varName + "Assignables = new HashSet<Class>();"); for (JType i : c.getFlattenedSupertypeHierarchy()) { if (!isVisible(i)) continue; pb(varName + "Assignables.add(" + i.getErasedType().getQualifiedSourceName() + ".class);"); } pb(varName + ".assignables = " + varName + "Assignables;"); } if (c.isInterface() != null) pb(varName + ".isInterface = true;"); if (c.isEnum() != null) pb(varName + ".isEnum = true;"); if (c.isArray() != null) pb(varName + ".isArray = true;"); if (c.isMemberType()) pb(varName + ".isMemberClass = true;"); pb(varName + ".isStatic = " + c.isStatic() + ";"); pb(varName + ".isAbstract = " + c.isAbstract() + ";"); if (c.getFields() != null) { pb(varName + ".fields = new Field[] {"); for (JField f : c.getFields()) { String enclosingType = getType(c); String fieldType = getType(f.getType()); int setter = nextId(); int getter = nextId(); String elementType = getElementTypes(f); String annotations = getAnnotations(f.getDeclaredAnnotations()); pb("new Field(\"" + f.getName() + "\", " + enclosingType + ", " + fieldType + ", " + f.isFinal() + ", " + f.isDefaultAccess() + ", " + f.isPrivate() + ", " + f.isProtected() + ", " + f.isPublic() + ", " + f.isStatic() + ", " + f.isTransient() + ", " + f.isVolatile() + ", " + getter + ", " + setter + ", " + elementType + ", " + annotations + "), "); SetterGetterStub stub = new SetterGetterStub(); stub.name = f.getName(); stub.enclosingType = enclosingType; stub.type = fieldType; stub.isStatic = f.isStatic(); stub.isFinal = f.isFinal(); if (enclosingType != null && fieldType != null) { stub.getter = getter; stub.setter = setter; } setterGetterStubs.add(stub); } pb("};"); } printMethods(c, varName, "Method", c.getMethods()); if (c.isPublic() && !c.isAbstract() && (c.getEnclosingType() == null || c.isStatic())) { printMethods(c, varName, "Constructor", c.getConstructors()); } else { logger.log(Type.INFO, c.getName() + " can't be instantiated. Constructors not generated"); } if (c.isArray() != null) { pb(varName + ".componentType = " + getType(c.isArray().getComponentType()) + ";"); } if (c.isEnum() != null) { JEnumConstant[] enumConstants = c.isEnum().getEnumConstants(); if (enumConstants != null) { pb(varName + ".enumConstants = new Object[" + enumConstants.length + "];"); for (int i = 0; i < enumConstants.length; i++) { pb(varName + ".enumConstants[" + i + "] = " + c.getErasedType().getQualifiedSourceName() + "." + enumConstants[i].getName() + ";"); } } } Annotation[] annotations = c.getDeclaredAnnotations(); if (annotations != null && annotations.length > 0) { pb(varName + ".annotations = " + getAnnotations(annotations) + ";"); } } else if (t.isAnnotation() != null) { pb(varName + ".isAnnotation = true;"); } else { pb(varName + ".isPrimitive = true;"); } pb("types.put(\"" + t.getErasedType().getQualifiedSourceName() + "\", " + varName + ");"); return buffer.toString(); }
From source file:com.badlogic.gwtref.gen.ReflectionCacheSourceCreator.java
License:Apache License
private String createTypeGenerator(JType t) { buffer.setLength(0);/*from w w w.ja va2 s . co m*/ int id = nextTypeId++; typeNames2typeIds.put(t.getErasedType().getQualifiedSourceName(), id); JClassType c = t.isClass(); String name = t.getErasedType().getQualifiedSourceName(); String superClass = null; if (c != null && (isVisible(c.getSuperclass()))) superClass = c.getSuperclass().getErasedType().getQualifiedSourceName() + ".class"; String assignables = null; String interfaces = null; if (c != null && c.getFlattenedSupertypeHierarchy() != null) { assignables = "new HashSet<Class>(Arrays.asList("; boolean used = false; for (JType i : c.getFlattenedSupertypeHierarchy()) { if (!isVisible(i) || i.equals(t) || "java.lang.Object".equals(i.getErasedType().getQualifiedSourceName())) continue; if (used) assignables += ", "; assignables += i.getErasedType().getQualifiedSourceName() + ".class"; used = true; } if (used) assignables += "))"; else assignables = null; } if (c == null) { // if it's not a class, it may be an interface instead c = t.isInterface(); } if (c != null && c.getImplementedInterfaces() != null) { interfaces = "new HashSet<Class>(Arrays.asList("; boolean used = false; for (JType i : c.getImplementedInterfaces()) { if (!isVisible(i) || i.equals(t)) continue; if (used) interfaces += ", "; interfaces += i.getErasedType().getQualifiedSourceName() + ".class"; used = true; } if (used) interfaces += "))"; else interfaces = null; } String varName = "c" + id; pb("private static Type " + varName + ";"); pb("private static Type " + varName + "() {"); pb("if(" + varName + "!=null) return " + varName + ";"); pb(varName + " = new Type(\"" + name + "\", " + id + ", " + name + ".class, " + superClass + ", " + assignables + ", " + interfaces + ");"); if (c != null) { if (c.isEnum() != null) pb(varName + ".isEnum = true;"); if (c.isArray() != null) pb(varName + ".isArray = true;"); if (c.isMemberType()) pb(varName + ".isMemberClass = true;"); if (c.isInterface() != null) { pb(varName + ".isInterface = true;"); } else { pb(varName + ".isStatic = " + c.isStatic() + ";"); pb(varName + ".isAbstract = " + c.isAbstract() + ";"); } if (c.getFields() != null && c.getFields().length > 0) { pb(varName + ".fields = new Field[] {"); for (JField f : c.getFields()) { String enclosingType = getType(c); String fieldType = getType(f.getType()); int setterGetter = nextSetterGetterId++; String elementType = getElementTypes(f); String annotations = getAnnotations(f.getDeclaredAnnotations()); pb(" new Field(\"" + f.getName() + "\", " + enclosingType + ", " + fieldType + ", " + f.isFinal() + ", " + f.isDefaultAccess() + ", " + f.isPrivate() + ", " + f.isProtected() + ", " + f.isPublic() + ", " + f.isStatic() + ", " + f.isTransient() + ", " + f.isVolatile() + ", " + setterGetter + ", " + setterGetter + ", " + elementType + ", " + annotations + "), "); SetterGetterStub stub = new SetterGetterStub(); stub.name = f.getName(); stub.enclosingType = enclosingType; stub.type = fieldType; stub.isStatic = f.isStatic(); stub.isFinal = f.isFinal(); if (enclosingType != null && fieldType != null) { stub.getter = setterGetter; stub.setter = setterGetter; } setterGetterStubs.add(stub); } pb("};"); } createTypeInvokables(c, varName, "Method", c.getMethods()); if (c.isPublic() && !c.isAbstract() && (c.getEnclosingType() == null || c.isStatic())) { createTypeInvokables(c, varName, "Constructor", c.getConstructors()); } else { logger.log(Type.INFO, c.getName() + " can't be instantiated. Constructors not generated"); } if (c.isArray() != null) { pb(varName + ".componentType = " + getType(c.isArray().getComponentType()) + ";"); } if (c.isEnum() != null) { JEnumConstant[] enumConstants = c.isEnum().getEnumConstants(); if (enumConstants != null) { pb(varName + ".enumConstants = new Object[" + enumConstants.length + "];"); for (int i = 0; i < enumConstants.length; i++) { pb(varName + ".enumConstants[" + i + "] = " + c.getErasedType().getQualifiedSourceName() + "." + enumConstants[i].getName() + ";"); } } } Annotation[] annotations = c.getDeclaredAnnotations(); if (annotations != null && annotations.length > 0) { pb(varName + ".annotations = " + getAnnotations(annotations) + ";"); } } else if (t.isAnnotation() != null) { pb(varName + ".isAnnotation = true;"); } else { pb(varName + ".isPrimitive = true;"); } pb("return " + varName + ";"); pb("}"); return buffer.toString(); }
From source file:com.guit.rebind.jsorm.JsonSerializerUtil.java
License:Apache License
private static void printJsniGettersAndSetters(SourceWriter writer, JClassType pojoType) { for (JField f : pojoType.getFields()) { // Non static, final or transient if (f.isStatic() || f.isFinal() || f.isTransient()) { continue; }//from w w w .j a v a 2s.com String fieldName = f.getName(); String getterName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); JType fieldType = f.getType(); // Print getters and setters String fieldTypeQualifiedType = fieldType.getQualifiedSourceName(); String pojoQualifiedName = pojoType.getQualifiedSourceName(); writer.println("private static native " + fieldTypeQualifiedType + " get" + getterName + "(" + pojoQualifiedName + " instance) /*-{\n" + " return instance.@" + pojoQualifiedName + "::" + fieldName + ";\n" + " }-*/;\n" + " \n" + " private static native void set" + getterName + "(" + pojoQualifiedName + " instance, " + fieldTypeQualifiedType + " value) /*-{\n" + " instance.@" + pojoQualifiedName + "::" + fieldName + " = value;\n" + " }-*/;"); } JClassType superclass = pojoType.getSuperclass(); if (superclass != null && !superclass.getQualifiedSourceName().equals(Object.class.getCanonicalName())) { printJsniGettersAndSetters(writer, superclass); } }
From source file:com.guit.rebind.jsorm.JsonSerializerUtil.java
License:Apache License
private static void getFields(List<JField> fields, JClassType pojoType) { for (JField f : pojoType.getFields()) { // Non static, final or transient if (f.isStatic() || f.isFinal() || f.isTransient()) { continue; }//from ww w . j a v a2 s .c o m fields.add(f); } JClassType superclass = pojoType.getSuperclass(); if (superclass != null && !superclass.getQualifiedSourceName().equals(Object.class.getCanonicalName())) { getFields(fields, superclass); } }
From source file:com.gwtplatform.dispatch.rebind.RestActionGenerator.java
License:Apache License
private void generateChildSerializersForType(JClassType type) throws Exception { JField[] fields = type.getFields();/*from w w w.ja v a2 s. co m*/ for (JField field : fields) { if (!field.isFinal()) { JType fieldType = field.getType(); if (fieldType.isParameterized() != null) { generateParametersSerializers(fieldType.isParameterized()); } else if (field.getType().isPrimitive() == null) { generateChildSerializer(field.getType().isClassOrInterface()); } } } }
From source file:com.hiramchirino.restygwt.rebind.JsonEncoderDecoderClassCreator.java
License:Apache License
public void generate() throws UnableToCompleteException { locator = new JsonEncoderDecoderInstanceLocator(context, logger); JClassType soruceClazz = source.isClass(); if (soruceClazz == null) { error("Type is not a class"); }//from w ww. j a v a2s . c o m if (!soruceClazz.isDefaultInstantiable()) { error("No default constuctor"); } Json jsonAnnotation = source.getAnnotation(Json.class); final Style classStyle = jsonAnnotation != null ? jsonAnnotation.style() : Style.DEFAULT; p(); p("public static final " + shortName + " INSTANCE = new " + shortName + "();"); p(); p("public " + JSON_VALUE_CLASS + " encode(" + source.getParameterizedQualifiedSourceName() + " value) {") .i(1); { p(JSON_OBJECT_CLASS + " rc = new " + JSON_OBJECT_CLASS + "();"); for (final JField field : getFields(source)) { final String getterName = getGetterName(field); // If can ignore some fields right off the back.. if (getterName == null && (field.isStatic() || field.isFinal() || field.isTransient())) { continue; } branch("Processing field: " + field.getName(), new Branch<Void>() { public Void execute() throws UnableToCompleteException { // TODO: try to get the field with a setter or JSNI if (getterName != null || field.isDefaultAccess() || field.isProtected() || field.isPublic()) { String name = field.getName(); String fieldExpr = "value." + name; if (getterName != null) { fieldExpr = "value." + getterName + "()"; } Json jsonAnnotation = field.getAnnotation(Json.class); Style style = jsonAnnotation != null ? jsonAnnotation.style() : classStyle; String expression = locator.encodeExpression(field.getType(), fieldExpr, style); p("{").i(1); { p(JSON_VALUE_CLASS + " v=" + expression + ";"); p("if( v!=null ) {").i(1); { if (field.isAnnotationPresent(ExcludeNull.class)) p("if (v != " + JSONNull.class.getCanonicalName() + ".getInstance())"); p("rc.put(" + wrap(name) + ", v);"); } i(-1).p("}"); } i(-1).p("}"); } else { error("field must not be private: " + field.getEnclosingType().getQualifiedSourceName() + "." + field.getName()); } return null; } }); } p("return rc;"); } i(-1).p("}"); p(); p("public " + source.getName() + " decode(" + JSON_VALUE_CLASS + " value) {").i(1); { p(JSON_OBJECT_CLASS + " object = toObject(value);"); p("" + source.getParameterizedQualifiedSourceName() + " rc = new " + source.getParameterizedQualifiedSourceName() + "();"); for (final JField field : getFields(source)) { final String setterName = getSetterName(field); // If can ignore some fields right off the back.. if (setterName == null && (field.isStatic() || field.isFinal() || field.isTransient())) { continue; } branch("Processing field: " + field.getName(), new Branch<Void>() { public Void execute() throws UnableToCompleteException { // TODO: try to set the field with a setter or JSNI if (setterName != null || field.isDefaultAccess() || field.isProtected() || field.isPublic()) { Json jsonAnnotation = field.getAnnotation(Json.class); Style style = jsonAnnotation != null ? jsonAnnotation.style() : classStyle; String name = field.getName(); String expression = locator.decodeExpression(field.getType(), "object.get(" + wrap(name) + ")", style); if (setterName != null) { p("rc." + setterName + "(" + expression + ");"); } else { p("rc." + name + "=" + expression + ";"); } } else { error("field must not be private."); } return null; } }); } p("return rc;"); } i(-1).p("}"); p(); }
From source file:com.totsp.gwt.freezedry.rebind.SerializableTypeOracleBuilder.java
License:Apache License
private void checkFields(TreeLogger logger, JClassType classOrInterface) { TreeLogger localLogger = logger;/*from w w w . j a v a2 s .co m*/ JField[] fields = classOrInterface.getFields(); if (fields.length > 0) { localLogger = localLogger.branch(TreeLogger.DEBUG, "Analyzing Fields:", null); contexts.push(classOrInterface); for (int i = 0; i < fields.length; ++i) { JField field = fields[i]; if (field.isStatic() || field.isTransient()) { continue; } if (field.isFinal()) { if (!suppressNonStaticFinalFieldWarnings) { localLogger.branch(TreeLogger.WARN, "Field '" + field.toString() + "' will not be serialized because it is final", null); } continue; } TreeLogger fieldLogger = localLogger.branch(TreeLogger.DEBUG, field.toString(), null); JType fieldType = field.getType(); checkForUnparameterizedType(fieldLogger, fieldType); checkType(fieldLogger, fieldType, true); } contexts.pop(); } else { localLogger.branch(TreeLogger.DEBUG, "No fields to analyze", null); } }
From source file:com.totsp.gwt.freezedry.rebind.SerializableTypeOracleImpl.java
License:Apache License
/** * Returns the fields which qualify for serialization. *///from w ww . ja v a 2 s . c o m public JField[] getSerializableFields(JClassType classType) { List fields = new ArrayList(); JField[] declFields = classType.getFields(); for (int iField = 0; iField < declFields.length; ++iField) { JField field = declFields[iField]; // TODO(mmendez): this is shared with the serializable type oracle // builder, join with that if (field.isStatic() || field.isTransient() || field.isFinal()) { continue; } fields.add(field); } Collections.sort(fields, FIELD_COMPARATOR); return (JField[]) fields.toArray(new JField[fields.size()]); }
From source file:de.csenk.gwt.ws.rebind.filter.serialization.GWTSerializerGenerator.java
License:Open Source License
/** * @param serializerLogger//from w w w .ja v a 2 s .c o m * @param context * @param typesSentFromBrowser * @param typesSentToBrowser * @param typeStrings * @return */ private String writeSerializationPolicyFile(TreeLogger logger, GeneratorContext ctx, SerializableTypeOracle serializationSto, SerializableTypeOracle deserializationSto, Map<JType, String> typeStrings, JClassType serializerInterface) 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[] { serializerInterface }); for (int i = 0; i < serializableTypes.length; ++i) { JType type = serializableTypes[i]; String binaryTypeName = TypeOracleMediator.computeBinaryClassName(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. */ ctx.commitArtifact(logger, new RpcPolicyFileArtifact(serializerInterface.getQualifiedSourceName(), resource)); } else { logger.log(TreeLogger.TRACE, "SerializationPolicy file for RemoteService '" + serializerInterface.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(); } }