List of usage examples for com.google.gwt.user.rebind SourceWriter outdent
void outdent();
From source file:org.jboss.as.console.rebind.forms.ApplicationMetaDataGenerator.java
License:Open Source License
private void generateMethodMethods(SourceWriter sourceWriter) { try {//from www. jav a2s . c om Class<?> beanFactoryClass = getClass().getClassLoader().loadClass(BEAN_FACTORY_NAME); int idx = 0; for (Method method : beanFactoryClass.getMethods()) { Type returnType = method.getGenericReturnType(); if (returnType instanceof ParameterizedType) { ParameterizedType type = (ParameterizedType) returnType; Type[] typeArguments = type.getActualTypeArguments(); if (typeArguments[0] instanceof Class) { Class beanTypeClass = (Class) typeArguments[0]; //sourceWriter.println("public void " + beanTypeClass.getSimpleName() + "_" + idx + "() {"); sourceWriter.println("public void " + beanTypeClass.getSimpleName() + "() {"); sourceWriter.indent(); sourceWriter.println("Class<?> listType = null;"); sourceWriter.println("String label = \"\";"); sourceWriter.println("String subgroup = \"\";"); sourceWriter.println("String tabName = \"\";"); sourceWriter.println("String[] acceptedValues = null;"); sourceWriter.println(""); sourceWriter.println("registry.put(" + beanTypeClass.getName() + ".class, new ArrayList<PropertyBinding>());"); // -------------------------------- // Mutator sourceWriter.println( "Mutator mut_" + idx + " = new Mutator<" + beanTypeClass.getName() + ">();"); sourceWriter .println("mutators.put(" + beanTypeClass.getName() + ".class , mut_" + idx + ");"); // ----------------------------- // PropertyBinding List<PropBindingDeclarations> bindings = mapProperties(beanTypeClass); for (PropBindingDeclarations binding : bindings) { BindingDeclaration bindDecl = binding.getBindingDeclaration(); FormItemDeclaration formDecl = binding.getFormItemDeclaration(); if (bindDecl.skip()) continue; if (!bindDecl.listType().equals("")) { sourceWriter.println("listType = " + bindDecl.listType() + ".class;"); } if (formDecl.localLabel().equals("")) { sourceWriter.println("label = \"" + formDecl.label() + "\";"); } else { sourceWriter.println("label = Console.CONSTANTS." + formDecl.localLabel() + "();"); } if (!"".equals(formDecl.subgroup())) { sourceWriter.println("subgroup = \"" + formDecl.subgroup() + "\";"); } else { sourceWriter.println("subgroup = \"\";"); } if ("CUSTOM".equals(formDecl.tabName())) sourceWriter.println("tabName = \"CUSTOM\";"); // will be set programmatically else if ("".equals(formDecl.localTabName())) sourceWriter.println("tabName = \"" + formDecl.tabName() + "\";"); else sourceWriter .println("tabName = Console.CONSTANTS." + formDecl.localTabName() + "();"); sourceWriter.println( "acceptedValues = " + makeStringArrayString(formDecl.acceptedValues()) + ";"); sourceWriter.println("registry.get(" + beanTypeClass.getName() + ".class).add("); sourceWriter.indent(); sourceWriter.println("new PropertyBinding(\"" + bindDecl.getJavaName() + "\", \"" + bindDecl.getDetypedName() + "\", \"" + bindDecl.getJavaTypeName() + "\", listType, this, " + bindDecl.key() + ", " + bindDecl.expr() + ", " + bindDecl.writeUndefined() + ", \"" + formDecl.defaultValue() + "\", label, " + formDecl.required() + ", \"" + formDecl.formItemTypeForEdit() + "\", \"" + formDecl.formItemTypeForAdd() + "\", subgroup, tabName, " + formDecl.order() + ", acceptedValues)"); sourceWriter.outdent(); sourceWriter.println(");"); // create and register setters sourceWriter.println("mut_" + idx + ".register(\"" + bindDecl.getJavaName() + "\", new Setter<" + beanTypeClass.getName() + ">() {\n" + "public void invoke(" + bindDecl.getBeanClassName() + " entity, Object value) {\n" + "entity.set" + bindDecl.getPropertyName() + "((" + bindDecl.getJavaTypeName() + ")value);\n" + "}\n" + "});\n"); // create and register getters String prefix = "get"; if (bindDecl.getJavaTypeName().equals("java.lang.Boolean")) prefix = "is"; sourceWriter.println( "mut_" + idx + ".register(\"" + bindDecl.getJavaName() + "\", new Getter<" + beanTypeClass.getName() + ">() {\n" + "public Object invoke(" + bindDecl.getBeanClassName() + " entity) {\n" + " return entity." + prefix + bindDecl.getPropertyName() + "();\n" + "}\n" + "});\n"); } // ----------------------------- // AddressBinding AddressDeclaration addr = parseAddress(beanTypeClass); sourceWriter.println("AddressBinding addr_" + idx + " = new AddressBinding();"); sourceWriter.println( "addressing.put(" + beanTypeClass.getName() + ".class , addr_" + idx + ");"); for (String[] token : addr.getAddress()) { sourceWriter .println("addr_" + idx + ".add(\"" + token[0] + "\", \"" + token[1] + "\");"); } // ----------------------------- // Factory lookup sourceWriter.println("factories.put(" + beanTypeClass.getName() + ".class, new EntityFactory<" + beanTypeClass.getName() + ">() {\n" + "public " + beanTypeClass.getName() + " create() {\n" + "return beanFactory." + method.getName() + "().as();\n" + "}\n" + "});\n"); sourceWriter.println("// ---- End " + beanTypeClass.getName() + " ----"); sourceWriter.outdent(); sourceWriter.println("}"); sourceWriter.println(""); } } idx++; } } catch (ClassNotFoundException e) { throw new RuntimeException("Failed to load " + BEAN_FACTORY_NAME); } }
From source file:org.jboss.as.console.rebind.ProductConfigGenerator.java
License:Open Source License
/** * Generate source code for new class. Class extends * <code>HashMap</code>./*from w w w . j a v a2 s . c o m*/ * * @param logger Logger object * @param context Generator context */ private void generateClass(TreeLogger logger, GeneratorContext context) throws Throwable { // get print writer that receives the source code PrintWriter printWriter = context.tryCreate(logger, packageName, className); // print writer if null, source code has ALREADY been generated, return if (printWriter == null) return; // init composer, set class properties, create source writer ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName, className); // Imports composerFactory.addImport("org.jboss.as.console.client.Console"); composerFactory.addImport("org.jboss.as.console.client.ProductConfig"); composerFactory.addImport("java.util.*"); // Interfaces composerFactory.addImplementedInterface("org.jboss.as.console.client.ProductConfig"); // SourceWriter SourceWriter sourceWriter = composerFactory.createSourceWriter(context, printWriter); // fields generateFields(sourceWriter); // ctor generateConstructor(logger, context, sourceWriter); // Methods generateMethods(sourceWriter, context); // close generated class sourceWriter.outdent(); sourceWriter.println("}"); // commit generated class context.commit(logger, printWriter); }
From source file:org.jboss.as.console.rebind.ProductConfigGenerator.java
License:Open Source License
private void generateConstructor(TreeLogger logger, GeneratorContext context, SourceWriter sourceWriter) { // start constructor source generation sourceWriter.println("public " + className + "() { "); sourceWriter.indent();/*from ww w . j a v a2 s .com*/ sourceWriter.println("super();"); // TODO sourceWriter.outdent(); sourceWriter.println("}"); }
From source file:org.jboss.as.console.rebind.ProductConfigGenerator.java
License:Open Source License
private void generateMethods(SourceWriter sourceWriter, GeneratorContext context) throws Throwable { PropertyOracle propertyOracle = context.getPropertyOracle(); String consoleProfileProperty = propertyOracle.getConfigurationProperty("console.profile").getValues() .get(0);/*from w ww .java 2 s. c o m*/ if (null == consoleProfileProperty) throw new BadPropertyValueException("Missing configuration property 'console.profile'!"); String prodVersionProperty = propertyOracle.getConfigurationProperty("console.product.version").getValues() .get(0); String consoleProductVersion = (prodVersionProperty != null) ? prodVersionProperty : ""; String devHostProperty = propertyOracle.getConfigurationProperty("console.dev.host").getValues().get(0); String consoleDevHost = (devHostProperty != null) ? devHostProperty : "127.0.0.1"; // most of the config attributes are by default empty // they need be overriden by custom gwt.xml descriptor on a project/product level sourceWriter.println("public String getProductTitle() { "); sourceWriter.indent(); sourceWriter.println("return \"\";"); sourceWriter.outdent(); sourceWriter.println("}"); sourceWriter.println("public String getProductVersion() { "); sourceWriter.indent(); sourceWriter.println("return \"" + consoleProductVersion + "\";"); sourceWriter.outdent(); sourceWriter.println("}"); sourceWriter.println("public String getCoreVersion() { "); sourceWriter.indent(); sourceWriter.println("return org.jboss.as.console.client.Build.VERSION;"); sourceWriter.outdent(); sourceWriter.println("}"); sourceWriter.println("public String getDevHost() { "); sourceWriter.indent(); sourceWriter.println("return \"" + devHostProperty + "\";"); sourceWriter.outdent(); sourceWriter.println("}"); sourceWriter.println("public ProductConfig.Profile getProfile() { "); sourceWriter.indent(); if ("eap".equals(consoleProfileProperty)) sourceWriter.println("return ProductConfig.Profile.EAP;"); else sourceWriter.println("return ProductConfig.Profile.JBOSS;"); sourceWriter.outdent(); sourceWriter.println("}"); }
From source file:org.jboss.bpm.console.rebind.WorkspaceGenerator.java
License:Open Source License
/** * Generate source code for new class. Class extends * <code>HashMap</code>./*from w ww. ja v a2 s . c o m*/ * * @param logger Logger object * @param context Generator context */ private void generateClass(TreeLogger logger, GeneratorContext context) { // get print writer that receives the source code PrintWriter printWriter = null; printWriter = context.tryCreate(logger, packageName, className); // print writer if null, source code has ALREADY been generated, return if (printWriter == null) return; // init composer, set class properties, create source writer ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName, className); // Imports //composerFactory.addImport("org.jboss.bpm.console.client.ApplicationContext"); composerFactory.addImport("org.jboss.bpm.console.client.Workspace"); composerFactory.addImport("org.jboss.bpm.console.client.WorkspaceLauncher"); // Interfaces composerFactory.addImplementedInterface("org.jboss.bpm.console.client.WorkspaceLauncher"); // SourceWriter SourceWriter sourceWriter = composerFactory.createSourceWriter(context, printWriter); // Methods generateMethods(sourceWriter); // close generated class sourceWriter.outdent(); sourceWriter.println("}"); // commit generated class context.commit(logger, printWriter); }
From source file:org.jboss.bpm.console.rebind.WorkspaceGenerator.java
License:Open Source License
private void generateMethods(SourceWriter sourceWriter) { // start constructor source generation sourceWriter.println("public void launch(Workspace workspace) { "); sourceWriter.indent();/*from ww w .jav a2s.c om*/ InputStream in = getClass().getClassLoader().getResourceAsStream(WORKSPACE_CONFIG); if (null == in) { in = getClass().getClassLoader().getResourceAsStream(WORKSPACE_CONFIG_DEFAULT); if (null == in) throw new RuntimeException( "Cannot find '" + WORKSPACE_CONFIG + "' or '" + WORKSPACE_CONFIG_DEFAULT + "'"); } try { //use buffering, reading one line at a time //FileReader always assumes default encoding is OK! BufferedReader input = new BufferedReader(new InputStreamReader(in)); try { String line = null; while ((line = input.readLine()) != null) { // ignore comments and empty lines if (line.equals("") || line.startsWith("#")) continue; sourceWriter.println("workspace.addEditor( new " + line + "(), false );"); } } finally { input.close(); } } catch (IOException ex) { throw new RuntimeException("Error reading '" + WORKSPACE_CONFIG + "'"); } // end constructor source generation sourceWriter.outdent(); sourceWriter.println("}"); }
From source file:org.jboss.errai.bus.rebind.ExtensionProxyGenerator.java
License:Apache License
/** * Generate source code for new class. Class extends * <code>HashMap</code>./* w w w . j a va 2s. co m*/ * * @param logger Logger object * @param context Generator context */ private void generateClass(TreeLogger logger, GeneratorContext context) { // get print writer that receives the source code PrintWriter printWriter = context.tryCreate(logger, packageName, className); // print writer if null, source code has ALREADY been generated, if (printWriter == null) return; // init composer, set class properties, create source writer ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, className); composer.addImplementedInterface(ExtensionsLoader.class.getName()); composer.addImport(JSONValue.class.getName()); composer.addImport(JSONString.class.getName()); composer.addImport(JSONNumber.class.getName()); composer.addImport(JSONBoolean.class.getName()); composer.addImport(JSONObject.class.getName()); SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter); // generator constructor source code generateExtensions(context, logger, sourceWriter); // close generated class sourceWriter.outdent(); sourceWriter.println("}"); // commit generated class context.commit(logger, printWriter); }
From source file:org.jboss.errai.bus.rebind.ExtensionProxyGenerator.java
License:Apache License
private void generateExtensions(GeneratorContext context, TreeLogger logger, SourceWriter sourceWriter) { // start constructor source generation sourceWriter.println("public " + className + "() { "); sourceWriter.indent();//from w w w . j ava 2 s.c o m sourceWriter.println("super();"); sourceWriter.outdent(); sourceWriter.println("}"); sourceWriter.println("public void initExtensions(final " + MessageBus.class.getName() + " bus) { "); sourceWriter.outdent(); MetaDataScanner scanner = ScannerSingleton.getOrCreateInstance(); new BusClientConfigGenerator().generate(context, logger, sourceWriter, scanner, typeOracle); Set<Class<?>> extensionComponents = scanner.getTypesAnnotatedWith(ExtensionComponent.class); for (Class<?> cls : extensionComponents) { if (ExtensionGenerator.class.isAssignableFrom(cls)) { try { ExtensionGenerator generator = cls.asSubclass(ExtensionGenerator.class).newInstance(); generator.generate(context, logger, sourceWriter, scanner, typeOracle); } catch (Exception e) { throw new RuntimeException("Could not load extension generator: " + cls.getName(), e); } } } // end constructor source generation sourceWriter.outdent(); sourceWriter.println("}"); }
From source file:org.jboss.errai.widgets.rebind.WidgetMappingsGenerator.java
License:Apache License
private void generateClass(TreeLogger logger, GeneratorContext context) { // get print writer that receives the source code PrintWriter printWriter = context.tryCreate(logger, packageName, className); // print writer if null, source code has ALREADY been generated, if (printWriter == null) { return;// ww w . j a v a 2 s. c o m } // init composer, set class properties, create source writer ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, className); composer.addImplementedInterface(typeName); composer.addImport(Widget.class.getName()); SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter); // generator constructor source code generateExtensions(context, logger, sourceWriter); // close generated class sourceWriter.outdent(); sourceWriter.println("}"); // commit generated class context.commit(logger, printWriter); }
From source file:org.jboss.errai.widgets.rebind.WidgetMappingsGenerator.java
License:Apache License
private void generateExtensions(GeneratorContext context, TreeLogger logger, SourceWriter sourceWriter) { // start constructor source generation sourceWriter.println("public " + className + "() { "); sourceWriter.println("}"); sourceWriter.println("public void mapAll(final " + strTargetType + " widget) { "); sourceWriter.outdent(); try {//from w ww. j a v a 2 s .co m JClassType widgetMapper = typeOracle.getType(CollectionWidgetMapper.class.getName()); for (JField currField : targetClass.getFields()) { if (currField.isAnnotationPresent(WidgetMapper.class) && widgetMapper.isAssignableFrom(currField.getType().isClassOrInterface())) { WidgetMapper mf = currField.getAnnotation(WidgetMapper.class); JField widgetField = targetClass.getField(mf.value()); String varName = widgetField.getName() + "Mapper"; JClassType binderField = currField.getType().isClassOrInterface(); JParameterizedType pType = binderField.isParameterized(); if (pType == null) { RuntimeException e = new RuntimeException( "Field '" + currField.getName() + "' must be parameterized"); logger.log(TreeLogger.Type.ERROR, e.getMessage(), e); throw e; } // The last type arg shall always be our domain object type per this spec. JClassType jEntityTarget = pType.getTypeArgs()[pType.getTypeArgs().length - 1]; String strTypeParms = generateTypeParmString(pType); List<JField> fieldsToMap = new LinkedList<JField>(); /** * If an EntityFields annotatio is present, then we discriminate on those fields. Otherwise * we capture all fields by default. */ if (currField.isAnnotationPresent(EntityFields.class)) { EntityFields ef = currField.getAnnotation(EntityFields.class); for (String fieldName : ef.value()) { JField fld = jEntityTarget.getField(fieldName); if (fld == null) { RuntimeException e = new RuntimeException("no such field in entity class '" + jEntityTarget.getName() + "': " + fieldName); logger.log(TreeLogger.Type.ERROR, e.getMessage(), e); throw e; } fieldsToMap.add(jEntityTarget.getField(fieldName)); } } else { for (JField fld : jEntityTarget.getFields()) { if (fld.getEnclosingType().equals(jEntityTarget)) { fieldsToMap.add(fld); } } } List<String> generatedInitializations = new LinkedList<String>(); List<String> generatedBindings = new LinkedList<String>(); FieldMapperGenerator g = getFieldMapper(widgetField.getType().getQualifiedSourceName()); generatedInitializations.add( g.init(typeOracle, widgetField, jEntityTarget, currField, null, varName, fieldsToMap)); if (g == null) { throw new RuntimeException( "Cannot generateGetField mapper for widget: " + jEntityTarget.getName()); } for (JField fld : fieldsToMap) { String fieldName = fld.getName(); JField targetField = jEntityTarget.getField(fieldName); if (targetField == null) { throw new RuntimeException( "The field '" + fieldName + "' does not correspond with a field in the class: " + jEntityTarget.getQualifiedSourceName()); } generatedBindings.add(g.generateFieldMapperGenerator(typeOracle, widgetField, jEntityTarget, null, targetField)); } Map<String, Object> vars = new HashMap<String, Object>(); vars.put("typeOracle", typeOracle); vars.put("variableName", varName); vars.put("strTypeParms", strTypeParms); vars.put("targetWidget", widgetField.getType().getQualifiedSourceName()); vars.put("targetType", jEntityTarget.getQualifiedSourceName()); vars.put("initializers", generatedInitializations); vars.put("bindings", generatedBindings); vars.put("targetFieldName", widgetField.getName()); String s = (String) TemplateRuntime.execute(mappingsGen, vars); sourceWriter.print(s); s = "widget." + currField.getName() + " = " + varName + ";"; sourceWriter.println(s); } else if (currField.isAnnotationPresent(EntityMapped.class)) { EntityMapped entityMappedA = currField.getAnnotation(EntityMapped.class); JClassType entityType = currField.getType().isClassOrInterface(); String varName = currField.getName() + "Mapper"; String entityFieldName = currField.getName(); String toEntityField; Map<String, List<JField>> toBeMapped = new HashMap<String, List<JField>>(); for (JField fld : targetClass.getFields()) { if (fld.isAnnotationPresent(MapField.class)) { MapField mapFieldA = fld.getAnnotation(MapField.class); toEntityField = "".equals(mapFieldA.value()) ? entityFieldName : mapFieldA.value(); if (!toBeMapped.containsKey(toEntityField)) { toBeMapped.put(toEntityField, new LinkedList<JField>()); } toBeMapped.get(toEntityField).add(fld); } } /** * Generate the field mappings. */ for (Map.Entry<String, List<JField>> entry : toBeMapped.entrySet()) { List<String> generatedInitializations = new LinkedList<String>(); List<String> generatedBindings = new LinkedList<String>(); ArrayList<String[]> fieldIndexPositions = new ArrayList<String[]>(); for (JField fld : entry.getValue()) { JClassType classType = fld.getType().isClassOrInterface(); String fieldName = fld.getAnnotation(MapField.class).value(); if ("".equals(fieldName)) { fieldName = fld.getName(); } JField targetField = entityType.getField(fieldName); if (targetField == null) { throw new RuntimeException("The field '" + fieldName + "' does not correspond with a field in the class: " + entityType.getQualifiedSourceName()); } JClassType targetFieldType = targetField.getType().isClassOrInterface(); FieldMapperGenerator g = getFieldMapper(classType.getQualifiedSourceName()); if (g == null) { throw new RuntimeException( "Cannot generateGetField mapper for widget: " + classType.getName()); } generatedInitializations .add(g.init(typeOracle, fld, entityType, targetField, currField, null, null)); generatedBindings.add(g.generateFieldMapperGenerator(typeOracle, fld, entityType, targetField, currField)); if (getType(typeOracle, Widget.class).isAssignableFrom(classType)) { fieldIndexPositions .add(new String[] { fieldName, g.generateValueExtractorStatement(typeOracle, fld, entityType, targetField, currField) }); } } Map<String, Object> vars = new HashMap<String, Object>(); vars.put("typeOracle", typeOracle); vars.put("variableName", varName); vars.put("initializers", generatedInitializations); vars.put("targetFieldName", entityFieldName); vars.put("bindings", generatedBindings); vars.put("fieldIndexPositions", fieldIndexPositions); vars.put("entityFieldName", entityFieldName); String s = (String) TemplateRuntime.execute(entityMappingGen, vars); sourceWriter.print(s); } } else if (currField.isAnnotationPresent(AddAllTo.class)) { String copyToField = currField.getAnnotation(AddAllTo.class).value(); String copyFromField = currField.getName(); Map<String, Object> vars = new HashMap<String, Object>(); vars.put("copyToField", copyToField); vars.put("copyFromField", copyFromField); String s = (String) TemplateRuntime.execute(addAllToGen, vars); sourceWriter.print(s); } } } catch (Exception e) { logger.log(TreeLogger.Type.ERROR, "failed to map field (does not exist)", e); e.printStackTrace(); } // end constructor source generation sourceWriter.outdent(); sourceWriter.println("}"); }