List of usage examples for com.google.gwt.user.rebind SourceWriter print
void print(String s);
From source file:com.threerings.gwt.rebind.MessagesLookupGenerator.java
License:Open Source License
protected void generateLookupMethod(GeneratorContext ctx, SourceWriter code) throws NotFoundException { JMethod[] methods = ctx.getTypeOracle().getType(_using).getMethods(); code.println("@Override public String fetch (String key, Object... params) {"); code.indent();// w ww.j ava 2s . c o m code.println("int length = (params != null) ? params.length : 0;"); for (JMethod method : methods) { String s = method.getName(); int jpi = method.getParameters().length; code.println("if (key.equals(\"" + s + "\") && length >= " + jpi + ") {"); code.indent(); code.print("return msg." + s + "("); for (int jj = 0; jj < jpi; jj++) { if (jj > 0) { code.print(", "); } code.print("params[" + jj + "].toString()"); } code.println(");"); // end return code.outdent(); code.println("}"); // end if } code.println("return \"Invalid key: \" + key + \" with \" + length + \" params.\";"); // end function code.outdent(); code.println("}"); }
From source file:com.totsp.gwittir.rebind.beans.IntrospectorGenerator.java
License:Open Source License
public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { //.println("Introspector Generate."); try {//from w w w.jav a2 s.co m this.objectType = context.getTypeOracle().getType("java.lang.Object"); } catch (NotFoundException ex) { logger.log(TreeLogger.ERROR, typeName, ex); return null; } List<BeanResolver> introspectables = this.getIntrospectableTypes(logger, context.getTypeOracle()); MethodWrapper[] methods = this.findMethods(logger, introspectables); ClassSourceFileComposerFactory mcf = new ClassSourceFileComposerFactory(this.packageName, this.methodsImplementationName); mcf.addImport(com.totsp.gwittir.client.beans.Method.class.getCanonicalName()); PrintWriter methodsPrintWriter = context.tryCreate(logger, this.packageName, this.methodsImplementationName); if (methodsPrintWriter != null) { SourceWriter methodsWriter = mcf.createSourceWriter(context, methodsPrintWriter); this.writeMethods(logger, methods, methodsWriter); methodsWriter.println("}"); context.commit(logger, methodsPrintWriter); } ClassSourceFileComposerFactory cfcf = new ClassSourceFileComposerFactory(this.packageName, this.implementationName); cfcf.addImplementedInterface(typeName); cfcf.addImport("java.util.HashMap"); cfcf.addImport(com.totsp.gwittir.client.beans.Method.class.getCanonicalName()); cfcf.addImport(com.totsp.gwittir.client.beans.Property.class.getCanonicalName()); cfcf.addImport(com.totsp.gwittir.client.beans.BeanDescriptor.class.getCanonicalName()); PrintWriter printWriter = context.tryCreate(logger, packageName, implementationName); if (printWriter == null) { //.println( "Introspector Generate skipped."); return packageName + "." + implementationName; } SourceWriter writer = cfcf.createSourceWriter(context, printWriter); this.writeIntrospectables(logger, introspectables, methods, writer); this.writeResolver(introspectables, writer); writer.println( "private HashMap<Class,BeanDescriptor> beanDescriptorLookup = new HashMap<Class,BeanDescriptor>();"); writer.println(); writer.println("public BeanDescriptor getDescriptor( Object object ){ "); writer.indent(); writer.println( "if( object == null ) throw new NullPointerException(\"Attempt to introspect null object\");"); writer.println("if( object instanceof " + SelfDescribed.class.getCanonicalName() + " ) return ((SelfDescribed)object).__descriptor();"); writer.println("BeanDescriptor descriptor = beanDescriptorLookup.get(object.getClass());"); writer.println("if (descriptor!=null){"); writer.indentln("return descriptor;"); writer.outdent(); writer.println("}"); writer.println("descriptor=_getDescriptor(object);"); writer.println("beanDescriptorLookup.put(object.getClass(),descriptor);"); writer.println("return descriptor;"); writer.outdent(); writer.println("}"); writer.println("private BeanDescriptor _getDescriptor( Object object ){ "); writer.indent(); for (BeanResolver resolver : introspectables) { writer.println("if( object instanceof " + resolver.getType().getQualifiedSourceName() + " ) {"); writer.indent(); String name = resolver.getType().getQualifiedSourceName().replaceAll("\\.", "_"); logger.log(TreeLogger.DEBUG, "Writing : " + name, null); writer.print("return " + name + " == null ? " + name + " = "); this.writeBeanDescriptor(logger, resolver, methods, writer); writer.print(": " + name + ";"); writer.outdent(); writer.println("}"); } writer.println(" throw new IllegalArgumentException(\"Unknown type\" + object.getClass() ); "); writer.outdent(); writer.println("}"); writer.outdent(); writer.println("}"); context.commit(logger, printWriter); //.println( "Introspector Generate completed."); return packageName + "." + implementationName; }
From source file:com.totsp.gwittir.rebind.beans.IntrospectorGenerator.java
License:Open Source License
private boolean box(JType type, SourceWriter writer) { if ((type.isPrimitive() != null) && (type.isPrimitive() == JPrimitiveType.INT)) { writer.print("new Integer( "); return true; }//from w w w .ja va2 s . c o m if ((type.isPrimitive() != null) && (type.isPrimitive() == JPrimitiveType.LONG)) { writer.print("new Long( "); return true; } if ((type.isPrimitive() != null) && (type.isPrimitive() == JPrimitiveType.FLOAT)) { writer.print("new Float( "); return true; } if ((type.isPrimitive() != null) && (type.isPrimitive() == JPrimitiveType.DOUBLE)) { writer.print("new Double( "); return true; } if ((type.isPrimitive() != null) && (type.isPrimitive() == JPrimitiveType.CHAR)) { writer.print("new Character( "); return true; } if ((type.isPrimitive() != null) && (type.isPrimitive() == JPrimitiveType.BYTE)) { writer.print("new Byte( "); return true; } if ((type.isPrimitive() != null) && (type.isPrimitive() == JPrimitiveType.BOOLEAN)) { writer.print("new Boolean( "); return true; } return false; }
From source file:com.totsp.gwittir.rebind.beans.IntrospectorGenerator.java
License:Open Source License
private boolean unbox(JType type, String reference, SourceWriter writer) { if ((type.isPrimitive() != null) && (type.isPrimitive() == JPrimitiveType.INT)) { writer.print("((Integer) " + reference + ").intValue()"); return true; }/*w w w . j ava 2s .co m*/ if ((type.isPrimitive() != null) && (type.isPrimitive() == JPrimitiveType.LONG)) { writer.print("((Long) " + reference + ").longValue()"); return true; } if ((type.isPrimitive() != null) && (type.isPrimitive() == JPrimitiveType.FLOAT)) { writer.print("((Float) " + reference + ").floatValue()"); return true; } if ((type.isPrimitive() != null) && (type.isPrimitive() == JPrimitiveType.DOUBLE)) { writer.print("((Double) " + reference + ").doubleValue()"); return true; } if ((type.isPrimitive() != null) && (type.isPrimitive() == JPrimitiveType.CHAR)) { writer.print("((Character) " + reference + ").charValue()"); return true; } if ((type.isPrimitive() != null) && (type.isPrimitive() == JPrimitiveType.BYTE)) { writer.print("((Byte) " + reference + ").byteValue()"); return true; } if ((type.isPrimitive() != null) && (type.isPrimitive() == JPrimitiveType.BOOLEAN)) { writer.print("((Boolean) " + reference + ").booleanValue()"); return true; } writer.print("(" + type.getQualifiedSourceName() + ") " + reference); return false; }
From source file:com.totsp.gwittir.rebind.beans.IntrospectorGenerator.java
License:Open Source License
private void writeBeanDescriptor(TreeLogger logger, BeanResolver info, MethodWrapper[] methods, SourceWriter writer) { writer.println("new BeanDescriptor() { "); writer.indent();/* w w w . j av a 2 s .c om*/ writer.println("private HashMap lookup;"); writer.println("private Property[] properties;"); writer.println("public Property[] getProperties(){"); writer.indent(); { writer.println("if( this.properties != null ) "); writer.indentln("return this.properties;"); writer.println("this.properties = new Property[" + (info.getProperties().size()) + "];"); Collection pds = info.getProperties().values(); String[] propertyNames = new String[pds.size()]; logger.log(TreeLogger.SPAM, "" + (pds == null), null); if (pds != null) { int i = 0; for (Iterator it = pds.iterator(); it.hasNext(); i++) { RProperty p = (RProperty) it.next(); propertyNames[i] = p.getName(); writer.println("{"); writer.indent(); writer.print("Method readMethod = "); if (p.getReadMethod() == null) { writer.println("null;"); } else { writer.println(this.packageName + "." + this.methodsImplementationName + ".METHOD_" + +this.find(methods, p.getReadMethod()) + ";"); } writer.print("Method writeMethod = "); if (p.getWriteMethod() == null) { writer.println("null;"); } else { writer.println(this.packageName + "." + this.methodsImplementationName + ".METHOD_" + +this.find(methods, p.getWriteMethod()) + ";"); } logger.log(TreeLogger.DEBUG, p.getName() + " " + p.getType().getQualifiedSourceName(), null); JType ptype = this.resolveType(p.getType()); logger.log(TreeLogger.DEBUG, p.getName() + " (Erased) " + ptype.getQualifiedSourceName(), null); writer.println("this.properties[" + (i) + "] = new Property( \"" + p.getName() + "\", " + ((p.getType() != null) ? ptype.getQualifiedSourceName() : "Object") + ".class, readMethod, writeMethod );"); writer.outdent(); writer.println("}"); } } writer.println("return this.properties;"); } writer.outdent(); writer.println("} //end getProperties()"); writer.println("public Property getProperty( String name ) {"); writer.indent(); //TODO Rewrite this to a nested if loop using the propertyNames parameter. writer.println("Property p = null;"); writer.println("if( this.lookup != null ) {"); writer.indentln("p = (Property) lookup.get(name); "); writer.println("} else {"); writer.indent(); writer.println("this.lookup = new HashMap();"); writer.println("Property[] props = this.getProperties(); "); writer.println("for( int i=0; i < props.length; i++ ) {"); writer.indent(); writer.println("this.lookup.put( props[i].getName(), props[i] );"); writer.outdent(); writer.println("}"); writer.println("p = (Property) this.lookup.get(name);"); writer.outdent(); writer.println("}"); writer.println("if( p == null ) throw new RuntimeException(\"Couldn't find property \"+name+\" for " + info.getType().getQualifiedSourceName() + "\");"); writer.println("else return p;"); writer.outdent(); writer.println("}"); writer.outdent(); writer.print("}"); }
From source file:com.totsp.gwittir.rebind.beans.IntrospectorGenerator.java
License:Open Source License
private void writeIntrospectables(TreeLogger logger, List introspectables, MethodWrapper[] methods, SourceWriter writer) { for (Iterator it = introspectables.iterator(); it.hasNext();) { BeanResolver bean = (BeanResolver) it.next(); logger.branch(TreeLogger.DEBUG, "Introspecting: " + bean.getType().getQualifiedSourceName(), null); try {/*from ww w. j av a 2s .co m*/ if (bean.getProperties().size() == 0) { continue; } writer.print("private static BeanDescriptor "); writer.print(bean.getType().getQualifiedSourceName().replaceAll("\\.", "_")); writer.println(" = null;"); } catch (Exception e) { logger.log(TreeLogger.ERROR, "Unable to introspect class. Is class a bean?", e); } } }
From source file:com.totsp.gwittir.rebind.beans.IntrospectorGenerator.java
License:Open Source License
private void writeMethod(TreeLogger logger, MethodWrapper method, SourceWriter writer) { JType ptype = this.resolveType(method.getDeclaringType()); writer.println("new Method(){ "); writer.indent();/*from w ww . ja v a 2s . com*/ writer.println("public String getName() {"); writer.indentln("return \"" + method.getBaseMethod().getName() + "\";"); writer.println(" }"); writer.println("public Object invoke( Object target, Object[] args ) throws Exception {"); writer.indent(); writer.println(ptype.getQualifiedSourceName() + " casted ="); writer.println("(" + ptype.getQualifiedSourceName() + ") target;"); logger.log(TreeLogger.SPAM, "Method: " + method.getBaseMethod().getName() + " " + method.getBaseMethod().getReturnType().getQualifiedSourceName(), null); if (!(method.getBaseMethod().getReturnType().isPrimitive() == JPrimitiveType.VOID)) { writer.print("return "); } JType type = this.resolveType(method.getBaseMethod().getReturnType()); boolean boxed = this.box(type, writer); writer.print("casted." + method.getBaseMethod().getName() + "("); if (method.getBaseMethod().getParameters() != null) { for (int j = 0; j < method.getBaseMethod().getParameters().length; j++) { JType arg = this.resolveType(method.getBaseMethod().getParameters()[j].getType()); this.unbox(arg, "args[" + j + "]", writer); if (j != (method.getBaseMethod().getParameters().length - 1)) { writer.print(", "); } } } writer.print(")"); if (boxed) { writer.print(")"); } writer.println(";"); if (method.getBaseMethod().getReturnType().getQualifiedSourceName().equals("void")) { writer.println("return null;"); } writer.outdent(); writer.println("}"); writer.outdent(); writer.println("};"); }
From source file:com.totsp.gwittir.rebind.beans.IntrospectorGenerator.java
License:Open Source License
private void writeMethods(TreeLogger logger, MethodWrapper[] methods, SourceWriter writer) { for (int i = 0; i < methods.length; i++) { writer.print("public static final Method METHOD_" + i + " = "); writeMethod(logger, methods[i], writer); }// ww w . j av a 2 s. c o m }
From source file:com.totsp.gwittir.serial.json.rebind.JSONCodecGenerator.java
License:Open Source License
private void writeSerializer(SourceWriter writer, BeanResolver r) { writer.println("public JSONObject serializeToJSONObject( " + r.getType().getQualifiedSourceName() + " source ) throws SerializationException { "); writer.indent();//www. ja v a 2 s.c o m writer.println(" JSONObject destination = new JSONObject();"); for (RProperty prop : r.getProperties().values()) { if (prop.getName().equals("class") || prop.getReadMethod() == null) { continue; } JSONField field = prop.getReadMethod().getBaseMethod().getAnnotation(JSONField.class); JSONOmit omit = prop.getReadMethod().getBaseMethod().getAnnotation(JSONOmit.class); System.out.println(" ws \t " + prop.getName() + " " + prop.getReadMethod().getBaseMethod().getEnclosingType() + prop.getReadMethod().getBaseMethod().getReadableDeclaration() + " " + omit + " " + field); if (omit != null) { continue; } String fieldName = (field == null) ? prop.getName() : field.value(); if (prop.getReadMethod() != null) { JClassType classType = prop.getType().isClassOrInterface(); JArrayType arrayType = prop.getType().isArray(); System.out.println(prop.getName() + " ArrayType " + arrayType + " :: " + ((arrayType == null ? "" : "" + arrayType.getComponentType()))); if ((classType != null) && (classType.isAssignableTo(this.collectionType)) || arrayType != null) { JType subType = (arrayType != null) ? arrayType.getComponentType() : classType.asParameterizationOf(this.collectionType.isGenericType()).getTypeArgs()[0]; writer.println(); writer.println( " if( source." + prop.getReadMethod().getBaseMethod().getName() + "() == null ){"); writer.println("destination.put(\"" + fieldName + "\", JSONNull.getInstance());"); writer.println(" } else { "); writer.println("int i=0; JSONArray value = new JSONArray();"); writer.println("for( " + subType.getQualifiedSourceName() + " o : source." + prop.getReadMethod().getBaseMethod().getName() + "()){"); writer.println(" value.set(i++, " + toType(subType, " o ") + ");"); writer.println("}"); writer.println("destination.put(\"" + fieldName + "\", value);"); //TODO JSONField writer.println("}"); } else { writer.print("destination.put( \"" + fieldName + "\", "); //TODO JSONField writer.print(toType(prop.getType(), " source." + prop.getReadMethod().getBaseMethod().getName() + "() ") + ");"); } } } writer.outdent(); writer.println("return destination;"); writer.println("}"); writer.println("public String serialize(" + r.getType().getQualifiedSourceName() + " source ) throws SerializationException { "); writer.println(" return serializeToJSONObject(source).toString();"); writer.println("}"); }
From source file:com.vaadin.server.widgetsetutils.AcceptCriteriaFactoryGenerator.java
License:Apache License
private void generateInstantiatorMethod(SourceWriter sourceWriter, GeneratorContext context, TreeLogger logger) {/*from w w w . j a v a 2s.co m*/ sourceWriter.println("public VAcceptCriterion get(String name) {"); sourceWriter.indent(); sourceWriter.println("name = name.intern();"); JClassType criteriaType = context.getTypeOracle().findType(VAcceptCriterion.class.getName()); JClassType[] subtypes = criteriaType.getSubtypes(); Arrays.sort(subtypes, ConnectorBundle.jClassComparator); for (JClassType clientClass : subtypes) { AcceptCriterion annotation = clientClass.getAnnotation(AcceptCriterion.class); if (annotation != null) { String clientClassName = clientClass.getQualifiedSourceName(); Class<?> serverClass = clientClass.getAnnotation(AcceptCriterion.class).value(); String serverClassName = serverClass.getCanonicalName(); logger.log(Type.INFO, "creating mapping for " + serverClassName); sourceWriter.print("if (\""); sourceWriter.print(serverClassName); sourceWriter.print("\" == name) return GWT.create("); sourceWriter.print(clientClassName); sourceWriter.println(".class );"); sourceWriter.print("else "); } } sourceWriter.println("return null;"); sourceWriter.outdent(); sourceWriter.println("}"); }