List of usage examples for com.google.gwt.core.ext.typeinfo JMethod getReturnType
JType getReturnType();
From source file:cc.alcina.framework.entity.gwtsynth.ClientReflectionGenerator.java
License:Apache License
public void writeIt(List<JClassType> beanInfoTypes, List<JClassType> instantiableTypes, SourceWriter sw, Map<JClassType, Set<RegistryLocation>> gwtRegisteringClasses, String implName) throws Exception { String qualifiedImplName = this.packageName + "." + implName; Map<JClassType, String> initClassMethodNames = new LinkedHashMap<>(); Map<JClassType, String> initNewInstanceNames = new LinkedHashMap<>(); List<String> methodLines = new ArrayList<String>(); sw.indent();/*from w w w . j a v a 2 s . c o m*/ sw.println("private JavaScriptObject createLookup;"); sw.println(); sw.println(String.format("public %s() {", implName)); sw.indent(); sw.println("super();"); sw.println("init();"); sw.outdent(); sw.println("}"); sw.println(); sw.println("@Override"); sw.println("@UnsafeNativeLong"); sw.println("public native <T> T newInstance0(Class<T> clazz, long objectId, long localId) /*-{"); sw.indent(); sw.println(String.format("var constructor = this.@%s::createLookup.get(clazz);", qualifiedImplName)); sw.println("return constructor ? constructor() : null;"); sw.outdent(); sw.println("}-*/;"); sw.println(); sw.println(); int methodCount = 0; for (JClassType jct : beanInfoTypes) { if (filter.omitForModule(jct, ReflectionAction.BEAN_INFO_DESCRIPTOR)) { continue; } String methodName = "initClass" + (methodCount++); initClassMethodNames.put(jct, methodName); sw.println(String.format("private void %s(){", methodName)); sw.indent(); sw.println( "Map<String,ClientPropertyReflector> propertyReflectors = new LinkedHashMap<String,ClientPropertyReflector>();"); for (JMethod method : getPropertyGetters(jct)) { String propertyName = getPropertyNameForReadMethod(method); if (propertyName.equals("class") || propertyName.equals("propertyChangeListeners")) { continue; } if (method.isStatic()) { continue; } Collection<Annotation> annotations = getSuperclassAnnotationsForMethod(method); int aCount = 0; String annArray = ""; boolean ignore = false; for (Annotation a : annotations) { if (a.annotationType() == Omit.class) { ignore = true; } } if (ignore) { continue; } sw.println("{"); sw.indent(); for (Annotation a : annotations) { if (!a.annotationType().isAnnotationPresent(ClientVisible.class) || a.annotationType() == RegistryLocation.class) { continue; } if (aCount++ != 0) { annArray += ", "; } String annImpl = getAnnImpl(a, ann2impl, aCount); annArray += "a" + aCount; sw.println(annImpl); } sw.println(String.format( "ClientPropertyReflector reflector = " + "new ClientPropertyReflector(\"%s\",%s.class," + " new Annotation[]{%s}) ;", propertyName, method.getReturnType().getQualifiedSourceName(), annArray)); sw.println("propertyReflectors.put(reflector.getPropertyName(), reflector);"); sw.outdent(); sw.println("}"); } int aCount = 0; String annArray = ""; for (Annotation a : getClassAnnotations(jct, visibleAnnotationClasses, false)) { if (aCount++ != 0) { annArray += ", "; } String annImpl = getAnnImpl(a, ann2impl, aCount); annArray += "a" + aCount; sw.println(annImpl); } sw.println(String.format( "ClientBeanReflector beanReflector = new ClientBeanReflector(" + "%s.class,new Annotation[]{%s},propertyReflectors);", jct.getQualifiedSourceName(), annArray)); sw.println("gwbiMap.put(beanReflector.getBeanClass(),beanReflector );"); sw.outdent(); sw.println("}"); sw.println(""); } Set<JClassType> allTypes = new LinkedHashSet<JClassType>(); allTypes.addAll(instantiableTypes); allTypes.addAll(beanInfoTypes); List<JClassType> constructorTypes = CollectionFilters.filter(allTypes, new CollectionFilter<JClassType>() { @Override public boolean allow(JClassType o) { return o.isEnum() == null; } }); methodCount = 0; for (JClassType jClassType : constructorTypes) { /* * private native void registerNewInstanceFunction0(Class clazz)/*-{ * var closure=this; * this.@au.com.barnet.jade.client.test.TestClientReflector * ::createLookup[clazz] = function() { return * closure.@au.com.barnet * .jade.client.test.TestClientReflector::createInstance0()(); }; }- */; String registerMethodName = String.format("registerNewInstanceFunction%s", methodCount); String createMethodName = String.format("createInstance%s", methodCount); initNewInstanceNames.put(jClassType, String.format("%s(%s.class);", registerMethodName, jClassType.getQualifiedSourceName())); sw.println(String.format("private Object %s(){", createMethodName)); sw.indent(); sw.println(String.format("return GWT.create(%s.class);", jClassType.getQualifiedSourceName())); sw.outdent(); sw.println("};"); sw.println(); sw.println(String.format("private native void %s(Class clazz)/*-{", registerMethodName)); sw.indent(); sw.println("var closure = this;"); sw.println(String.format("var fn = function() {", qualifiedImplName)); sw.indent(); sw.println(String.format("return closure.@%s::%s()();", qualifiedImplName, createMethodName)); sw.outdent(); sw.println("};"); sw.println(String.format("this.@%s::createLookup.set(clazz,fn);", qualifiedImplName)); sw.outdent(); sw.println("}-*/;"); sw.println(); methodCount++; } sw.println("private native void initCreateLookup0()/*-{"); sw.indent(); sw.println(String.format("this.@%s::createLookup = new Map();", qualifiedImplName)); sw.outdent(); sw.println("}-*/;"); sw.println(); sw.println("protected void initReflector(Class clazz) {"); sw.indent(); sw.println("switch(clazz.getName()){"); sw.indent(); initClassMethodNames.entrySet().forEach(e -> { sw.println("case \"%s\":", e.getKey().getQualifiedBinaryName()); sw.indent(); sw.println("%s();", e.getValue()); sw.println("break;"); sw.outdent(); }); sw.outdent(); sw.println("}"); sw.outdent(); sw.println("}"); sw.println(); sw.println("protected void initialiseNewInstance(Class clazz) {"); sw.indent(); sw.println("switch(clazz.getName()){"); sw.indent(); initNewInstanceNames.entrySet().forEach(e -> { sw.println("case \"%s\":", e.getKey().getQualifiedBinaryName()); sw.indent(); sw.println("%s", e.getValue()); sw.println("break;"); sw.outdent(); }); sw.outdent(); sw.println("}"); sw.outdent(); sw.println("}"); sw.println(); sw.println("private void init() {"); sw.indent(); sw.println("initCreateLookup0();"); for (JClassType t : allTypes) { if (!filter.omitForModule(t, ReflectionAction.NEW_INSTANCE)) { sw.println(String.format("forNameMap.put(\"%s\",%s.class);", t.getQualifiedBinaryName(), t.getQualifiedSourceName())); } } sw.println(""); sw.println("//init registry"); sw.println(""); for (JClassType clazz : gwtRegisteringClasses.keySet()) { for (RegistryLocation l : gwtRegisteringClasses.get(clazz)) { StringBuffer sb = new StringBuffer(); writeAnnImpl(l, ann2impl, 0, false, sb, false); sw.println( String.format("Registry.get().register(%s.class,%s);", clazz.getQualifiedSourceName(), sb)); } } sw.outdent(); sw.println("}"); sw.outdent(); sw.println("}"); }
From source file:com.ait.ext4j.rebind.BeanModelGenerator.java
License:Apache License
protected void createGetMethods(List<JMethod> getters, SourceWriter sw, String typeName) { sw.println("public <X> X getPropertyAsString(String s) {"); sw.println("if (allowNestedValues && NestedModelUtil.isNestedProperty(s)) {"); sw.indentln("return (X)NestedModelUtil.getNestedValue(this, s);"); sw.println("}"); for (JMethod method : getters) { JClassType returnType = method.getReturnType().isClassOrInterface(); String s = method.getName(); String p = lowerFirst(s.substring(s.startsWith("g") ? 3 : 2)); // get // or sw.println("if (s.equals(\"" + p + "\")) {"); sw.println("Object value = ((" + typeName + ")bean)." + s + "();"); try {/*from ww w . j a va 2 s.c o m*/ if (returnType != null && returnType.isAssignableTo(oracle.getType(List.class.getName())) && returnType.isParameterized() != null) { JParameterizedType type = returnType.isParameterized(); JClassType[] params = type.getTypeArgs(); if (beans.contains(params[0])) { sw.println("if (value != null) {"); sw.indent(); sw.println("java.util.List list = (java.util.List)value;"); sw.println("java.util.List list2 = " + BeanModelLookup.class.getCanonicalName() + ".get().getFactory(" + params[0].getQualifiedSourceName() + ".class).createModel((java.util.Collection) list);"); sw.outdent(); sw.println("return (X) list2;"); sw.println("}"); } } else { // swap returnType as generic types were not matching // (beans.contains(returnType)) if (returnType != null) { String t = returnType.getQualifiedSourceName(); if (t.indexOf("extends") == -1) { returnType = oracle.getType(t); } } if (beans.contains(returnType)) { sw.println("if (value != null) {"); sw.println(" BeanModel nestedModel = nestedModels.get(s);"); sw.println(" if (nestedModel != null) {"); sw.println(" Object bean = nestedModel.getBean();"); sw.println(" if (!bean.equals(value)){"); sw.println(" nestedModel = null;"); sw.println(" }"); sw.println(" }"); sw.println(" if (nestedModel == null) {"); sw.println(" nestedModel = " + BeanModelLookup.class.getCanonicalName() + ".get().getFactory(" + returnType.getQualifiedSourceName() + ".class).createModel(value);"); sw.println(" nestedModels.put(s, nestedModel);"); sw.println(" }"); sw.println(" return (X)processValue(nestedModel);"); sw.println("}"); } } } catch (Exception e) { e.printStackTrace(); } sw.println("return (X)processValue(value);"); sw.println("}"); } sw.println("return super.getFromCache(s);"); sw.println("}"); }
From source file:com.ait.toolkit.rebind.BeanModelGenerator.java
License:Open Source License
protected void createGetMethods(List<JMethod> getters, SourceWriter sw, String typeName) { sw.println("public <X> X getPropertyAsString(String s) {"); sw.println("if (allowNestedValues && NestedModelUtil.isNestedProperty(s)) {"); sw.indentln("return (X)NestedModelUtil.getNestedValue(this, s);"); sw.println("}"); for (JMethod method : getters) { JClassType returnType = method.getReturnType().isClassOrInterface(); String s = method.getName(); String p = lowerFirst(s.substring(s.startsWith("g") ? 3 : 2)); // get // or//w ww . ja v a 2 s.c o m sw.println("if (s.equals(\"" + p + "\")) {"); sw.println("Object value = ((" + typeName + ")bean)." + s + "();"); try { if (returnType != null && returnType.isAssignableTo(oracle.getType(List.class.getName())) && returnType.isParameterized() != null) { JParameterizedType type = returnType.isParameterized(); JClassType[] params = type.getTypeArgs(); if (beans.contains(params[0])) { sw.println("if (value != null) {"); sw.indent(); sw.println("java.util.List list = (java.util.List)value;"); sw.println("java.util.List list2 = " + BeanLookup.class.getCanonicalName() + ".get().getFactory(" + params[0].getQualifiedSourceName() + ".class).createModel((java.util.Collection) list);"); sw.outdent(); sw.println("return (X) list2;"); sw.println("}"); } } else { // swap returnType as generic types were not matching // (beans.contains(returnType)) if (returnType != null) { String t = returnType.getQualifiedSourceName(); if (t.indexOf("extends") == -1) { returnType = oracle.getType(t); } } if (beans.contains(returnType)) { sw.println("if (value != null) {"); sw.println(" Bean nestedModel = nestedModels.get(s);"); sw.println(" if (nestedModel != null) {"); sw.println(" Object bean = nestedModel.getBean();"); sw.println(" if (!bean.equals(value)){"); sw.println(" nestedModel = null;"); sw.println(" }"); sw.println(" }"); sw.println(" if (nestedModel == null) {"); sw.println(" nestedModel = " + BeanLookup.class.getCanonicalName() + ".get().getFactory(" + returnType.getQualifiedSourceName() + ".class).createModel(value);"); sw.println(" nestedModels.put(s, nestedModel);"); sw.println(" }"); sw.println(" return (X)processValue(nestedModel);"); sw.println("}"); } } } catch (Exception e) { e.printStackTrace(); } sw.println("return (X)processValue(value);"); sw.println("}"); } sw.println("return super.getFromCache(s);"); sw.println("}"); }
From source file:com.artemis.gwtref.gen.ReflectionCacheSourceCreator.java
License:Apache License
private void gatherTypes(JType type, List<JType> types) { nesting++;//from w ww .j a v a2s.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.badlogic.gwtref.gen.ReflectionCacheSourceCreator.java
License:Apache License
private void gatherTypes(JType type, List<JType> types) { nesting++;/* ww w .j a va2s . c om*/ // 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.cgxlib.xq.rebind.JsonBuilderGenerator.java
License:Apache License
public void generateMethod(SourceWriter sw, JMethod method, String name, TreeLogger logger) throws UnableToCompleteException { String ifaceName = method.getEnclosingType().getQualifiedSourceName(); String retType = method.getReturnType().getParameterizedQualifiedSourceName(); sw.print("public final " + retType + " " + method.getName()); JParameter[] params = method.getParameters(); if (params.length == 0) { JArrayType arr = method.getReturnType().isArray(); JParameterizedType list = method.getReturnType().isParameterized(); sw.println("() {"); sw.indent();/*from w w w .j a v a 2s . c o m*/ if (retType.matches("(java.lang.Boolean|boolean)")) { sw.println("return p.getBoolean(\"" + name + "\");"); } else if (retType.matches("java.util.Date")) { sw.println("return new Date(java.lang.Long.parseLong(p.getStr(\"" + name + "\")));"); } else if (method.getReturnType().isPrimitive() != null) { sw.println("return (" + retType + ")p.getFloat(\"" + name + "\");"); } else if (retType.equals("java.lang.Character")) { sw.println("return (char) p.getFloat(\"" + name + "\");"); } else if (retType.equals("java.lang.Byte")) { sw.println("return (byte) p.getFloat(\"" + name + "\");"); } else if (retType.equals("java.lang.Integer")) { sw.println("return (int) p.getFloat(\"" + name + "\");"); } else if (retType.equals("java.lang.Float")) { sw.println("return p.getFloat(\"" + name + "\");"); } else if (retType.equals("java.lang.Double")) { sw.println("return (double) p.getFloat(\"" + name + "\");"); } else if (retType.equals("java.lang.Long")) { sw.println("return (long) p.getFloat(\"" + name + "\");"); } else if (retType.equals("java.lang.Byte")) { sw.println("return (byte) p.getFloat(\"" + name + "\");"); } else if (isTypeAssignableTo(method.getReturnType(), stringType)) { sw.println("return p.getStr(\"" + name + "\");"); } else if (isTypeAssignableTo(method.getReturnType(), jsonBuilderType)) { String q = method.getReturnType().getQualifiedSourceName(); sw.println("return " + "((" + q + ")GWT.create(" + q + ".class))" + ".load(getPropertiesBase(\"" + name + "\"));"); } else if (isTypeAssignableTo(method.getReturnType(), settingsType)) { String q = method.getReturnType().getQualifiedSourceName(); sw.println("return " + "((" + q + ")getPropertiesBase(\"" + name + "\"));"); } else if (retType.equals(Properties.class.getName())) { sw.println("return getPropertiesBase(\"" + name + "\");"); } else if (isTypeAssignableTo(method.getReturnType(), jsType)) { sw.println("return p.getJavaScriptObject(\"" + name + "\");"); } else if (isTypeAssignableTo(method.getReturnType(), functionType)) { sw.println("return p.getFunction(\"" + name + "\");"); } else if (arr != null || list != null) { JType type = arr != null ? arr.getComponentType() : list.getTypeArgs()[0]; boolean buildType = isTypeAssignableTo(type, jsonBuilderType); String t = type.getQualifiedSourceName(); sw.println("JsArrayMixed a = p.getArray(\"" + name + "\");"); sw.println("int l = a == null ? 0 : a.length();"); String ret; if (buildType) { sw.println(t + "[] r = new " + t + "[l];"); sw.println("JsObjectArray<?> a1 = p.getArray(\"" + name + "\").cast();"); sw.println("int l1 = r.length;"); sw.println("for (int i = 0 ; i < l1 ; i++) {"); sw.println(" Object w = a1.get(i);"); sw.println(" " + t + " instance = GWT.create(" + t + ".class);"); sw.println(" r[i] = instance.load(w);"); sw.println("}"); ret = "r"; } else { ret = "getArrayBase(\"" + name + "\", new " + t + "[l], " + t + ".class)"; } if (arr != null) { sw.println("return " + ret + ";"); } else { sw.println("return Arrays.asList(" + ret + ");"); } } else if (method.getReturnType().isEnum() != null) { sw.println("return " + method.getReturnType().getQualifiedSourceName() + ".valueOf(p.getStr(\"" + name + "\"));"); } else { sw.println("System.err.println(\"JsonBuilderGenerator WARN: unknown return type " + retType + " " + ifaceName + "." + name + "()\"); "); // We return the object because probably the user knows how to handle it sw.println("return p.get(\"" + name + "\");"); } sw.outdent(); sw.println("}"); } else if (params.length == 1) { JType type = params[0].getType(); JArrayType arr = type.isArray(); JParameterizedType list = type.isParameterized(); sw.print("(" + type.getParameterizedQualifiedSourceName() + " a)"); sw.println("{"); sw.indent(); if (arr != null || list != null) { String a = "a"; if (list != null) { a = "a.toArray(new " + list.getTypeArgs()[0].getQualifiedSourceName() + "[0])"; } sw.println("setArrayBase(\"" + name + "\", " + a + ");"); } else if (type.getParameterizedQualifiedSourceName().matches("java.util.Date")) { sw.println("p.setNumber(\"" + name + "\", a.getTime());"); } else if (type.getParameterizedQualifiedSourceName().matches( "(java.lang.(Character|Long|Double|Integer|Float|Byte)|(char|long|double|int|float|byte))")) { sw.println("p.setNumber(\"" + name + "\", a);"); } else if (type.getParameterizedQualifiedSourceName().matches("(java.lang.Boolean|boolean)")) { sw.println("p.setBoolean(\"" + name + "\", a);"); } else if (type.getParameterizedQualifiedSourceName().matches("com.cgxlib.xq.client.Function")) { sw.println("p.setFunction(\"" + name + "\", a);"); } else if (type.isEnum() != null) { sw.println("p.set(\"" + name + "\", a.name());"); } else { sw.println("set(\"" + name + "\", a);"); } if (!"void".equals(retType)) { if (isTypeAssignableTo(method.getReturnType(), method.getEnclosingType())) { sw.println("return this;"); } else { sw.println("return null;"); } } sw.outdent(); sw.println("}"); } }
From source file:com.cgxlib.xq.rebind.LazyGenerator.java
License:Apache License
public void generateMethod(SourceWriter sw, JMethod method, String nonLazyClass, String genClass, TreeLogger logger) throws UnableToCompleteException { JParameter[] params = method.getParameters(); JTypeParameter gType = method.getReturnType().isTypeParameter(); String retType = method.getReturnType().getParameterizedQualifiedSourceName(); if (gType != null) { retType = "<" + gType.getParameterizedQualifiedSourceName() + " extends " + gType.getFirstBound().getQualifiedSourceName() + "> " + retType; }// ww w . j a v a2s . co m sw.print("public final native " + retType + " " + method.getName()); sw.print("("); int argNum = 0; for (JParameter param : params) { sw.print((argNum == 0 ? "" : ", ") + param.getType().getParameterizedQualifiedSourceName() + " arg" + argNum); argNum++; } sw.println(") /*-{"); sw.indent(); sw.println("var self=this;"); sw.println("this.@" + genClass + "::closures.push("); sw.indent(); sw.println("function() {"); sw.indent(); sw.print("self.@" + genClass + "::ctx=self.@" + genClass + "::ctx.@" + nonLazyClass + "::" + method.getName()); sw.print(getJSNIParams(method)); sw.print("("); for (int i = 0; i < argNum; i++) { sw.print("arg" + i + (i < argNum - 1 ? "," : "")); } sw.print(")"); // special case, as() needs to invoke createLazy() if ("as".equals(method.getName())) { sw.print(".createLazy()"); } sw.println(";"); sw.outdent(); sw.println("}"); sw.outdent(); sw.println(");"); sw.println("return this;"); sw.outdent(); sw.println("}-*/;"); }
From source file:com.cgxlib.xq.rebind.SelectorGeneratorBase.java
License:Apache License
public void generateMethod(SourceWriter sw, JMethod method, TreeLogger logger) throws UnableToCompleteException { Selector selectorAnnotation = method.getAnnotation(Selector.class); if (selectorAnnotation == null) { return;//from w w w . j a v a2s . c om } JParameter[] params = method.getParameters(); String retType = method.getReturnType().getParameterizedQualifiedSourceName(); sw.print("public final " + retType + " " + method.getName()); boolean hasContext = false; if (params.length == 0) { sw.print("()"); } else if (params.length == 1) { JClassType type = params[0].getType().isClassOrInterface(); if (type != null && type.isAssignableTo(nodeType)) { sw.print("(Node root)"); hasContext = true; } } sw.println(" {"); sw.indent(); Selector sel = method.getAnnotation(Selector.class); if (sel != null && sel.value().matches("^#\\w+$")) { // short circuit #foo sw.println("return " + wrap(method, "JsNodeArray.create(((Document)root).getElementById(\"" + sel.value().substring(1) + "\"))") + ";"); } else if (sel != null && sel.value().matches("^\\w+$")) { // short circuit FOO sw.println("return " + wrap(method, "JsNodeArray.create(((Element)root).getElementsByTagName(\"" + sel.value() + "\"))") + ";"); } else if (sel != null && sel.value().matches("^\\.\\w+$") && hasGetElementsByClassName()) { // short circuit .foo for browsers with native getElementsByClassName sw.println("return " + wrap(method, "JsNodeArray.create(getElementsByClassName(\"" + sel.value().substring(1) + "\", root))") + ";"); } else { generateMethodBody(sw, method, logger, hasContext); } sw.outdent(); sw.println("}"); }
From source file:com.cgxlib.xq.rebind.SelectorGeneratorBase.java
License:Apache License
protected String wrap(JMethod method, String expr) { if ("NodeList".equals(method.getReturnType().getSimpleSourceName())) { return expr; } else {/* w w w . j a v a 2s . c o m*/ return "XQ.$(" + expr + ")"; } }
From source file:com.cgxlib.xq.rebind.SelectorGeneratorBase.java
License:Apache License
protected String wrapJS(JMethod method, String expr) { if ("XQ".equals(method.getReturnType().getSimpleSourceName())) { return expr; } else {/* w w w . j av a2 s.c om*/ return "XQ.$(" + expr + ")"; } }