List of usage examples for com.google.gwt.core.ext.typeinfo JField getAnnotation
<T extends Annotation> T getAnnotation(Class<T> annotationClass);
null if it is not. 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.guit.rebind.binder.GuitBinderGenerator.java
License:Apache License
private void printProvidedFields(JClassType baseClass, HashMap<String, JType> validBindingFieldsTypes, SourceWriter writer, boolean fromPool, ArrayList<JField> fields) throws UnableToCompleteException { for (JField f : fields) { if (f.isAnnotationPresent(ViewField.class)) { String name = f.getAnnotation(ViewField.class).name(); if (name.isEmpty()) { name = f.getName();//w w w . j av a 2s . c o m } if (f.getAnnotation(ViewField.class).provided()) { if (!fromPool) { // Sanity assert writer.println("assert (" + validBindingFieldsTypes.get(name).getQualifiedSourceName() + ")presenter." + f.getName() + " != null: \"You forgot to provide the field, make sure it is not null. Found: " + baseClass + "." + name + "\";"); writer.println( "view." + name + " = (" + validBindingFieldsTypes.get(name).getQualifiedSourceName() + ")presenter." + f.getName() + ";"); } else { writer.println("presenter." + f.getName() + " = (" + validBindingFieldsTypes.get(name).getQualifiedSourceName() + ")view." + name + ";"); } } } } }
From source file:com.guit.rebind.binder.GuitBinderGenerator.java
License:Apache License
private void printViewFieldBindings(SourceWriter writer, JClassType presenterType, String viewTypeName, HashMap<String, JType> validBindingFieldsType, ArrayList<JField> fields) throws UnableToCompleteException { Set<String> validBindingFields = validBindingFieldsType.keySet(); JClassType viewAccesorType = getType(ViewAccesor.class.getCanonicalName()); ArrayList<JField> superFields = new ArrayList<JField>(); collectAllFields(presenterType.getSuperclass(), superFields); JClassType elementDomType = getType(com.guit.client.dom.Element.class.getCanonicalName()); for (JField f : fields) { if (f.isAnnotationPresent(ViewField.class)) { // Check for repetided fields if (f.getType().isClassOrInterface().isAssignableTo(elementDomType)) { if (superFields.contains(f)) { }// w w w. ja v a2s . co m } String name = f.getName(); ViewField viewField = f.getAnnotation(ViewField.class); String viewName = viewField.name(); if (viewName.isEmpty()) { viewName = name; } if (!validBindingFields.contains(viewName)) { error("The field '%s' do not exists. Found: %s.%s", viewName, presenterType.getQualifiedSourceName(), name); } JClassType type = f.getType().isClassOrInterface(); // if (type.isInterface() == null && !viewField.provided()) { // error("A ViewField must be an interface. Found: %s.%s", presenterType // .getQualifiedSourceName(), name); // } if (type.isAssignableTo(viewAccesorType)) { writer.println("{"); writer.indent(); if (!type.isAnnotationPresent(Implementation.class)) { writer.println(type.getQualifiedSourceName() + " accesor = " + GinOracle.getProvidedInstance(type.getQualifiedSourceName()) + ".get();"); } else { JClassType implementation = getType( type.getAnnotation(Implementation.class).value().getCanonicalName()); // If they are parameterized look for the base type JParameterizedType implementationParameterized = implementation.isParameterized(); if (implementationParameterized != null) { implementation = implementationParameterized.getBaseType(); } JParameterizedType typeParameterized = type.isParameterized(); if (typeParameterized != null) { type = typeParameterized.getBaseType(); } // Verify that they are assignable if (!implementation.isAssignableTo(type)) { error("An implementation of a ViewAccesor must implement the ViewAccesor interface. Found: %s", implementation.getQualifiedSourceName()); } writer.println(type.getQualifiedSourceName() + " accesor = new " + implementation.getQualifiedSourceName() + "();"); } writer.println("accesor.setTarget(view." + viewName + ");"); writer.println("presenter." + name + " = accesor;"); writer.outdent(); writer.print("}"); writer.println(); } else if (type == null || type.isAssignableFrom(validBindingFieldsType.get(viewName).isClassOrInterface()) || type.getQualifiedSourceName().startsWith("elemental.")) { String qualifiedSourceName = f.getType().getQualifiedSourceName(); writer.println("presenter." + name + " = (" + qualifiedSourceName + ") view." + viewName + ";"); writer.println(); } else { // Interface emulation (without exceptions) writer.println( "presenter." + name + " = new " + type.getParameterizedQualifiedSourceName() + "() {"); writer.indent(); ArrayList<JMethod> methods = new ArrayList<JMethod>(); findAllMethods(type, methods); for (JMethod m : methods) { writer.print(m.getReadableDeclaration(false, true, true, true, true)); writer.println("{"); writer.indent(); // Find the parameters StringBuilder callParameters = new StringBuilder(); for (JParameter p : m.getParameters()) { if (callParameters.length() > 0) { callParameters.append(", "); } if (p.isAnnotationPresent(ImplicitCast.class)) { callParameters.append("("); callParameters .append(p.getAnnotation(ImplicitCast.class).value().getCanonicalName()); callParameters.append(") "); } callParameters.append(p.getName()); } JType returnType = m.getReturnType(); if (!returnType.equals(JPrimitiveType.VOID)) { writer.print("return "); // Implicit cast writer.print("(" + returnType.getParameterizedQualifiedSourceName() + ")"); } writer.indent(); writer.println(createdClassName + ".this.view." + viewName + "." + m.getName() + "(" + callParameters.toString() + ");"); writer.outdent(); writer.outdent(); writer.println("}"); writer.println(); } // Get .equals working on emulated interfaces for // event.getSource() comparations writer.println("@Override"); writer.println("public boolean equals(Object obj) {"); writer.indent(); writer.println("return view." + viewName + ".equals(obj);"); writer.outdent(); writer.println("}"); writer.println(); writer.outdent(); writer.println("};"); } } } }
From source file:com.gwtent.gen.GenUtils.java
License:Apache License
/** * Get All annotations from classType// w w w . j ava2 s . com * NOTE: This is ordered by ParentClass to DevidedClass * The parentclass's annotation comes first * @param <T> * @param classType * @param annotationClass * @return */ public static <T extends Annotation> Map<Object, T> getAllAnnotations(JClassType classType, Class<T> annotationClass) { Map<Object, T> results = new HashMap<Object, T>(); JClassType parent = classType.getSuperclass(); if (parent != null) { results.putAll(getAllAnnotations(parent, annotationClass)); } T a = classType.getAnnotation(annotationClass); if (a != null) { results.put(classType, a); } for (JField field : classType.getFields()) { a = field.getAnnotation(annotationClass); if (a != null) results.put(field, a); } for (JMethod method : classType.getMethods()) { a = method.getAnnotation(annotationClass); if (a != null) results.put(method, a); } return results; }
From source file:com.gwtent.gen.reflection.ReflectAllInOneCreator.java
License:Apache License
private void processFields(JClassType classType, Reflectable reflectable) { boolean need = reflectable.relationTypes(); for (JField field : classType.getFields()) { if (reflectable.fieldAnnotations() || (hasReflectionAnnotation(field))) { processAnnotationClasses(field, reflectable); JClassType type = field.getType().isClassOrInterface(); if (type != null) if (need || (hasReflection(field) && field.getAnnotation(HasReflect.class).fieldType())) if (!type.isAssignableTo(classType)) //some times, it's itself of devided class processRelationClasses(type, reflectable); addClassIfNotExists(type, reflectable); }//from w w w. java 2s . co m } }
From source file:com.gwtplatform.mvp.rebind.ClassInspector.java
License:Apache License
/** * Inspects the fields to collect the ones of the specified parameterized type with the specified * type as a parameter annotated with the specified annotation. The field can be either in the * inspected class or any of its parent classes. Only static fields are collected. * * @param type The parameterized type of the desired fields. * @param typeParameter The type parameter of {@code type}. * @param annotation The annotation that should be present on the field. * @param collection The list in which to collect matching fields. * @throws UnableToCompleteException If a field annotated with {@code annotation} is found that * does not respect the other criteria of the search. *//*from w w w .j a v a 2s .co m*/ public void collectStaticAnnotatedFields(JClassType type, JClassType typeParameter, Class<? extends Annotation> annotation, List<JField> collection) throws UnableToCompleteException { for (JClassType classType : inspectedClass.getFlattenedSupertypeHierarchy()) { for (JField field : classType.getFields()) { if (field.getAnnotation(annotation) != null) { JParameterizedType parameterizedType = field.getType().isParameterized(); if (!field.isStatic() || parameterizedType == null || !type.isAssignableFrom(parameterizedType) || !typeParameter.isAssignableFrom(parameterizedType.getTypeArgs()[0])) { logger.log(TreeLogger.ERROR, "Found the annotation @" + annotation.getSimpleName() + " on the invalid field '" + classType.getName() + "." + field.getName() + "'. Field must be static and its type must be " + type.getName() + "<" + typeParameter.getName() + ">.", null); throw new UnableToCompleteException(); } collection.add(field); } } } }
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. java2 s . co 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: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.j av a 2s .c om } 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:fr.onevu.gwt.uibinder.rebind.model.OwnerField.java
License:Apache License
/** * Constructor.//from w w w.j a va 2s . co m * * @param field * the field of the owner class * @param logger * @param context */ public OwnerField(JField field, MortalLogger logger, UiBinderContext context) throws UnableToCompleteException { this.name = field.getName(); // Get the field type and ensure it's a class or interface JClassType fieldClassType = field.getType().isClassOrInterface(); if (fieldClassType == null) { logger.die("Type for field " + name + " is not a class: " + field.getType().getSimpleSourceName()); } this.fieldType = OwnerFieldClass.getFieldClass(fieldClassType, logger, context); // Get the UiField annotation and process it UiField annotation = field.getAnnotation(UiField.class); if (annotation == null) { logger.die("Field " + name + " is not annotated with @UiField"); } isProvided = annotation.provided(); }
From source file:fr.putnami.pwt.core.inject.rebind.delegate.InitializeFormCreator.java
License:Open Source License
public InitializeFormCreator(JField modelField) { this.modelField = modelField; this.fieldType = modelField.getType(); Initialize initializeAnnotation = modelField.getAnnotation(Initialize.class); this.constantClassName = initializeAnnotation.constantsClass(); if (ConstantsWithLookup.class.equals(this.constantClassName)) { this.constantClassName = null; }//from ww w .j ava2 s. c om if (this.fieldType instanceof JParameterizedType) { JParameterizedType paramType = (JParameterizedType) this.fieldType; this.beanType = paramType.getTypeArgs()[0]; } else { throw new RuntimeException("modelField can not be injected as Model"); } }