List of usage examples for com.google.gwt.core.ext.typeinfo JClassType getFields
JField[] getFields();
From source file:ch.unifr.pai.twice.comm.serverPush.rebind.RemoteEventSerializerGenerator.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; try {//w ww . j a v a 2 s. co m classType = context.getTypeOracle().getType(typeName); JClassType superClass = classType.getSuperclass(); JClassType[] generics = superClass.isParameterized().getTypeArgs(); JClassType eventHandlerClass = generics[0]; // Here you would retrieve the metadata based on typeName for this // Screen SourceWriter src = getSourceWriter(classType, context, logger); if (src != null) { src.println("@Override"); src.println("public " + String.class.getName() + " getEventType(){"); src.println("return " + classType.getQualifiedSourceName() + ".class.getName();"); src.println("}"); if (superClass.getQualifiedSourceName().equals(RemoteEventWrapper.class.getName()) || superClass.getQualifiedSourceName().equals(UndoableRemoteEventWrapper.class.getName())) { JClassType eventClass = generics[1]; src.println("@Override"); src.println("public void wrap(" + eventClass.getQualifiedSourceName() + " event){"); for (JMethod method : classType.getMethods()) { String realMethodName = method.getName().replaceAll("_", "()."); src.println("setProperty(\"" + method.getName() + "\", " + String.class.getName() + ".valueOf(event." + realMethodName + "()));"); } src.println("}"); for (JMethod method : classType.getMethods()) { if (method.isAbstract()) { src.println(); src.println("@Override"); src.println("public " + String.class.getName() + " " + method.getName() + "(){"); src.println( JSONValue.class.getName() + " value = json.get(\"" + method.getName() + "\");"); src.println( "return value!=null && value.isString()!=null ? value.isString().stringValue() : null;"); src.println("}"); } } src.println(); } src.println("@Override"); src.println("public " + GwtEvent.class.getName() + "." + Type.class.getSimpleName() + "<" + eventHandlerClass.getQualifiedSourceName() + "> getAssociatedType() {"); src.println("\treturn " + classType.getQualifiedSourceName() + ".TYPE;"); src.println("}"); src.println(); src.println("@Override"); src.println( "protected void dispatch(" + eventHandlerClass.getQualifiedSourceName() + " handler) {"); // for (JMethod m : eventHandlerClass.getMethods()) { // if(!m.getName().equals("undo")){ boolean undoable = classType .isAssignableTo(context.getTypeOracle().getType(UndoableRemoteEvent.class.getName())); if (undoable) { src.println("if(isUndo())"); src.println("handler.undo(this);"); src.println("else {"); src.println("handler.saveState(this);"); } src.println("\t handler.onEvent(this);"); if (undoable) { src.println("}"); } // } // } src.println("}"); src.println(); src.println("@Override"); src.println("public String serialize(" + TWICESecurityManager.class.getName() + " security) throws " + MessagingException.class.getName() + "{"); for (JField field : classType.getFields()) { if (!field.isStatic() && !field.isTransient()) { src.println("if(" + field.getName() + "!=null){"); src.println("setProperty(\"" + field.getName() + "\", String.valueOf(" + field.getName() + "));}"); } } src.println("return super.serialize(security);"); src.println("}"); src.println(); src.println("@Override"); src.println( "public " + RemoteEvent.class.getName() + "<" + eventHandlerClass.getQualifiedSourceName() + "> deserialize(String string, " + TWICESecurityManager.class.getName() + " security) throws " + MessagingException.class.getName() + "{"); src.println(RemoteEvent.class.getName() + " result = super.deserialize(string, security);"); for (JField field : classType.getFields()) { if (!field.isStatic()) { if (String.class.getName().equals(field.getType().getQualifiedSourceName())) src.println(field.getName() + " = getProperty(\"" + field.getName() + "\");"); else { src.println("String " + field.getName() + "Tmp = getProperty(\"" + field.getName() + "\");"); src.println(field.getName() + " = " + field.getName() + "Tmp!=null ? " + field.getType().getQualifiedSourceName() + ".valueOf(" + field.getName() + "Tmp) : null;"); } } } src.println("return result;"); src.println("}"); src.commit(logger); } return typeName + "Impl"; } catch (NotFoundException e) { e.printStackTrace(); } return null; }
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 www. j ava 2s.c om 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 void gatherTypes(JType type, List<JType> types) { nesting++;// ww w . jav a 2 s . co m // came here from a type that has no super class if (type == null) { nesting--; return; } // package info if (type.getQualifiedSourceName().contains("-")) { nesting--; return; } // not visible if (!isVisible(type)) { nesting--; return; } // filter reflection scope based on configuration in gwt xml module boolean keep = false; String name = type.getQualifiedSourceName(); try { ConfigurationProperty prop; keep |= !name.contains("."); prop = context.getPropertyOracle().getConfigurationProperty("artemis.reflect.include"); for (String s : prop.getValues()) keep |= name.contains(s); prop = context.getPropertyOracle().getConfigurationProperty("artemis.reflect.exclude"); for (String s : prop.getValues()) keep &= !name.equals(s); } catch (BadPropertyValueException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (!keep) { nesting--; return; } // already visited this type if (types.contains(type.getErasedType())) { nesting--; return; } types.add(type.getErasedType()); out(type.getErasedType().getQualifiedSourceName(), nesting); if (type instanceof JPrimitiveType) { // nothing to do for a primitive type nesting--; return; } else { // gather fields JClassType c = (JClassType) type; JField[] fields = c.getFields(); if (fields != null) { for (JField field : fields) { gatherTypes(field.getType().getErasedType(), types); } } // gather super types & interfaces gatherTypes(c.getSuperclass(), types); JClassType[] interfaces = c.getImplementedInterfaces(); if (interfaces != null) { for (JClassType i : interfaces) { gatherTypes(i.getErasedType(), types); } } // gather method parameter & return types JMethod[] methods = c.getMethods(); if (methods != null) { for (JMethod m : methods) { gatherTypes(m.getReturnType().getErasedType(), types); if (m.getParameterTypes() != null) { for (JType p : m.getParameterTypes()) { gatherTypes(p.getErasedType(), types); } } } } // gather inner classes JClassType[] inner = c.getNestedTypes(); if (inner != null) { for (JClassType i : inner) { gatherTypes(i.getErasedType(), types); } } } nesting--; }
From source file:com.artemis.gwtref.gen.ReflectionCacheSourceCreator.java
License:Apache License
private String createTypeGenerator(JType t) { buffer.setLength(0);/*from ww w .jav a 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 void gatherTypes(JType type, List<JType> types) { nesting++;/*from www .j a v a 2 s . c o m*/ // came here from a type that has no super class if (type == null) { nesting--; return; } // package info if (type.getQualifiedSourceName().contains("-")) { nesting--; return; } // not visible if (!isVisible(type)) { nesting--; return; } // filter reflection scope based on configuration in gwt xml module boolean keep = false; String name = type.getQualifiedSourceName(); try { ConfigurationProperty prop; keep |= !name.contains("."); prop = context.getPropertyOracle().getConfigurationProperty("gdx.reflect.include"); for (String s : prop.getValues()) keep |= name.contains(s); prop = context.getPropertyOracle().getConfigurationProperty("gdx.reflect.exclude"); for (String s : prop.getValues()) keep &= !name.equals(s); } catch (BadPropertyValueException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (!keep) { nesting--; return; } // already visited this type if (types.contains(type.getErasedType())) { nesting--; return; } types.add(type.getErasedType()); out(type.getErasedType().getQualifiedSourceName(), nesting); if (type instanceof JPrimitiveType) { // nothing to do for a primitive type nesting--; return; } else { // gather fields JClassType c = (JClassType) type; JField[] fields = c.getFields(); if (fields != null) { for (JField field : fields) { gatherTypes(field.getType().getErasedType(), types); } } // gather super types & interfaces gatherTypes(c.getSuperclass(), types); JClassType[] interfaces = c.getImplementedInterfaces(); if (interfaces != null) { for (JClassType i : interfaces) { gatherTypes(i.getErasedType(), types); } } // gather method parameter & return types JMethod[] methods = c.getMethods(); if (methods != null) { for (JMethod m : methods) { gatherTypes(m.getReturnType().getErasedType(), types); if (m.getParameterTypes() != null) { for (JType p : m.getParameterTypes()) { gatherTypes(p.getErasedType(), types); } } } } // gather inner classes JClassType[] inner = c.getNestedTypes(); if (inner != null) { for (JClassType i : inner) { gatherTypes(i.getErasedType(), types); } } } nesting--; }
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 v a2s . c o 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.didactilab.gwt.phprpc.rebind.phpgen.PhpClass.java
License:Apache License
@Override protected void getContents(TreeLogger logger, StringBuffer buffer) throws UnableToCompleteException { JClassType type = getJavaType(); buffer.append("/**\n"); buffer.append(" * @gwtname ").append(type.getQualifiedBinaryName()).append("\n"); if (type.getEnclosingType() != null) { buffer.append(" * @enclosing ").append(type.getEnclosingType().getQualifiedBinaryName()).append("\n"); }//from w w w . j a va2 s . co m buffer.append(" */\n"); buffer.append("class ").append(PhpTools.typeToString(type, true)); JClassType superClass = type.getSuperclass(); if ((superClass != null) && (!superClass.getQualifiedBinaryName().equals("java.lang.Object"))) { buffer.append(" extends ").append(PhpTools.typeToString(superClass, true)); } buffer.append(" implements IsSerializable {\n"); if (serializable) { for (JField field : type.getFields()) { if (field.isStatic()) continue; if (field.isTransient()) continue; buffer.append("\t/** @var ").append(PhpTools.typeToString(field.getType(), false)).append(" */\n"); buffer.append("\tpublic $").append(field.getName()).append(";\n\n"); } } buffer.append("}\n"); }
From source file:com.github.nmorel.gwtjackson.rebind.property.PropertyParser.java
License:Apache License
private static void parseFields(TreeLogger logger, JClassType type, Map<String, PropertyAccessorsBuilder> propertiesMap, boolean mixin) { if (type.getQualifiedSourceName().equals("java.lang.Object")) { return;// w w w .j av a 2 s . c om } for (JField field : type.getFields()) { if (field.isStatic()) { continue; } String fieldName = field.getName(); PropertyAccessorsBuilder property = propertiesMap.get(fieldName); if (null == property) { property = new PropertyAccessorsBuilder(fieldName); propertiesMap.put(fieldName, property); } if (property.getField().isPresent() && !mixin) { // we found an other field with the same name on a superclass. we ignore it logger.log(Type.INFO, "A field with the same name as '" + field.getName() + "' has already been found on child class"); } else { property.addField(field, mixin); } } }
From source file:com.guit.rebind.binder.GuitBinderGenerator.java
License:Apache License
public static void collectAllFields(JClassType clazz, ArrayList<JField> list) { for (JField jField : clazz.getFields()) { list.add(jField);/*from w w w . j a v a 2s. c om*/ } JClassType superclass = clazz.getSuperclass(); if (superclass != null) { collectAllFields(superclass, list); } }
From source file:com.guit.rebind.binder.GuitBinderGenerator.java
License:Apache License
private void checkForRepetition(JClassType presenterType) throws UnableToCompleteException { ArrayList<JField> superFields = new ArrayList<JField>(); collectAllFields(presenterType.getSuperclass(), superFields); ArrayList<String> superFieldsNames = new ArrayList<String>(); for (JField jField : superFields) { superFieldsNames.add(jField.getName()); }/*from www .ja v a2 s. c o m*/ for (JField f : presenterType.getFields()) { if (f.isAnnotationPresent(ViewField.class) || f.getName().equals("driver")) { if (superFieldsNames.contains(f.getName())) { error("The field '" + f.getName() + "' is already declared on a superclass, to fix delete the field. Found: " + presenterType.getQualifiedSourceName()); } } } }