List of usage examples for com.google.gwt.core.ext.typeinfo JField getEnclosingType
JClassType getEnclosingType();
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 w w. jav 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:com.hiramchirino.restygwt.rebind.JsonEncoderDecoderClassCreator.java
License:Apache License
/** * /*from w ww . j ava 2s .c o m*/ * @param field * @return the name for the setter for the specified field or null if a setter can't be found. */ private String getSetterName(JField field) { String fieldName = field.getName(); fieldName = "set" + upperCaseFirstChar(fieldName); JClassType type = field.getEnclosingType(); if (exists(type, field, fieldName, true)) { return fieldName; } else { return null; } }
From source file:com.hiramchirino.restygwt.rebind.JsonEncoderDecoderClassCreator.java
License:Apache License
/** * //ww w. ja v a2 s .co m * @param field * @return the name for the getter for the specified field or null if a getter can't be found. */ private String getGetterName(JField field) { String fieldName = field.getName(); JType booleanType = null; try { booleanType = find(Boolean.class); } catch (UnableToCompleteException e) { //do nothing } if (field.getType().equals(JPrimitiveType.BOOLEAN) || field.getType().equals(booleanType)) { fieldName = "is" + upperCaseFirstChar(fieldName); } else { fieldName = "get" + upperCaseFirstChar(fieldName); } JClassType type = field.getEnclosingType(); if (exists(type, field, fieldName, false)) { return fieldName; } else { return null; } }
From source file:com.vaadin.server.widgetsetutils.metadata.FieldProperty.java
License:Apache License
public static Collection<FieldProperty> findProperties(JClassType type) { Collection<FieldProperty> properties = new ArrayList<FieldProperty>(); List<JField> fields = getPublicFields(type); for (JField field : fields) { properties.add(new FieldProperty(field.getEnclosingType(), field)); }/* ww w.ja v a2 s . c om*/ return properties; }
From source file:de.knightsoftnet.validators.rebind.GwtSpecificValidatorCreator.java
License:Apache License
private Annotation getAnnotation(final PropertyDescriptor ppropertyDescription, final boolean useField, final Class<? extends Annotation> expectedAnnotationClass) { Annotation annotation = null; if (useField) { final JField field = this.beanType.findField(ppropertyDescription.getPropertyName()); if (field.getEnclosingType().equals(this.beanType)) { annotation = field.getAnnotation(expectedAnnotationClass); }/*from w w w.ja v a2 s .co m*/ } else { final JMethod method = this.beanType.findMethod(asGetter(ppropertyDescription), NO_ARGS); if (method.getEnclosingType().equals(this.beanType)) { annotation = method.getAnnotation(expectedAnnotationClass); } } return annotation; }
From source file:de.knightsoftnet.validators.rebind.GwtSpecificValidatorCreator.java
License:Apache License
private void writeFieldWrapperMethod(final SourceWriter sw, final JField field) { this.writeUnsafeNativeLongIfNeeded(sw, field.getType()); // private native fieldType _fieldName(com.example.Bean object) /*-{ sw.print("private native ");/* w w w .j ava 2 s. c o m*/ sw.print(field.getType().getQualifiedSourceName()); sw.print(" "); sw.print(this.toWrapperName(field)); sw.print("("); sw.print(field.getEnclosingType().getQualifiedSourceName()); sw.println(" object) /*-{"); sw.indent(); // return object.@com.examples.Bean::myMethod(); sw.print("return object.@"); sw.print(field.getEnclosingType().getQualifiedSourceName()); sw.print("::" + field.getName()); sw.println(";"); // }-*/; sw.outdent(); sw.println("}-*/;"); }
From source file:fr.onevu.gwt.uibinder.rebind.JClassTypeAdapter.java
License:Apache License
/** * Creates a mock GWT field for the given Java field. * * @param realField the java field//from w ww .j ava 2s. c o m * @param enclosingType the GWT enclosing type * @return the GWT field */ public JField adaptField(final Field realField, JClassType enclosingType) { JField field = createMock(JField.class); addAnnotationBehaviour(realField, field); expect(field.getType()).andStubAnswer(new IAnswer<JType>() { public JType answer() throws Throwable { return adaptType(realField.getType()); } }); expect(field.getEnclosingType()).andStubReturn(enclosingType); expect(field.getName()).andStubReturn(realField.getName()); EasyMock.replay(field); return field; }
From source file:org.cruxframework.crux.core.rebind.ioc.IocContainerRebind.java
License:Apache License
@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)"; }/* w w w. ja v a 2 s. c o m*/ 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; }
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 w w w.j a v a 2s . c om 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:org.fusesource.restygwt.rebind.JsonEncoderDecoderClassCreator.java
License:Apache License
/** * * @param field/*www .ja v a2 s. c o m*/ * @return the name for the getter for the specified field or null if a * getter can't be found. */ private String getGetterName(JField field) { String fieldName = field.getName(); JType booleanType = null; try { booleanType = find(Boolean.class); } catch (UnableToCompleteException e) { // do nothing } JClassType type = field.getEnclosingType(); if (field.getType().equals(JPrimitiveType.BOOLEAN) || field.getType().equals(booleanType)) { fieldName = "is" + upperCaseFirstChar(field.getName()); if (exists(type, field, fieldName, false)) { return fieldName; } fieldName = "has" + upperCaseFirstChar(field.getName()); if (exists(type, field, fieldName, false)) { return fieldName; } } fieldName = "get" + upperCaseFirstChar(field.getName()); if (exists(type, field, fieldName, false)) { return fieldName; } else { return null; } }