List of usage examples for com.google.gwt.core.ext.typeinfo JField isDefaultAccess
boolean isDefaultAccess();
From source file:com.artemis.gwtref.gen.ReflectionCacheSourceCreator.java
License:Apache License
private String createTypeGenerator(JType t) { buffer.setLength(0);//w w w .j av a 2s.co 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 ww .j a v a 2s . 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.github.nmorel.gwtjackson.rebind.property.PropertyProcessor.java
License:Apache License
private static boolean isFieldAutoDetected(RebindConfiguration configuration, PropertyAccessors propertyAccessors, BeanInfo info) { if (!propertyAccessors.getField().isPresent()) { return false; }//from w w w. j a v a 2s. com for (Class<? extends Annotation> annotation : AUTO_DISCOVERY_ANNOTATIONS) { if (propertyAccessors.isAnnotationPresentOnField(annotation)) { return true; } } JField field = propertyAccessors.getField().get(); JsonAutoDetect.Visibility visibility = info.getFieldVisibility(); if (Visibility.DEFAULT == visibility) { visibility = configuration.getDefaultFieldVisibility(); } return isAutoDetected(visibility, field.isPrivate(), field.isProtected(), field.isPublic(), field.isDefaultAccess()); }
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 av a 2 s. 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:org.fusesource.restygwt.rebind.JsonEncoderDecoderClassCreator.java
License:Apache License
@Override public void generate() throws UnableToCompleteException { locator = new JsonEncoderDecoderInstanceLocator(context, logger); JClassType soruceClazz = source.isClass(); if (soruceClazz == null) { error("Type is not a class"); }/*from www .jav 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(); if (null != soruceClazz.isEnum()) { p(); p("public " + JSON_VALUE_CLASS + " encode(" + source.getParameterizedQualifiedSourceName() + " value) {").i(1); { p("if( value==null ) {").i(1); { p("return com.google.gwt.json.client.JSONNull.getInstance();").i(-1); } p("}"); p("return new com.google.gwt.json.client.JSONString(value.toString());"); } i(-1).p("}"); p(); p("public " + source.getName() + " decode(" + JSON_VALUE_CLASS + " value) {").i(1); { p("if( value == null || value.isNull()!=null ) {").i(1); { p("return null;").i(-1); } p("}"); p("com.google.gwt.json.client.JSONString str = value.isString();"); p("if( null == str ) {").i(1); { p("throw new DecodingException(\"Expected a json string (for enum), but was given: \"+value);") .i(-1); } p("}"); p("return Enum.valueOf(" + source.getParameterizedQualifiedSourceName() + ".class, str.stringValue());").i(-1); } p("}"); p(); return; } p("public " + JSON_VALUE_CLASS + " encode(" + source.getParameterizedQualifiedSourceName() + " value) {") .i(1); { p("if( value==null ) {").i(1); { p("return null;"); } i(-1).p("}"); 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()) { Json jsonAnnotation = field.getAnnotation(Json.class); String name = field.getName(); String jsonName = name; if (jsonAnnotation != null && jsonAnnotation.name().length() > 0) { jsonName = jsonAnnotation.name(); } String fieldExpr = "value." + name; if (getterName != null) { fieldExpr = "value." + getterName + "()"; } Style style = jsonAnnotation != null ? jsonAnnotation.style() : classStyle; String expression = locator.encodeExpression(field.getType(), fieldExpr, style); p("{").i(1); { if (null != field.getType().isEnum()) { p("if(" + fieldExpr + " == null) {").i(1); p("rc.put(" + wrap(name) + ", null);"); i(-1).p("} else {").i(1); } p(JSON_VALUE_CLASS + " v=" + expression + ";"); p("if( v!=null ) {").i(1); { p("rc.put(" + wrap(jsonName) + ", v);"); } i(-1).p("}"); if (null != field.getType().isEnum()) { 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 jsonName = field.getName(); if (jsonAnnotation != null && jsonAnnotation.name().length() > 0) { jsonName = jsonAnnotation.name(); } String objectGetter = "object.get(" + wrap(jsonName) + ")"; String expression = locator.decodeExpression(field.getType(), objectGetter, style); p("if(" + objectGetter + " != null) {").i(1); if (field.getType().isPrimitive() == null) { p("if(" + objectGetter + " instanceof com.google.gwt.json.client.JSONNull) {").i(1); if (setterName != null) { p("rc." + setterName + "(null);"); } else { p("rc." + name + "=null;"); } i(-1).p("} else {").i(1); } if (setterName != null) { p("rc." + setterName + "(" + expression + ");"); } else { p("rc." + name + "=" + expression + ";"); } i(-1).p("}"); if (field.getType().isPrimitive() == null) { i(-1).p("}"); } } else { error("field must not be private."); } return null; } }); } p("return rc;"); } i(-1).p("}"); p(); }
From source file:rocket.generator.rebind.gwt.JFieldFieldAdapter.java
License:Apache License
protected Visibility createVisibility() { Visibility visibility = null; while (true) { final JField field = this.getJField(); if (field.isPrivate()) { visibility = Visibility.PRIVATE; break; }/* www . j a v a2 s .com*/ if (field.isDefaultAccess()) { visibility = Visibility.PACKAGE_PRIVATE; break; } if (field.isProtected()) { visibility = Visibility.PROTECTED; break; } if (field.isPublic()) { visibility = Visibility.PUBLIC; break; } Checker.fail("Unknown visibility for field " + field); } return visibility; }