List of usage examples for com.google.gwt.user.rebind SourceWriter commit
void commit(TreeLogger logger);
From source file:org.server.AppGenerator.java
License:Open Source License
@Override public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { //System.out.println("Starting AppGenerator.generate( typeName=" + typeName); TypeOracle oracle = context.getTypeOracle(); JClassType classType = oracle.findType(typeName); JClassType iXholonType = oracle.findType(org.primordion.xholon.base.IXholon.class.getName()); String packageName = classType.getPackage().getName(); String simpleName = classType.getSimpleSourceName() + CLASSNAME_SUFFIX; ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, simpleName); composer.setSuperclass(classType.getName()); // add imports needed by composer composer.addImport("org.primordion.xholon.app.IApplication"); composer.addImport("org.primordion.xholon.base.IControl"); composer.addImport("org.primordion.xholon.base.IMechanism"); composer.addImport("org.primordion.xholon.base.IXholon"); composer.addImport("org.primordion.xholon.base.IXholonClass"); composer.addImport("java.util.ArrayList"); composer.addImport("java.util.List"); PrintWriter printWriter = context.tryCreate(logger, packageName, simpleName); SourceWriter src = null; if (printWriter == null) { // the code has already been generated return typeName + CLASSNAME_SUFFIX; } else {//from w ww.ja v a 2s . c om src = composer.createSourceWriter(context, printWriter); } System.out.println("Starting AppGenerator.generate( typeName=" + typeName); // Add appSpecific methods. JClassType[] types = getPackageTypes(classType); // classes and objects src.println(writeMakeAppSpecificNode(packageName, classType.getSimpleSourceName(), types, iXholonType)); src.println(writeFindAppSpecificClass(packageName, classType.getSimpleSourceName(), types, iXholonType)); src.println( writeIsAppSpecificClassFindable(packageName, classType.getSimpleSourceName(), types, iXholonType)); // constants src.println(writeFindAppSpecificConstantValue(packageName, classType.getSimpleSourceName(), types, iXholonType)); // ports src.println(writeGetAppSpecificObjectVal(packageName, classType.getSimpleSourceName(), types, iXholonType, oracle)); src.println(writeSetAppSpecificObjectVal(packageName, classType.getSimpleSourceName(), types, iXholonType)); // port arrays src.println(writeSetAppSpecificObjectArrayVal(packageName, classType.getSimpleSourceName(), types, iXholonType)); // attributes src.println(writeGetAppSpecificAttribute(packageName, types, iXholonType, oracle)); src.println(writeSetAppSpecificAttribute(packageName, types, iXholonType, oracle)); src.println(writeGetAppSpecificAttributes(packageName, types, iXholonType, oracle)); src.println(writeIsAppSpecificAttribute(packageName, types, iXholonType, oracle)); src.commit(logger); return typeName + CLASSNAME_SUFFIX; }
From source file:org.server.ExternalFormatServiceGenerator.java
License:Open Source License
@Override public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { //System.out.println("Starting ExternalFormatServiceGenerator.generate( typeName=" + typeName); JClassType classType = context.getTypeOracle().findType(typeName); //System.out.println("classType=" + classType.getName()); String packageName = classType.getPackage().getName(); // org.primordion.xholon.service //System.out.println("packageName: " + packageName); String simpleName = classType.getSimpleSourceName() + CLASSNAME_SUFFIX; ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, simpleName); composer.setSuperclass(classType.getName()); // add imports needed by composer composer.addImport("org.primordion.xholon.app.IApplication"); composer.addImport("org.primordion.xholon.base.IXholon"); composer.addImport("org.primordion.xholon.base.Xholon"); composer.addImport("org.primordion.xholon.service.ef.IXholon2ExternalFormat"); PrintWriter printWriter = context.tryCreate(logger, packageName, simpleName); SourceWriter src = null; if (printWriter == null) { // the code has already been generated return typeName + CLASSNAME_SUFFIX; } else {/*from w ww .j a v a 2 s . c o m*/ src = composer.createSourceWriter(context, printWriter); } // add appSpecific methods sbAn.append("protected static final String[] DEFAULT_ACTION_NAMES = {\n"); packageName = EF_PACKAGE_NAME; JClassType efClassType = context.getTypeOracle() .findType(EF_PACKAGE_NAME + ".AbstractXholon2ExternalFormat"); JClassType[] types = getPackageTypes(efClassType); JPackage[] subPackages = null; if (types != null) { // this can happen if EF_PACKAGE_NAME = "org.primordion.ef" does not exist subPackages = getSubPackages(packageName, context.getTypeOracle()); writeActionNames(types); for (int i = 0; i < subPackages.length; i++) { writeActionNames(subPackages[i].getTypes(), subPackages[i].getName()); } } sbAn.append("};\n"); src.println(sbAn.toString()); src.println(writeGetActionList(types)); src.println(writeInitExternalFormatWriter(types)); src.commit(logger); return typeName + CLASSNAME_SUFFIX; }
From source file:org.sprintapi.hyperdata.gwt.ArrayAdapterGenerator.java
License:Apache License
@Override public String generate(TreeLogger logger, GeneratorContext context, String requestedClass) throws UnableToCompleteException { TypeOracle typeOracle = context.getTypeOracle(); JClassType objectType = typeOracle.findType(requestedClass); if (objectType == null) { logger.log(TreeLogger.ERROR, "Could not find type: " + requestedClass); throw new UnableToCompleteException(); }// ww w . j a v a 2 s . c o m String implTypeName = objectType.getSimpleSourceName() + "Impl"; String implPackageName = objectType.getPackage().getName(); JClassType[] implementedTypes = objectType.getImplementedInterfaces(); // Can only implement one interface if ((implementedTypes == null) || (implementedTypes.length != 1) || !implementedTypes[0].getQualifiedSourceName().equals(ArrayAdapter.class.getName())) { logger.log(TreeLogger.ERROR, "The type: " + requestedClass + " must implement only one interface: " + ArrayAdapter.class.getName()); throw new UnableToCompleteException(); } // Get parameterized type JParameterizedType parameterType = implementedTypes[0].isParameterized(); if (parameterType == null) { logger.log(TreeLogger.ERROR, "The type: " + parameterType + " must implement only one parameterized interface: " + ArrayAdapter.class.getName()); throw new UnableToCompleteException(); } JClassType parameterizedType = parameterType.getTypeArgs()[0]; JArrayType arrayType = parameterizedType.isArray(); if (arrayType == null) { logger.log(TreeLogger.ERROR, "The type: " + parameterType + " is not an array."); throw new UnableToCompleteException(); } ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(implPackageName, implTypeName); composerFactory.addImplementedInterface(objectType.getQualifiedSourceName()); PrintWriter printWriter = context.tryCreate(logger, implPackageName, implTypeName); if (printWriter != null) { SourceWriter sourceWriter = composerFactory.createSourceWriter(context, printWriter); composeDimesionMethod(sourceWriter, arrayType); composeSetMethod(sourceWriter, arrayType); composeGetMethod(sourceWriter, parameterizedType); composeGetArrayClassMethod(sourceWriter, arrayType); composeCreateInstanceMethod(sourceWriter, arrayType); sourceWriter.commit(logger); } return implPackageName + "." + implTypeName; }
From source file:org.sprintapi.hyperdata.gwt.BeanAdapterGenerator.java
License:Apache License
@Override public String generate(TreeLogger logger, GeneratorContext context, String requestedClass) throws UnableToCompleteException { TypeOracle typeOracle = context.getTypeOracle(); JClassType objectType = typeOracle.findType(requestedClass); if (objectType == null) { logger.log(TreeLogger.ERROR, "Could not find type: " + requestedClass); throw new UnableToCompleteException(); }/*from w w w. j a v a2 s .c o m*/ String implTypeName = objectType.getSimpleSourceName() + "Impl"; String implPackageName = objectType.getPackage().getName(); JClassType[] implementedTypes = objectType.getImplementedInterfaces(); // Can only implement one interface if ((implementedTypes == null) || (implementedTypes.length != 1) || !implementedTypes[0].getQualifiedSourceName().equals(BeanAdapter.class.getName())) { logger.log(TreeLogger.ERROR, "The type: " + requestedClass + " must implement only one interface: " + BeanAdapter.class.getName()); throw new UnableToCompleteException(); } // Get parameterized type JParameterizedType parameterType = implementedTypes[0].isParameterized(); if (parameterType == null) { logger.log(TreeLogger.ERROR, "The type: " + requestedClass + " must implement only one parameterized interface: " + BeanAdapter.class.getName()); throw new UnableToCompleteException(); } JClassType parameterizedType = parameterType.getTypeArgs()[0]; ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(implPackageName, implTypeName); composerFactory.addImplementedInterface(objectType.getQualifiedSourceName()); PrintWriter printWriter = context.tryCreate(logger, implPackageName, implTypeName); if (printWriter != null) { SourceWriter sourceWriter = composerFactory.createSourceWriter(context, printWriter); composeSetPropertyValueMethod(sourceWriter, parameterizedType); composeGetPropertyValueMethod(sourceWriter, parameterizedType); composeGetPropertiesMethod(sourceWriter, parameterizedType); composeGetBeanClassMethod(sourceWriter, parameterizedType); composeGetBeanAttributesMethod(sourceWriter, parameterizedType); composeCreateInstanceMethod(sourceWriter, parameterizedType); sourceWriter.commit(logger); } return implPackageName + "." + implTypeName; }
From source file:org.tiwonk.gwt.rest.generator.ClientResourceGenerator.java
License:Apache License
@Override public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { TypeOracle typeOracle = context.getTypeOracle(); JClassType clientInterfaceType = typeOracle.findType(typeName); String apiPackage = clientInterfaceType.getPackage().getName(); String implPackage = apiPackage + ".impl"; String implClassSimpleName = clientInterfaceType.getSimpleSourceName() + "Impl"; GeneratorInfo info = new GeneratorInfo(logger, clientInterfaceType); ImportAnalyzer importAnalyzer = new ImportAnalyzer(implPackage); importAnalyzer.analyze(clientInterfaceType); ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(implPackage, implClassSimpleName);//from w w w . j ava 2s .c o m importAnalyzer.add(JSONValue.class.getName()); importAnalyzer.add(JSONObject.class.getName()); importAnalyzer.add(JSONParser.class.getName()); importAnalyzer.add(Request.class.getName()); importAnalyzer.add(Response.class.getName()); importAnalyzer.add(RequestException.class.getName()); importAnalyzer.add(RequestCallback.class.getName()); importAnalyzer.add(JavaScriptObject.class.getName()); importAnalyzer.add(AbstractClientResource.class.getName()); importAnalyzer.add(ResourceAccessException.class.getName()); importAnalyzer.add(RequestBuilder.class.getName()); importAnalyzer.addImports(composerFactory); composerFactory.setSuperclass(AbstractClientResource.class.getSimpleName()); composerFactory.addImplementedInterface(clientInterfaceType.getSimpleSourceName()); PrintWriter printWriter = context.tryCreate(logger, implPackage, implClassSimpleName); if (printWriter != null) { SourceWriter sourceWriter = composerFactory.createSourceWriter(context, printWriter); addMethods(info, sourceWriter, clientInterfaceType); sourceWriter.commit(logger); } return implPackage + "." + implClassSimpleName; }
From source file:org.turbogwt.net.http.rebind.JsonSerdesGenerator.java
License:Apache License
@Override public String generate(TreeLogger logger, GeneratorContext ctx, String typeName) throws UnableToCompleteException { TypeOracle typeOracle = ctx.getTypeOracle(); assert typeOracle != null; JClassType intfType = typeOracle.findType(typeName); if (intfType == null) { logger.log(TreeLogger.ERROR, "Unable to find metadata for type '" + typeName + "'", null); throw new UnableToCompleteException(); }/*from w w w . j a v a2 s .c o m*/ if (intfType.isInterface() == null) { logger.log(TreeLogger.ERROR, intfType.getQualifiedSourceName() + " is not an interface", null); throw new UnableToCompleteException(); } TreeLogger typeLogger = logger.branch(TreeLogger.ALL, "Generating SerDes powered by Gwt Jackson...", null); final SourceWriter sourceWriter = getSourceWriter(typeLogger, ctx, intfType); if (sourceWriter != null) { sourceWriter.println(); final ArrayList<String> serdes = new ArrayList<>(); for (JClassType type : typeOracle.getTypes()) { Json annotation = type.getAnnotation(Json.class); if (annotation != null) { serdes.add(generateSerdes(sourceWriter, type, annotation)); } } generateFields(sourceWriter); generateConstructor(sourceWriter, serdes); generateIteratorMethod(sourceWriter); sourceWriter.commit(typeLogger); } return typeName + "Impl"; }
From source file:org.vaadin.alump.offlinebuilder.OfflineConnectorReflectionGenerator.java
License:Open Source License
/** * Generates generator class with instantiate method * @param genClassName/*ww w . j a v a 2 s. com*/ * @param composer * @param sourceWriter * @param logger * @param allInstantiableClasses */ private void generateFactoryClass(String genClassName, ClassSourceFileComposerFactory composer, SourceWriter sourceWriter, TreeLogger logger, List<JClassType> allInstantiableClasses) { // generate factory constructor; simple non-parametric constructor sourceWriter.println("public " + genClassName + "( ) {}"); sourceWriter.println(); // generate factory method generateFactoryMethod(allInstantiableClasses, sourceWriter); // flush all writes to stream; mark generating process as finished and ready for compiler sourceWriter.commit(logger); }
From source file:ru.fly.shared.bean.BeanGenerator.java
License:Apache License
@Override public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { oracle = context.getTypeOracle();// ww w . ja va 2s. com beanType = oracle.findType(BaseModel.class.getName()); JClassType[] types = oracle.getTypes(); for (JClassType type : types) { if (type.isAssignableTo(beanType)) { beans.add(type); } } final String genPackageName = BeanGenerator.class.getPackage().getName(); final String genClassName = "BeanFactoryImpl"; ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(genPackageName, genClassName); composer.setSuperclass(BeanFactory.class.getCanonicalName()); PrintWriter pw = context.tryCreate(logger, genPackageName, genClassName); if (pw != null) { SourceWriter sw = composer.createSourceWriter(context, pw); sw.println("public " + beanModelName + " create(Object obj) {"); sw.println("String className = obj.getClass().getName();"); for (JClassType bean : beans) { sw.println("if(className.equals(\"" + bean.getQualifiedSourceName() + "\")){"); sw.println("return (" + beanModelName + ") new " + createBean(bean, logger, context) + "(obj);"); sw.println("}"); } sw.println("return null;"); sw.println("}"); sw.commit(logger); } return composer.getCreatedClassName(); }
From source file:ru.fly.shared.bean.BeanGenerator.java
License:Apache License
protected String createBean(JClassType bean, TreeLogger logger, GeneratorContext context) { final String genPackageName = bean.getPackage().getName(); final String genClassName = beanModelName + "_" + bean.getQualifiedSourceName().replace(".", "_"); ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(genPackageName, genClassName); composer.setSuperclass(BeanModel.class.getCanonicalName()); PrintWriter pw = context.tryCreate(logger, genPackageName, genClassName); if (pw != null) { SourceWriter sw = composer.createSourceWriter(context, pw); sw.println("private Object obj;"); sw.println("private java.util.List<String> fields = new java.util.ArrayList<String>();"); sw.println("public " + genClassName + "(Object obj){"); sw.println(" this.obj = obj;"); List<String> fields = findFields(bean); for (String fld : fields) { sw.println(" fields.add(\"" + fld + "\");"); }/*w w w. j a v a 2s. co m*/ sw.println("}"); sw.println("public java.util.List<String> getFields(){ return fields; }"); List<JMethod> getters = findGetters(bean); sw.println("public Object get(String field){"); for (JMethod method : getters) { String f = method.getName(); if (f.startsWith("get")) { f = f.replaceFirst("get", ""); } else if (f.startsWith("is")) { f = f.replaceFirst("is", ""); } f = f.substring(0, 1).toLowerCase() + f.substring(1); sw.println(" if(field.equals(\"" + f + "\"))"); sw.println(" return ((" + bean.getQualifiedSourceName() + ")obj)." + method.getName() + "();"); JClassType retType = method.getReturnType().isClass(); if (retType != null && retType.isAssignableTo(beanType)) { buildGettersForSubBean(sw, bean, retType, f, method.getName() + "()", 0); } } sw.println("return null;"); sw.println("}"); List<JMethod> setters = findSetters(bean); sw.println("public void set(String field, Object value){"); for (JMethod method : setters) { String f = method.getName().replaceFirst("set", ""); ; f = f.substring(0, 1).toLowerCase() + f.substring(1); sw.println(" if(field.equals(\"" + f + "\"))"); String castType = method.getParameters()[0].getType().getQualifiedSourceName(); if (!castType.contains(".")) castType = castType.substring(0, 1).toUpperCase() + castType.substring(1); sw.println(" ((" + bean.getQualifiedSourceName() + ")obj)." + method.getName() + "((" + castType + ")value);"); } sw.println("}"); sw.commit(logger); } return composer.getCreatedClassName(); }
From source file:sheath.rebind.SheathGenerator.java
License:Apache License
@Override public RebindResult generateIncrementally(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { TypeOracle oracle = context.getTypeOracle(); JClassType toGenerate = oracle.findType(typeName).isInterface(); if (toGenerate == null) { logger.log(TreeLogger.ERROR, typeName + " is not an interface type"); throw new UnableToCompleteException(); }/*from w ww .j a v a 2s .co m*/ JClassType sheathType = oracle.findType(Sheath.class.getCanonicalName()); if (!toGenerate.isAssignableTo(sheathType)) { logger.log(TreeLogger.ERROR, typeName + " is not assignable to " + Sheath.class.getCanonicalName()); throw new UnableToCompleteException(); } if (toGenerate.equals(sheathType)) { logger.log(TreeLogger.ERROR, "You must declare an interface that extends " + Sheath.class.getCanonicalName()); throw new UnableToCompleteException(); } // TODO: really generate incrementally! Class<?>[] moduleClasses = collectAllModules(logger, toGenerate); Map<String, String> injectableTypes = new LinkedHashMap<String, String>(); Set<Class<?>> staticInjections = new LinkedHashSet<Class<?>>(); for (Class<?> module : moduleClasses) { Module annotation = module.getAnnotation(Module.class); String moduleName = module.getCanonicalName(); for (Class<?> key : annotation.injects()) { injectableTypes.put(key.getName(), moduleName); } for (Class<?> c : annotation.staticInjections()) { staticInjections.add(c); } } String packageName = toGenerate.getPackage().getName(); String simpleSourceName = toGenerate.getName().replace('.', '_') + "Impl"; PrintWriter pw = context.tryCreate(logger, packageName, simpleSourceName); if (pw == null) { return new RebindResult(RebindMode.USE_ALL_NEW_WITH_NO_CACHING, packageName + "." + simpleSourceName); } ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName, simpleSourceName); factory.setSuperclass(AbstractSheath.class.getCanonicalName()); factory.addImplementedInterface(typeName); SourceWriter sw = factory.createSourceWriter(context, pw); // Linker sw.println("private static class Plugin extends AbstractSheathPlugin {"); sw.indent(); sw.println("@java.lang.Override"); sw.println( "public native %1$s<?> getAtInjectBinding(%2$s key, %2$s className, boolean mustBeInjectable) /*-{", Binding.class.getCanonicalName(), String.class.getCanonicalName()); sw.indent(); sw.println("switch (className) {"); LinkedHashMap<String, String> factoryMethodsToGenerate = new LinkedHashMap<String, String>(); for (JClassType type : oracle.getTypes()) { // XXX: workaround for http://code.google.com/p/google-web-toolkit/issues/detail?id=6799 // We really should use type.getName().endsWith("$$InjectAdapter") but GWT makes a JClassType // with name "InjectAdapter", thinking it's a nested class, and we lose the part before the // dollar. So in the mean time, look for the InjectAdapter in the classpath FOR EACH CLASS, // and we additionally generate a non-JSNI class to instantiate it, so that processing of the // class name is done by JDT rather than GWT's TypeOracle. String adapterName = type.getQualifiedSourceName() + "$$InjectAdapter"; boolean found; try { Class.forName(adapterName, false, Thread.currentThread().getContextClassLoader()); found = true; } catch (Throwable t) { found = false; } if (found /*&& type.isAssignableTo(oracle.findType(Binding.class.getCanonicalName()))*/) { String name = type.getQualifiedSourceName(); String factoryName = "create_" + name.replace('.', '_') + "_InjectAdapter"; factoryMethodsToGenerate.put(adapterName, factoryName); sw.println("case '%s': return this.@%s.Plugin::%s()();", name, factory.getCreatedClassName(), factoryName); } } sw.println("default: return null;"); sw.println("}"); sw.outdent(); sw.println("}-*/;"); for (Map.Entry<String, String> factoryMethodToGenerate : factoryMethodsToGenerate.entrySet()) { sw.println(); sw.println("private %s %s() {", factoryMethodToGenerate.getKey(), factoryMethodToGenerate.getValue()); sw.indentln("return new %s();", factoryMethodToGenerate.getKey()); sw.println("}"); } sw.println(); sw.println("@java.lang.Override"); sw.println("public %s[] createStaticInjections() {", StaticInjection.class.getCanonicalName()); sw.indent(); sw.println("return new %s[] {", StaticInjection.class.getCanonicalName()); sw.indent(); for (Class<?> staticInjection : staticInjections) { sw.println("new %s$$StaticInjection(),", staticInjection.getName()); } sw.outdent(); sw.println("};"); sw.outdent(); sw.println("}"); sw.outdent(); sw.print("}"); // Constructor sw.println(); sw.println("public %s() {", simpleSourceName); sw.indent(); sw.println("super(new Plugin(), "); sw.println("new %s<?>[] {", ModuleAdapter.class.getCanonicalName()); sw.indent(); for (Class<?> module : moduleClasses) { sw.println("new %s$$ModuleAdapter(),", module.getName()); } sw.outdent(); sw.println("});"); sw.outdent(); sw.outdent(); sw.println("}"); for (JMethod method : toGenerate.getOverridableMethods()) { // TODO: check arguments (number, injectable types) if (method.getParameterTypes().length != 1) { // could be injectStatics() continue; } JType toInject = method.getParameterTypes()[0]; sw.println("@java.lang.Override"); sw.println("public %s %s(%s instance) {", method.getReturnType().getParameterizedQualifiedSourceName(), method.getName(), toInject.getParameterizedQualifiedSourceName()); sw.indent(); sw.println("doInject(instance, \"members/%s\", %s.class);", toInject.getParameterizedQualifiedSourceName(), injectableTypes.get(toInject.getQualifiedBinaryName())); if (!JPrimitiveType.VOID.equals(method.getReturnType())) { sw.println("return arg;"); } sw.outdent(); sw.println("}"); } sw.commit(logger); return new RebindResult(RebindMode.USE_ALL_NEW_WITH_NO_CACHING, packageName + "." + simpleSourceName); }