List of usage examples for com.google.gwt.user.rebind SourceWriter indent
void indent();
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(); 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();/* w ww.j a va2s . c o m*/ 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 {// w w w.j a v a 2 s. com 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 void writeBeanDescriptor(TreeLogger logger, BeanResolver info, MethodWrapper[] methods, SourceWriter writer) { writer.println("new BeanDescriptor() { "); writer.indent(); writer.println("private HashMap lookup;"); writer.println("private Property[] properties;"); writer.println("public Property[] getProperties(){"); writer.indent();//from www . j a v a 2 s . co m { 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 writeMethod(TreeLogger logger, MethodWrapper method, SourceWriter writer) { JType ptype = this.resolveType(method.getDeclaringType()); writer.println("new Method(){ "); writer.indent(); 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();/* w w w . j a va 2s . c o m*/ 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 writeResolver(List introspectables, SourceWriter writer) { writer.println("public Class resolveClass(Object object){"); writer.indent(); for (Iterator it = introspectables.iterator(); it.hasNext();) { BeanResolver type = (BeanResolver) it.next(); writer.println("if( object instanceof " + type.getType().getQualifiedSourceName() + " ) return " + type.getType().getQualifiedSourceName() + ".class;"); }/* w w w.j av a 2 s.c o m*/ writer.println("throw new RuntimeException( \"Object \"+object+\"could not be resolved.\" );"); writer.outdent(); writer.println("}"); }
From source file:com.totsp.gwittir.serial.json.rebind.JSONCodecGenerator.java
License:Open Source License
public void writeReader(SourceWriter writer, RProperty prop) { if (prop.getWriteMethod() == null) { return;/*w w w . ja va 2s . co m*/ } JSONField field = prop.getReadMethod() == null ? null : prop.getReadMethod().getBaseMethod().getAnnotation(JSONField.class); JSONOmit omit = prop.getReadMethod() == null ? null : prop.getReadMethod().getBaseMethod().getAnnotation(JSONOmit.class); System.out.println(prop.getName() + " omit " + omit + " field " + field); if (omit != null) { return; } String fieldName = (field == null) ? prop.getName() : field.value(); try { writer.println("if(root.containsKey(\"" + fieldName + "\")){"); if (prop.getType().isPrimitive() == null) { writer.println("if(root.get(\"" + fieldName + "\").isNull() != null) {"); writer.println(this.setterPrefix(prop) + "null);"); writer.println("} else {"); } if (prop.getType().isArray() != null) { JArrayType arrayType = prop.getType().isArray(); JType paramType = arrayType.getComponentType(); writer.println("JSONArray array = root.get(\"" + fieldName + "\").isArray();"); writer.println(paramType.getQualifiedSourceName() + "[] value = new " + paramType.getQualifiedSourceName() + "[ array.size() ];"); writer.println("for(int i=0; i<array.size(); i++){"); writer.indent(); writer.println(" value[i] = " + this.fromType(paramType, "array.get(i)") + ";"); writer.outdent(); writer.println("}"); //endfor writer.println(this.setterPrefix(prop) + " value );"); } else if (prop.getType() instanceof JClassType && ((JClassType) prop.getType()).isAssignableTo(this.collectionType)) { // get the parameter type JClassType propType = (JClassType) prop.getType(); JType paramType = propType.asParameterizationOf((JGenericType) this.collectionType) .getTypeArgs()[0]; writer.println("JSONArray array = root.get(\"" + fieldName + "\").isArray();"); writer.println(propType.getParameterizedQualifiedSourceName() + " col = " + this.newCollectionExpression(propType) + ";"); writer.println("for(int i=0; i<array.size(); i++){"); writer.indent(); writer.println(" col.add(" + this.fromType(paramType, "array.get(i)") + ");"); writer.outdent(); writer.println("}"); //endfor writer.println(this.setterPrefix(prop) + " col );"); } else { writer.println( setterPrefix(prop) + fromType(prop.getType(), "root.get(\"" + fieldName + "\")") + ");"); } if (prop.getType().isPrimitive() == null) { writer.println("}"); //end null else } writer.println("}"); //end contains key } catch (Exception e) { System.out.println("Exception on prop " + prop); throw new RuntimeException(e); } }
From source file:com.totsp.gwittir.serial.json.rebind.JSONCodecGenerator.java
License:Open Source License
private void writeDeserializer(SourceWriter writer, BeanResolver r) { writer.println("public " + r.toString() + " deserialize(String data) throws SerializationException { "); writer.indent(); writer.println("try {"); writer.indent();/*from ww w .j a v a2 s.c o m*/ writer.println("JSONObject root = JSONParser.parse(data).isObject(); "); writer.println(" return this.deserializeFromJSONObject(root);"); writer.println("} catch (Exception e) { "); writer.indent(); writer.println("throw new SerializationException(e);"); writer.outdent(); writer.println("}"); writer.println("}"); writer.println("public " + r.toString() + " deserializeFromJSONObject(JSONObject root) throws SerializationException {"); writer.indent(); writer.println(" if(root == null) return null;"); writer.println("try {"); writer.indent(); writer.println(r.getType().getQualifiedSourceName() + " destination = new " + r.getType().getQualifiedSourceName() + "();"); for (RProperty p : r.getProperties().values()) { if (p.getWriteMethod() != null) { writeReader(writer, p); } } writer.println(" return destination;"); writer.outdent(); writer.println("} catch (Exception e) { "); writer.indent(); writer.println("throw new SerializationException(e);"); writer.outdent(); writer.println("}"); writer.outdent(); writer.println("}"); }
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(); writer.println(" JSONObject destination = new JSONObject();"); for (RProperty prop : r.getProperties().values()) { if (prop.getName().equals("class") || prop.getReadMethod() == null) { continue; }// ww w .ja v a 2s. c o m 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.tyf.gwtphp.linker.PHPRPCLinker.java
License:Open Source License
private void writeArray(SourceWriter src, String[] params) { src.indent(); for (String p : params) { src.println("array('type' => '%s'),", p); }/*from w ww . j a v a 2 s .c o m*/ src.outdent(); }
From source file:com.vaadin.server.widgetsetutils.AcceptCriteriaFactoryGenerator.java
License:Apache License
private void generateInstantiatorMethod(SourceWriter sourceWriter, GeneratorContext context, TreeLogger logger) {// ww w .j a v a 2 s.c o 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("}"); }