List of usage examples for com.google.gwt.user.rebind SourceWriter outdent
void outdent();
From source file:com.smartgwt.rebind.BeanProperty.java
License:Open Source License
public void writeMultipleConstructor(SourceWriter source, boolean addComma) { final String beanClassName = beanClass.getSimpleBeanClassName(); source.println("new BeanPropertyMultiple<" + beanClassName + "> (\"" + getName() + "\","); source.indent();/* www . j a v a2 s . co m*/ if (getters.size() == 0) { source.println("null, // no getters"); } else { source.println("new BeanMethod[] {"); source.indent(); Iterator<BeanMethod> iterator = getters.iterator(); while (iterator.hasNext()) { BeanMethod getter = iterator.next(); getter.writeNew(source, beanClassName, getMethodIndex(getter), null, iterator.hasNext()); } source.outdent(); source.println("},"); } if (setters.size() == 0) { source.println("null // no setters"); } else { source.println("new BeanMethod[] {"); source.indent(); Iterator<BeanMethod> iterator = setters.iterator(); while (iterator.hasNext()) { BeanMethod setter = iterator.next(); setter.writeNew(source, beanClassName, null, getMethodIndex(setter), iterator.hasNext()); } source.outdent(); source.println("}"); } source.outdent(); source.println(")" + (addComma ? "," : "")); }
From source file:com.smartgwt.rebind.BeanValueType.java
License:Open Source License
public String generateFactory(TreeLogger logger, GeneratorContext context) { final String packageName = getFactoryPackage(); final String factoryName = getSimpleFactoryName(); ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, factoryName); composer.addImport(com.smartgwt.client.bean.BeanValueType.class.getCanonicalName()); composer.addImport(com.google.gwt.core.client.JavaScriptObject.class.getCanonicalName()); // Import our valueType, but without the [] designation composer.addImport(getQualifiedTypeName().replace("[]", "")); composer.addImport(beanValueType.getQualifiedSourceName()); composer.setSuperclass(beanValueType.getSimpleSourceName() + "<" + getSimpleGenericName() + ">"); PrintWriter printWriter = context.tryCreate(logger, packageName, factoryName); if (printWriter != null) { SourceWriter source = composer.createSourceWriter(context, printWriter); source.println("// This class lovingly generated by com.smartgwt.rebind.BeanValueType\n"); source.println("public static void registerValueType () {"); source.indent();//from w w w. j a v a 2 s. c om source.println("// We check first to see if it's already registered, to avoid\n" + "// constructing the singleton over and over again. This will\n" + "// be called multiple times as various BeanFactories initialize\n" + "// themselves."); source.println("if (BeanValueType.getBeanValueType(" + getSimpleValueTypeLiteral() + ") == null) {"); source.indent(); source.println("BeanValueType.registerBeanValueType(new " + getSimpleFactoryName() + "());"); source.outdent(); source.println("}"); source.outdent(); source.println("}\n"); source.println("@Override public Class<" + getSimpleTypeName() + "> getValueType () {"); source.indent(); source.println("return " + getSimpleTypeName() + ".class;"); source.outdent(); source.println("}\n"); source.println("@Override public boolean isAssignableFrom (Object value) {"); source.indent(); source.println("return value == null || value instanceof " + getSimpleTypeName() + ";"); source.outdent(); source.println("}"); if (componentType != null) { source.println("\nprivate " + getSimpleTypeName() + " emptyArray = new " + componentType.getSimpleSourceName() + "[0];"); source.println("\n@Override public " + getSimpleTypeName() + " emptyArray () {"); source.indent(); source.println("return emptyArray;"); source.outdent(); source.println("}"); } if (scClassName != null) { source.println("\n@Override public String getScClassName () {"); source.indent(); source.println("return \"" + scClassName + "\";"); source.outdent(); source.println("}"); } // Try to write a newInstance function that takes a JavaScriptObject if (isAbstract) { // If the type is abstract, our only hope is if it has a static getOrCreateRef method if (hasGetOrCreateRef) { source.println("\n@Override public " + getSimpleTypeName() + " newInstance (JavaScriptObject jsObject) {"); source.indent(); source.println("return " + getSimpleTypeName() + ".getOrCreateRef(jsObject);"); source.outdent(); source.println("}"); } } else { if (jsObjConstructor != null) { // If it has the right kind of constructor, then use that source.println("\n@Override public " + getSimpleTypeName() + " newInstance (JavaScriptObject jsObject) {"); source.indent(); source.println("return new " + getSimpleTypeName() + "(jsObject);"); source.outdent(); source.println("}"); } else if (hasSetJavaScriptObject) { // Custom subclasses likely won't have the constructor, but may have a a setJavaScriptObject method source.println("\n@Override public " + getSimpleTypeName() + " newInstance (JavaScriptObject jsObject) {"); source.indent(); source.println(getSimpleTypeName() + " value = new " + getSimpleTypeName() + "();"); source.println("value.setJavaScriptObject(jsObject);"); source.println("return value;"); source.outdent(); source.println("}"); } else if (hasGetOrCreateRef) { // And may as well fall back to getOrCreateRef if it exists source.println("\n@Override public " + getSimpleTypeName() + " newInstance (JavaScriptObject jsObject) {"); source.indent(); source.println("return " + getSimpleTypeName() + ".getOrCreateRef(jsObject);"); source.outdent(); source.println("}"); } } source.commit(logger); } return composer.getCreatedClassName(); }
From source file:com.smartgwt.rebind.BeanValueTypeFactoryGenerator.java
License:Open Source License
public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { TypeOracle typeOracle = context.getTypeOracle(); JClassType metaFactoryType = typeOracle.findType(typeName); final String genPackageName = metaFactoryType.getPackage().getName(); final String genClassName = metaFactoryType.getSimpleSourceName() + "Impl"; ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(genPackageName, genClassName); composer.addImplementedInterface(typeName); composer.addImport(com.smartgwt.client.bean.BeanValueType.class.getCanonicalName()); composer.addImport("com.smartgwt.client.bean.types.*"); PrintWriter printWriter = context.tryCreate(logger, genPackageName, genClassName); if (printWriter != null) { SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter); sourceWriter.println("// This class lovingly generated by " + BeanValueTypeFactoryGenerator.class.getCanonicalName() + "\n"); StringBuilder functions = new StringBuilder(); // Our constructor ... will be called by GWT.create() sourceWriter.println(genClassName + " () {"); sourceWriter.indent();/* w w w.j a v a2s . c o m*/ JClassType beanValueTypeClass = typeOracle .findType(com.smartgwt.client.bean.BeanValueType.class.getCanonicalName()).isClass(); // Iterate over the methods defined on the interface for (JMethod method : metaFactoryType.getMethods()) { if (method.getParameters().length != 0) { logger.log(Type.ERROR, typeName + "::" + method.getName() + " should have no parameters."); throw new UnableToCompleteException(); } JParameterizedType returnType = method.getReturnType().isParameterized(); if (returnType == null) { logger.log(Type.ERROR, typeName + "::" + method.getName() + " has a non-parameterized return type."); throw new UnableToCompleteException(); } if (returnType.getBaseType() != beanValueTypeClass) { logger.log(Type.ERROR, typeName + "::" + method.getName() + " does not have BeanValueType<> as its return type."); throw new UnableToCompleteException(); } JClassType[] typeArgs = returnType.getTypeArgs(); if (typeArgs.length != 1) { logger.log(Type.ERROR, typeName + "::" + method.getName() + " should have a return type with one parameterized type."); throw new UnableToCompleteException(); } BeanValueType beanValueType = new BeanValueType(typeArgs[0], typeOracle); // Write the function to register the value type. Note that a side-effect // is that the factory for the value type is actually generated! beanValueType.writeRegisterValueType(sourceWriter, logger, context); // And we'll need to generate the function! functions.append("\n\n@Override public BeanValueType<" + beanValueType.getQualifiedTypeName() + "> " + method.getName() + "() {\n " + "return (BeanValueType<" + beanValueType.getQualifiedTypeName() + ">) BeanValueType.getBeanValueType(" + beanValueType.getQualifiedValueTypeLiteral() + ");\n}"); } sourceWriter.outdent(); sourceWriter.println("}"); sourceWriter.println(functions.toString()); sourceWriter.commit(logger); } return composer.getCreatedClassName(); }
From source file:com.smartgwt.rebind.CanvasMetaBeanFactoryGenerator.java
License:Open Source License
@Override public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { TypeOracle oracle = context.getTypeOracle(); JClassType canvasType = oracle.findType(Canvas.class.getCanonicalName()); final String genPackageName = "com.smartgwt.client.bean"; final String genClassName = "CanvasMetaBeanFactoryImpl"; ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(genPackageName, genClassName); composer.addImplementedInterface(BeanFactory.CanvasMetaFactory.class.getCanonicalName()); PrintWriter printWriter = context.tryCreate(logger, genPackageName, genClassName); if (printWriter != null) { SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter); sourceWriter.println("// This class lovingly generated by " + CanvasMetaBeanFactoryGenerator.class.getCanonicalName() + "\n"); // Our constructor ... will be called by GWT.create() sourceWriter.println(genClassName + " () {"); sourceWriter.indent();//from w w w . j av a 2s .com for (JClassType classType : oracle.getTypes()) { if (classType.isAssignableTo(canvasType) && isEligibleForGeneration(classType)) { BeanClass beanClass = new BeanClass(classType); beanClass.generateFactory(logger, context); // We have to instantiate the factory to register it in the BeanFactory static API sourceWriter.println(beanClass.getQualifiedFactoryName() + ".create(false);"); } } sourceWriter.outdent(); sourceWriter.println("}"); sourceWriter.commit(logger); } return composer.getCreatedClassName(); }
From source file:com.smartgwt.rebind.FormItemMetaBeanFactoryGenerator.java
License:Open Source License
@Override public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { TypeOracle oracle = context.getTypeOracle(); JClassType formItemType = oracle.findType(FormItem.class.getCanonicalName()); final String genPackageName = "com.smartgwt.client.bean"; final String genClassName = "FormItemMetaBeanFactoryImpl"; ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(genPackageName, genClassName); composer.addImplementedInterface(BeanFactory.FormItemMetaFactory.class.getCanonicalName()); PrintWriter printWriter = context.tryCreate(logger, genPackageName, genClassName); if (printWriter != null) { SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter); sourceWriter.println("// This class lovingly generated by " + FormItemMetaBeanFactoryGenerator.class.getCanonicalName() + "\n"); // Our constructor ... will be called by GWT.create() sourceWriter.println(genClassName + " () {"); sourceWriter.indent();/* w w w.jav a 2 s. c o m*/ for (JClassType classType : oracle.getTypes()) { if (classType.isAssignableTo(formItemType) && isEligibleForGeneration(classType)) { BeanClass beanClass = new BeanClass(classType); beanClass.generateFactory(logger, context); // We have to instantiate the factory to register it in the BeanFactory static API sourceWriter.println(beanClass.getQualifiedFactoryName() + ".create(false);"); } } sourceWriter.outdent(); sourceWriter.println("}"); sourceWriter.commit(logger); } return composer.getCreatedClassName(); }
From source file:com.smartgwt.rebind.MetaBeanFactoryGenerator.java
License:Open Source License
public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { TypeOracle typeOracle = context.getTypeOracle(); JClassType metaFactoryType = typeOracle.findType(typeName); final String genPackageName = getFactoryPackage(metaFactoryType); final String genClassName = getSimpleFactoryName(metaFactoryType); ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(genPackageName, genClassName); composer.addImplementedInterface(typeName); composer.addImport(com.smartgwt.client.bean.BeanFactory.class.getCanonicalName()); PrintWriter printWriter = context.tryCreate(logger, genPackageName, genClassName); if (printWriter != null) { SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter); sourceWriter.println("// This class lovingly generated by " + MetaBeanFactoryGenerator.class.getCanonicalName() + "\n"); StringBuilder functions = new StringBuilder(); // Our constructor ... will be called by GWT.create() sourceWriter.println(genClassName + " () {"); sourceWriter.indent();//from www.j a v a 2 s .c o m JClassType beanFactoryType = typeOracle.findType(BeanFactory.class.getCanonicalName()).isClass(); JClassType baseWidgetType = typeOracle.findType(BaseWidget.class.getCanonicalName()).isClass(); JClassType dataClassType = typeOracle.findType(DataClass.class.getCanonicalName()).isClass(); // Iterate over the methods defined on the interface for (JMethod method : metaFactoryType.getMethods()) { if (method.getParameters().length != 0) { logger.log(Type.ERROR, typeName + "::" + method.getName() + " should have no parameters."); throw new UnableToCompleteException(); } JParameterizedType returnType = method.getReturnType().isParameterized(); if (returnType == null) { logger.log(Type.ERROR, typeName + "::" + method.getName() + " has a non-parameterized return type."); throw new UnableToCompleteException(); } if (returnType.getBaseType() != beanFactoryType) { logger.log(Type.ERROR, typeName + "::" + method.getName() + " does not have BeanFactory<> as its return type."); throw new UnableToCompleteException(); } JClassType[] typeArgs = returnType.getTypeArgs(); if (typeArgs.length != 1) { logger.log(Type.ERROR, typeName + "::" + method.getName() + " should have a return type with one parameterized type."); throw new UnableToCompleteException(); } JClassType beanClassType = typeArgs[0]; if (!baseWidgetType.isAssignableFrom(beanClassType) && !dataClassType.isAssignableFrom(beanClassType)) { logger.log(Type.ERROR, typeName + "::" + method.getName() + ": for now, factories can only be created for Canvas or DataClass and subclasses."); throw new UnableToCompleteException(); } BeanClass beanClass = new BeanClass(beanClassType); beanClass.generateFactory(logger, context); // We have to instantiate the factory to register it in the BeanFactory static API sourceWriter.println(beanClass.getQualifiedFactoryName() + ".create(false);"); // And we'll need to generate the function! functions.append("\n\n@Override public BeanFactory<" + beanClassType.getQualifiedSourceName() + "> " + method.getName() + "() {\n " + "return (BeanFactory<" + beanClassType.getQualifiedSourceName() + ">) BeanFactory.getFactory(" + beanClassType.getQualifiedSourceName() + ".class);\n}"); } sourceWriter.outdent(); sourceWriter.println("}"); sourceWriter.println(functions.toString()); sourceWriter.commit(logger); } return composer.getCreatedClassName(); }
From source file:com.threerings.gwt.rebind.MessagesLookupGenerator.java
License:Open Source License
protected void generateClass(TreeLogger logger, GeneratorContext ctx) throws NotFoundException { PrintWriter printWriter = ctx.tryCreate(logger, _packageName, _className); if (printWriter == null) { // if this is null, the class was already generated for another module so we don't need // to generate it again return;/*from w w w .j a v a2s . c o m*/ } ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(_packageName, _className); composer.setSuperclass(_typeName); SourceWriter code = composer.createSourceWriter(ctx, printWriter); code.println("protected static final " + _using + " msg = com.google.gwt.core.client.GWT.create(" + _using + ".class);"); code.println("private " + _className + "() { }"); // TODO: omit? generateLookupMethod(ctx, code); // close generated class code.outdent(); code.println("}"); // commit generated class ctx.commit(logger, printWriter); }
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();// ww w. j a v a2s .c om 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 {// w w w.j a v a 2s .c om 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();//from ww w . j av a 2s .com 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("}"); }