List of usage examples for com.google.gwt.user.rebind SourceWriter commit
void commit(TreeLogger logger);
From source file:com.xclinical.mdr.server.gwt.FactoryGenerator.java
License:Apache License
public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { TypeOracle typeOracle = context.getTypeOracle(); JClassType clazz = typeOracle.findType(typeName); if (clazz == null) { logger.log(TreeLogger.ERROR, "The type '" + typeName + "' was not found"); throw new UnableToCompleteException(); }/*from w w w . j av a2 s . co m*/ logger.log(TreeLogger.DEBUG, "Generating source for " + clazz.getQualifiedSourceName()); String packageName = clazz.getPackage().getName(); String simpleName = clazz.getSimpleSourceName() + IMPL_POSTFIX; PrintWriter printWriter = context.tryCreate(logger, packageName, simpleName); // If the printWriter is null here, this may indicate the class is already existing. // Don't know if it is a good idea to imply WE have generated it though. if (printWriter != null) { ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, simpleName); composer.addImport("java.util.HashMap"); composer.addImplementedInterface("ObjectFactory"); SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter); sourceWriter.println("private static HashMap<String, ObjectCreator> factories;"); sourceWriter.println("private static HashMap<String, Object> singletons;"); sourceWriter.println("static {"); sourceWriter.println(" factories = new HashMap<String, ObjectCreator>();"); sourceWriter.println(" singletons = new HashMap<String, Object>();"); // Add an implementation of ObjectCreator for every Instantiable. for (JClassType type : typeOracle.getTypes()) { Instantiable instantiable; if ((instantiable = type.getAnnotation(Instantiable.class)) != null) { String targetName = instantiable.value(); if (targetName.length() == 0) targetName = type.getQualifiedSourceName(); sourceWriter.println("factories.put(\"" + targetName + "\", new ObjectCreator() {"); sourceWriter.println(" public Object newInstance() {"); sourceWriter.println(" return new " + type.getQualifiedSourceName() + "();"); sourceWriter.println(" }});"); if (type.getAnnotation(Singleton.class) != null) { sourceWriter.println("singletons.put(\"" + targetName + "\", new " + type.getQualifiedSourceName() + "());"); } } } // End static { sourceWriter.println("}"); // Add an implementation for ObjectFactory.newInstance(String) that forwards // to the mappings. sourceWriter.println("public Object newInstance(String className) throws ReflectException {"); sourceWriter.println(" ObjectCreator c = factories.get(className);"); sourceWriter.println(" if (c == null) throw new ReflectException(className);"); sourceWriter.println(" return c.newInstance();"); sourceWriter.println("}"); // Add an implementation for ObjectFactory.getInstance(String) sourceWriter.println("public Object getInstance(String className) throws ReflectException {"); sourceWriter.println(" Object obj = singletons.get(className);"); sourceWriter.println(" if (obj == null) {"); sourceWriter.println(" ObjectCreator c = factories.get(className);"); sourceWriter.println(" if (c == null) throw new ReflectException(className);"); sourceWriter.println(" obj = c.newInstance();"); sourceWriter.println(" singletons.put(className, obj);"); sourceWriter.println(" }"); sourceWriter.println(" return obj;"); sourceWriter.println("}"); sourceWriter.commit(logger); logger.log(TreeLogger.DEBUG, "Done generating source for " + clazz.getQualifiedSourceName()); } return clazz.getQualifiedSourceName() + IMPL_POSTFIX; }
From source file:cz.muni.ucn.opsi.wui.gwt.rebind.beanModel.BeanModelGenerator.java
License:Open Source License
@Override public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { oracle = context.getTypeOracle();//w ww. j a va 2 s.co m beanModelMarkerType = oracle.findType(BeanModelMarker.class.getName()); beanModelTagType = oracle.findType(BeanModelTag.class.getName()); try { // final all beans and bean markers beans = new ArrayList<JClassType>(); JClassType[] types = oracle.getTypes(); for (JClassType type : types) { if (isBeanMarker(type)) { beans.add(getMarkerBean(type)); } else if (isBean(type)) { beans.add(type); } } final String genPackageName = BeanModelLookup.class.getPackage().getName(); final String genClassName = "BeanModelLookupImpl"; ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(genPackageName, genClassName); composer.setSuperclass(BeanModelLookup.class.getCanonicalName()); composer.addImport(BeanModelFactory.class.getName()); composer.addImport(Map.class.getName()); composer.addImport(FastMap.class.getName()); PrintWriter pw = context.tryCreate(logger, genPackageName, genClassName); if (pw != null) { SourceWriter sw = composer.createSourceWriter(context, pw); sw.println("private Map<String, BeanModelFactory> m;"); sw.println("public BeanModelFactory getFactory(String n) {"); sw.indent(); // sw.println("String n = b.getName();"); sw.println("if (m == null) {"); sw.indentln("m = new FastMap<BeanModelFactory>();"); sw.println("}"); sw.println("if (m.get(n) == null) {"); sw.indent(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < beans.size(); i++) { JClassType bean = beans.get(i); String name = createBean(bean, logger, context); String factory = createFactory(bean, name, logger, context); if (i > 0) { sw.print(" else "); } sw.println("if (" + bean.getQualifiedSourceName() + ".CLASS_NAME.equals(n)) {"); sw.indentln("m" + i + "();"); sb.append("private void m" + i + "() {\n"); sb.append( " m.put(" + bean.getQualifiedSourceName() + ".CLASS_NAME, new " + factory + "());\n"); sb.append("}\n"); sw.print("}"); } sw.outdent(); sw.println("}"); sw.println("return m.get(n);"); sw.outdent(); sw.println("}"); sw.println(sb.toString()); sw.commit(logger); } return composer.getCreatedClassName(); } catch (Exception e) { logger.log(TreeLogger.ERROR, "Class " + typeName + " not found.", e); throw new UnableToCompleteException(); } }
From source file:cz.muni.ucn.opsi.wui.gwt.rebind.beanModel.BeanModelGenerator.java
License:Open Source License
protected String createFactory(JClassType bean, String beanModelName, TreeLogger logger, GeneratorContext context) throws Exception { final String genPackageName = BeanModelLookup.class.getPackage().getName(); final String genClassName = "BeanModel_" + bean.getQualifiedSourceName().replace(".", "_") + "_Factory"; ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(genPackageName, genClassName); composer.setSuperclass(BeanModelFactory.class.getCanonicalName()); composer.addImport(BeanModelE.class.getCanonicalName()); composer.addImport(BeanModel.class.getCanonicalName()); PrintWriter pw = context.tryCreate(logger, genPackageName, genClassName); if (pw != null) { SourceWriter sw = composer.createSourceWriter(context, pw); sw.println("public BeanModelE newInstance() {"); sw.println("return new " + beanModelName + "();"); sw.println("}"); sw.commit(logger); }/*from w w w . j a v a 2 s. c o m*/ return composer.getCreatedClassName(); }
From source file:cz.muni.ucn.opsi.wui.gwt.rebind.beanModel.BeanModelGenerator.java
License:Open Source License
protected String createBean(JClassType bean, TreeLogger logger, GeneratorContext context) throws Exception { final String genPackageName = bean.getPackage().getName(); final String genClassName = "BeanModel_" + bean.getQualifiedSourceName().replace(".", "_"); ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(genPackageName, genClassName); composer.setSuperclass(BeanModelE.class.getCanonicalName()); composer.addImport(BeanModelE.class.getName()); composer.addImport(BeanModel.class.getName()); composer.addImport(NestedModelUtil.class.getName()); PrintWriter pw = context.tryCreate(logger, genPackageName, genClassName); if (pw != null) { List<JMethod> getters = findGetters(bean); List<JMethod> setters = findSetters(bean); SourceWriter sw = composer.createSourceWriter(context, pw); sw.println("public " + genClassName + "(){"); for (JMethod method : getters) { String s = method.getName(); String p = lowerFirst(s.substring(s.startsWith("g") ? 3 : 2)); // get or // is sw.println("beanProperties.add(\"" + p + "\");"); }/*from w w w .j a va 2s. c o m*/ sw.println("}"); createGetMethods(getters, sw, bean.getQualifiedSourceName()); createSetMethods(setters, sw, bean.getQualifiedSourceName()); // delegate equals to bean sw.println("public boolean equals(Object obj) {"); sw.println(" if (obj instanceof " + "BeanModel" + ") {"); sw.println(" obj = ((BeanModel)obj).getBean();"); sw.println(" }"); sw.println(" if (bean == null && obj == null) {"); sw.println(" return true;"); sw.println(" }"); sw.println(" if (bean == null && obj != null) {"); sw.println(" return false;"); sw.println(" }"); sw.println(" return bean.equals(obj);"); sw.println("}"); // delegate hashCode to bean sw.println("public int hashCode(){"); sw.println(" return bean.hashCode();"); sw.println("}"); sw.commit(logger); } return composer.getCreatedClassName(); }
From source file:de.csenk.gwt.commons.bean.rebind.observe.impl.ObservableBeanFactoryCreator.java
License:Apache License
/** * @return/*from www . java2 s . co m*/ */ public String create() { final String packageName = sourceType.getPackage().getName(); final PrintWriter targetTypePrintWriter = context.tryCreate(logger, packageName, simpleTargetTypeName); if (targetTypePrintWriter == null) { return packageName + "." + simpleTargetTypeName; } final ClassSourceFileComposerFactory beanFactoryFactory = new ClassSourceFileComposerFactory(packageName, simpleTargetTypeName); beanFactoryFactory.setSuperclass(AbstractObservableBeanFactory.class.getCanonicalName()); beanFactoryFactory.addImplementedInterface(sourceType.getQualifiedSourceName()); beanFactoryFactory.addImport(AutoBean.class.getCanonicalName()); beanFactoryFactory.addImport(GWT.class.getCanonicalName()); final SourceWriter beanFactoryWriter = beanFactoryFactory.createSourceWriter(context, targetTypePrintWriter); try { beanFactoryWriter.println(); for (ObservableBeanModel beanModel : model.getBeans().values()) { writeObservableBean(beanModel); } writeAutoBeanFactory(sourceType.getOracle(), beanFactoryWriter); writeFactoryMethods(beanFactoryWriter); } finally { beanFactoryWriter.commit(logger); } return beanFactoryFactory.getCreatedClassName(); }
From source file:de.csenk.gwt.commons.bean.rebind.observe.impl.ObservableBeanFactoryCreator.java
License:Apache License
/** * @param beanModel /*from w ww .j a v a 2 s . c o m*/ * */ private void writeObservableBean(ObservableBeanModel beanModel) { final String packageName = beanModel.getType().getPackage().getName(); final PrintWriter targetTypePrintWriter = context.tryCreate(logger, packageName, beanModel.getSimpleImplementationTypeName()); if (targetTypePrintWriter == null) { return; } final ClassSourceFileComposerFactory beanFactory = new ClassSourceFileComposerFactory(packageName, beanModel.getSimpleImplementationTypeName()); beanFactory.setSuperclass(String.format("%s<%s>", AbstractObservableBean.class.getSimpleName(), beanModel.getType().getQualifiedSourceName())); beanFactory.addImport(AbstractObservableBean.class.getCanonicalName()); beanFactory.addImport(AbstractObservableProperty.class.getCanonicalName()); beanFactory.addImport(PropertyValueChangeEvent.class.getCanonicalName()); final SourceWriter beanWriter = beanFactory.createSourceWriter(context, targetTypePrintWriter); try { beanWriter.println(); writeBeanShimField(beanWriter, beanModel); writeConstructor(beanWriter, beanModel); writeAsMethod(beanWriter, beanModel); } finally { beanWriter.commit(logger); } }
From source file:de.csenk.gwt.ws.rebind.filter.serialization.GWTSerializerGenerator.java
License:Open Source License
/** * @param serializerLogger/* w w w. jav a2s .c o m*/ * @param context * @param serializerInterface * @return */ private String createSerializer(TreeLogger serializerLogger, GeneratorContext context, JClassType serializerInterface) throws UnableToCompleteException { TypeOracle typeOracle = context.getTypeOracle(); SourceWriter sourceWriter = getSourceWriter(serializerLogger, context, serializerInterface); if (sourceWriter == null) return getImplementationQualifiedName(serializerInterface); // TODO gwtws: validate serializerInterface final PropertyOracle propertyOracle = context.getPropertyOracle(); // Load the blacklist/whitelist TypeFilter blacklistTypeFilter = new BlacklistTypeFilter(serializerLogger, propertyOracle); // Determine the set of serializable types SerializableTypeOracleBuilder typesSentFromBrowserBuilder = new SerializableTypeOracleBuilder( serializerLogger, propertyOracle, typeOracle); typesSentFromBrowserBuilder.setTypeFilter(blacklistTypeFilter); SerializableTypeOracleBuilder typesSentToBrowserBuilder = new SerializableTypeOracleBuilder( serializerLogger, propertyOracle, typeOracle); typesSentToBrowserBuilder.setTypeFilter(blacklistTypeFilter); addRoots(serializerLogger, typeOracle, typesSentFromBrowserBuilder, typesSentToBrowserBuilder, serializerInterface); // TODO gwtws: Type name ellision? // Create a resource file to receive all of the serialization // information // computed by STOB and mark it as private so it does not end up in the // output. OutputStream pathInfo = context.tryCreateResource(serializerLogger, serializerInterface.getQualifiedSourceName() + ".rpc.log"); PrintWriter writer = null; SerializableTypeOracle typesSentFromBrowser; SerializableTypeOracle typesSentToBrowser; try { writer = new PrintWriter(pathInfo); typesSentFromBrowserBuilder.setLogOutputStream(pathInfo); typesSentToBrowserBuilder.setLogOutputStream(pathInfo); writer.write("====================================\n"); writer.write("Types potentially sent from browser:\n"); writer.write("====================================\n\n"); writer.flush(); typesSentFromBrowser = typesSentFromBrowserBuilder.build(serializerLogger); writer.write("===================================\n"); writer.write("Types potentially sent from server:\n"); writer.write("===================================\n\n"); writer.flush(); typesSentToBrowser = typesSentToBrowserBuilder.build(serializerLogger); if (pathInfo != null) { context.commitResource(serializerLogger, pathInfo).setPrivate(true); } } finally { if (writer != null) { writer.close(); } } Map<JType, String> typeStrings = generateTypeHandlers(serializerLogger, context, typesSentFromBrowser, typesSentToBrowser, serializerInterface); String serializationPolicyStrongName = writeSerializationPolicyFile(serializerLogger, context, typesSentFromBrowser, typesSentToBrowser, typeStrings, serializerInterface); String serializerInterfaceName = TypeOracleMediator.computeBinaryClassName(serializerInterface); generateFields(sourceWriter, typesSentFromBrowser, serializationPolicyStrongName, serializerInterfaceName, serializerInterface); generateContructor(sourceWriter, serializerInterface, typeStrings); sourceWriter.commit(serializerLogger); return getImplementationQualifiedName(serializerInterface); }
From source file:de.knightsoftnet.navigation.rebind.VersionInfoGenerator.java
License:Apache License
@Override public final String generate(final TreeLogger plogger, final GeneratorContext pcontext, final String ptypeName) throws UnableToCompleteException { try {//from w ww. ja va 2 s .c o m final JClassType classType = pcontext.getTypeOracle().getType(ptypeName); // Here you would retrieve the metadata based on typeName for this class final SourceWriter src = this.getSourceWriter(classType, pcontext, plogger); if (src != null) { final ResourceBundle bundle = ResourceBundle.getBundle("Version"); final String version = bundle.getString("application.version"); final String buildTimeString = bundle.getString("build.timestamp"); final String copyright = bundle.getString("application.copyright"); final String author = bundle.getString("application.author"); src.println("@Override"); src.println("public final SafeHtml getCopyrightText() {"); src.println(" return SafeHtmlUtils.fromString(\" " + copyright + "\");"); src.println("}"); src.println("@Override"); src.println("public final SafeHtml getVersionNumber() {"); src.println(" return SafeHtmlUtils.fromString(\"" + version + "\");"); src.println("}"); src.println("@Override"); src.println("public final SafeHtml getVersionDate() {"); src.println( " return SafeHtmlUtils.fromString(this.parseAndFormatDate(\"" + buildTimeString + "\"));"); src.println("}"); src.println("@Override"); src.println("public final SafeHtml getAuthor() {"); src.println(" return SafeHtmlUtils.fromString(\"" + author + "\");"); src.println("}"); src.commit(plogger); System.out.println("Generating for: " + ptypeName); } return ptypeName + "Generated"; } catch (final NotFoundException e) { e.printStackTrace(); } return null; }
From source file:de.knightsoftnet.validators.rebind.AbstractCreator.java
License:Apache License
/** * create logger./* www .j a v a 2 s.c o m*/ * * @return qualified name * @throws UnableToCompleteException if fails */ public final String create() throws UnableToCompleteException { final SourceWriter sourceWriter = this.getSourceWriter(this.logger, this.context); if (sourceWriter != null) { this.writeClassBody(sourceWriter); sourceWriter.commit(this.logger); } return this.getQualifiedName(); }
From source file:de.knightsoftnet.validators.rebind.BeanHelperCache.java
License:Apache License
/** * Write an Empty Interface implementing * {@link de.knightsoftnet.validators.client.impl.GwtSpecificValidator} with Generic parameter of * the bean type./*from w w w . j a v a2 s. c om*/ */ private void writeInterface(final GeneratorContext context, final TreeLogger logger, final BeanHelper bean) { final PrintWriter pw = context.tryCreate(logger, bean.getPackage(), bean.getValidatorName()); if (pw != null) { final TreeLogger interfaceLogger = logger.branch(TreeLogger.TRACE, "Creating the interface for " + bean.getFullyQualifiedValidatorName()); final ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(bean.getPackage(), bean.getValidatorName()); factory.addImplementedInterface( GwtSpecificValidator.class.getCanonicalName() + " <" + bean.getTypeCanonicalName() + ">"); factory.addImport(GWT.class.getCanonicalName()); factory.makeInterface(); final SourceWriter sw = factory.createSourceWriter(context, pw); // static MyValidator INSTANCE = GWT.create(MyValidator.class); sw.print("static "); sw.print(bean.getValidatorName()); sw.print(" INSTANCE = GWT.create("); sw.print(bean.getValidatorName()); sw.println(".class);"); sw.commit(interfaceLogger); pw.close(); } }