List of usage examples for com.google.gwt.core.ext.typeinfo JClassType getMethods
JMethod[] getMethods();
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 {/*from ww w . j ava 2 s. c o 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:com.ait.ext4j.rebind.BeanModelGenerator.java
License:Apache License
protected void addGetters(JClassType cls, List<JMethod> methods) { if (cls == null) { return;//w w w . ja va2 s .com } // ignore methods of Object if (cls.isInterface() != null || cls.getSuperclass() != null) { addGetters(cls.getSuperclass(), methods); for (JMethod m : cls.getMethods()) { if (m.isPublic() || m.isProtected()) { String name = m.getName(); if ((name.matches("get.*") || name.matches("is.*")) && m.getParameters().length == 0) { methods.add(m); } } } } }
From source file:com.ait.ext4j.rebind.BeanModelGenerator.java
License:Apache License
protected void addSetters(JClassType cls, List<JMethod> methods) { if (cls.getSuperclass() != null) { addSetters(cls.getSuperclass(), methods); }/*from www .j ava2s . co m*/ for (JMethod m : cls.getMethods()) { if (m.isPublic() || m.isProtected()) { String name = m.getName(); if (name.matches("set.*") && m.getParameters().length == 1) { methods.add(m); } } } }
From source file:com.artemis.gwtref.gen.ReflectionCacheSourceCreator.java
License:Apache License
private void gatherTypes(JType type, List<JType> types) { nesting++;/*www .jav a2 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("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 w w w . jav a2 s . com 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 ww w . ja 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 www . j a va 2s . 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.cgxlib.xq.rebind.JsniBundleGenerator.java
License:Apache License
public String generate(TreeLogger logger, GeneratorContext context, String requestedClass) throws UnableToCompleteException { TypeOracle oracle = context.getTypeOracle(); JClassType clazz = oracle.findType(requestedClass); String packageName = clazz.getPackage().getName(); String className = clazz.getName().replace('.', '_') + "_Impl"; String fullName = packageName + "." + className; PrintWriter pw = context.tryCreate(logger, packageName, className); if (pw != null) { ClassSourceFileComposerFactory fact = new ClassSourceFileComposerFactory(packageName, className); if (clazz.isInterface() != null) { fact.addImplementedInterface(requestedClass); } else {/*from w w w . ja va 2 s. c o m*/ fact.setSuperclass(requestedClass); } SourceWriter sw = fact.createSourceWriter(context, pw); if (sw != null) { for (JMethod method : clazz.getMethods()) { LibrarySource librarySource = method.getAnnotation(LibrarySource.class); String value, prepend, postpend; String replace[]; if (librarySource != null) { value = librarySource.value(); prepend = librarySource.prepend(); postpend = librarySource.postpend(); replace = librarySource.replace(); } else { MethodSource methodSource = method.getAnnotation(MethodSource.class); if (methodSource != null) { value = methodSource.value(); prepend = methodSource.prepend(); postpend = methodSource.postpend(); replace = methodSource.replace(); } else { continue; } } try { // Read the javascript content String content = getContent(logger, packageName.replace(".", "/"), value); // Adjust javascript so as we can introduce it in a JSNI comment block without // breaking java syntax. String jsni = parseJavascriptSource(content); for (int i = 0; i < replace.length - 1; i += 2) { jsni = jsni.replaceAll(replace[i], replace[i + 1]); } pw.println(method.toString().replace("abstract", "native") + "/*-{"); pw.println(prepend); pw.println(jsni); pw.println(postpend); pw.println("}-*/;"); } catch (Exception e) { logger.log(TreeLogger.ERROR, "Error parsing javascript source: " + value + " " + e.getMessage()); throw new UnableToCompleteException(); } } } sw.commit(logger); } return fullName; }
From source file:com.cgxlib.xq.rebind.LazyGenerator.java
License:Apache License
public String generate(TreeLogger treeLogger, GeneratorContext generatorContext, String requestedClass) throws UnableToCompleteException { TypeOracle oracle = generatorContext.getTypeOracle(); lazyType = oracle.findType("com.cgxlib.xq.client.Lazy"); lazyBaseType = oracle.findType("com.cgxlib.xq.client.LazyBase"); assert lazyType != null : "Can't find Lazy interface type"; assert lazyBaseType != null : "Can't find LazyBase interface type"; JClassType requestedType = oracle.findType(requestedClass); JClassType targetType = null; JClassType nonLazyType = null;/*from w ww .j a v a 2s. c o m*/ for (JClassType inf : requestedType.getImplementedInterfaces()) { if (inf.isAssignableTo(lazyType)) { nonLazyType = inf.isParameterized().getTypeArgs()[0]; targetType = inf.isParameterized().getTypeArgs()[1]; break; } } if (targetType == null) return null; assert targetType != null : "Parameter of Lazy<T> not found"; String genClass = targetType.getPackage().getName() + "." + targetType.getSimpleSourceName() + getImplSuffix(); SourceWriter sw = getSourceWriter(treeLogger, generatorContext, requestedType.getPackage().getName(), targetType.getSimpleSourceName() + getImplSuffix(), targetType.getQualifiedSourceName()); if (sw != null) { generatePreamble(sw, nonLazyType.getQualifiedSourceName(), treeLogger); sw.indent(); for (JMethod method : targetType.getMethods()) { generateMethod(sw, method, nonLazyType.getQualifiedSourceName(), genClass, treeLogger); } sw.outdent(); generateDoneMethod(sw, nonLazyType, treeLogger); sw.commit(treeLogger); } return genClass; }
From source file:com.chrome.gwt.linker.ComponentGenerator.java
License:Apache License
private static String processBrowserAction(TreeLogger logger, GeneratorContext context, JClassType userType, String typeName) throws UnableToCompleteException { BrowserAction.ManifestInfo spec = userType.getAnnotation(BrowserAction.ManifestInfo.class); if (spec == null) { logger.log(TreeLogger.ERROR,//from www . jav a2 s . c om "BrowserAction (" + typeName + ") must be annotated with a Specificaiton."); throw new UnableToCompleteException(); } JMethod[] methods = userType.getMethods(); List<String> iconFileNames = new ArrayList<String>(); List<String> iconMethodNames = new ArrayList<String>(); // TODO(jaimeyap): Do something smarter about verifying that the files // actually exist on disk, and then coming up with something sane for // the path information. May even consider strong names. See what // ClientBundle/ImageResource does. for (int i = 0; i < methods.length; i++) { if (methods[i].getReturnType().getQualifiedSourceName().equals(ICON_USER_TYPE)) { JMethod method = methods[i]; String iconFileName; Icon.Source iconSource = method.getAnnotation(Icon.Source.class); if (iconSource == null) { iconFileName = method.getName() + ".png"; } else { iconFileName = iconSource.value(); } iconFileNames.add(iconFileName); iconMethodNames.add(method.getName()); } } if (iconFileNames.size() == 0) { logger.log(TreeLogger.ERROR, "BrowserActions must have at least one Icon (" + typeName + ")"); throw new UnableToCompleteException(); } context.commitArtifact(logger, new BrowserActionArtifact(spec.name(), iconFileNames.toArray(new String[0]), spec.defaultIcon(), spec.popup())); return emitBrowserActionCode(logger, context, userType, spec.name(), iconMethodNames, iconFileNames); }