List of usage examples for com.google.gwt.user.rebind SourceWriter outdent
void outdent();
From source file:org.example.modular_mvp.base.rebind.ExtensionManagerGenerator.java
License:Open Source License
private void generateClass(TreeLogger logger, GeneratorContext context, String packageName, String className, List<ExtensionDefinition> extensions) { PrintWriter pw = context.tryCreate(logger, packageName, className); if (pw == null) { return;/* w ww .ja va 2 s . co m*/ } ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName, className); // imports composerFactory.addImport(ArrayList.class.getCanonicalName()); composerFactory.addImport(List.class.getCanonicalName()); composerFactory.addImport(ExtensionDefinition.class.getCanonicalName()); composerFactory.addImport(ExtensionManager.class.getCanonicalName()); // interface composerFactory.addImplementedInterface(ExtensionManager.class.getCanonicalName()); SourceWriter sw = composerFactory.createSourceWriter(context, pw); // begin class definition sw.indent(); // fields sw.println("private final List<NavigatorItem> items = new ArrayList<NavigatorItem>();"); // constructor sw.println("public " + className + "() {"); sw.indent(); for (ExtensionDefinition extension : extensions) { for (ExtensionDefinition.NavigatorItem item : extension.value()) { sw.println("items.add(new NavigatorItem(\"%s\", \"%s\"));", escape(item.name()), escape(item.token())); } } sw.outdent(); sw.println("}"); // methods // getNavigatorItems sw.println("public List<NavigatorItem> getNavigatorItems() {"); sw.indentln("return items;"); sw.println("}"); // close it out sw.outdent(); sw.println("}"); context.commit(logger, pw); }
From source file:org.gwt.beansbinding.core.rebind.BeanPropertyDescriptorGenerator.java
License:Apache License
private void write(SourceWriter w, JClassType _type, JClassType[] types) { w.println("public static void setupBeanInfo() throws " + IntrospectionException.class.getName()); w.println("{"); w.indent();//from w w w . j a v a 2 s .c o m w.println("GwtBeanInfo beanInfo;"); for (JClassType type : types) { Collection<Property> properties = lookupJavaBeanPropertyAccessors(type); w.println("\n// " + type.getName()); w.println("beanInfo = new GwtBeanInfo();"); for (Property property : properties) { w.print("beanInfo.addPropertyDescriptor( "); writePropertyDescriptor(w, type, property.name, property.propertyType, property.getter, property.setter); w.println(" );"); } w.println("GwtIntrospector.setBeanInfo( " + type.getQualifiedSourceName() + ".class, beanInfo );"); } w.outdent(); w.println("}"); // automatically invoke setupBeanInfo() w.println("static {"); w.indent(); w.println( "try{setupBeanInfo();}catch(Exception ex){com.google.gwt.user.client.Window.alert(ex.getMessage());}"); w.outdent(); w.println("}"); }
From source file:org.gwt.beansbinding.core.rebind.BeanPropertyDescriptorGenerator.java
License:Apache License
private void writePropertyDescriptor(SourceWriter sw, JClassType type, String propertyName, String propertyType, JMethod getter, JMethod setter) { sw.print("new PropertyDescriptor( \"" + propertyName + "\", " + propertyType + ".class, "); if (getter != null) { sw.println("new Method() "); sw.println("{"); sw.indent();// ww w.j a va 2 s .c o m sw.println("public Object invoke( Object bean, Object... args )"); sw.println("{"); sw.indent(); sw.println("return ( (" + type.getQualifiedSourceName() + ") bean)." + getter.getName() + "();"); sw.outdent(); sw.println("}"); sw.outdent(); sw.print("}, "); } else { sw.print("null, "); } if (setter != null) { sw.println("new Method() "); sw.println("{"); sw.indent(); sw.println("public Object invoke( Object bean, Object... args )"); sw.println("{"); sw.indent(); JType argType = setter.getParameters()[0].getType().getErasedType(); String argTypeName; if (argType.isPrimitive() != null) { argTypeName = argType.isPrimitive().getQualifiedBoxedSourceName(); } else { argTypeName = argType.getQualifiedSourceName(); } sw.println("( (" + type.getQualifiedSourceName() + ") bean)." + setter.getName() + "( (" + argTypeName + ") args[0] );"); sw.println("return null;"); sw.outdent(); sw.println("}"); sw.outdent(); sw.print("} )"); } else { sw.print("null )"); } }
From source file:org.gwt.json.serialization.InternalArraySerializerGenerator.java
License:Apache License
public String generateSerializers(SourceWriter writer) throws NotFoundException { String serializerName = jArrayType.getQualifiedSourceName().replaceAll("\\.", "\\$").replaceAll("\\[\\]", "_1") + "_SerializableImpl"; writer.println("public class " + serializerName + " extends JsonSimpleTypeSerializer<" + jArrayType.getQualifiedSourceName() + "> { "); writer.indent();/*from w w w. jav a2 s .c o m*/ writer.println("public " + serializerName + "(){}"); writer.println("public JSONValue serialize(" + jArrayType.getQualifiedSourceName() + " obj) {"); writer.indent(); writer.println("if (obj == null) return JSONNull.getInstance();"); writer.println("JSONArray jsonArray = new JSONArray();"); writer.println("for (int i=0; i< obj.length; i++) {"); writer.indent(); JType componentType = jArrayType.getComponentType(); String fieldType = GenericGenerationUtils.resolveType(componentType); if (componentType.isArray() != null) { writer.println( "jsonArray.set(i, getSerializerForType(\"" + fieldType + "\").serialize(obj[i], null));"); registerArraySerializer(componentType.isArray()); } else { JParameterizedType parametrizedType = componentType.isParameterized(); writer.println("jsonArray.set(i, getSerializerForType(\"" + fieldType + "\").serialize(obj[i], " + GenericGenerationUtils.getGenericTypesParam(componentType, parametrizedType != null ? parametrizedType.getTypeArgs() : null) + "));"); } writer.outdent(); writer.println("}"); writer.println("return jsonArray;"); writer.outdent(); writer.println("} "); writer.println(); writer.println("public " + jArrayType.getQualifiedSourceName() + " deSerialize(JSONValue jsonObj) throws JSONException {"); writer.indent(); writer.println("if (jsonObj == null) return null;"); writer.println("JSONArray arr = jsonObj.isArray();"); writer.println(jArrayType.getQualifiedSourceName() + " response = new " + insertAsFirstArray(jArrayType.getComponentType().getQualifiedSourceName(), "[arr.size()]") + ";"); writer.println("JsonGenericTypeSerializer<" + fieldType + "> serializer = getSerializerForType(\"" + fieldType + "\");"); writer.println("for (int i=0; i<arr.size(); i++) {"); writer.indent(); if (componentType.isArray() != null) { writer.print("response[i] = serializer.deSerialize(arr.get(i), null);"); registerArraySerializer(componentType.isArray()); } else { JParameterizedType parametrizedType = componentType.isParameterized(); writer.print("response[i] = serializer.deSerialize(arr.get(i), " + GenericGenerationUtils.getGenericTypesParam(componentType, parametrizedType != null ? parametrizedType.getTypeArgs() : null) + ");"); } writer.outdent(); writer.println("}"); writer.println("return response;"); writer.outdent(); writer.println("}"); writer.outdent(); writer.println("}"); return serializerName; }
From source file:org.gwt.json.serialization.InternalSerializerGenerator.java
License:Apache License
public String generateSerializers(SourceWriter writer) throws NotFoundException { Set<JField> candidates = AccessorResolveUtil.resolveFieldCandidates(jClassType); List<FieldInfo> resolvedFields = new ArrayList<FieldInfo>(); for (JField jField : candidates) { resolvedFields.add(AccessorResolveUtil.resolveAccessor(jField, jClassType)); }/*from w w w . j a v a 2s. co m*/ String serializerName = jClassType.getName().replaceAll("\\.", "\\$") + "_SerializableImpl"; writer.println("public class " + serializerName + " extends JsonSimpleTypeSerializer<" + jClassType.getQualifiedSourceName() + "> { "); writer.indent(); writer.println("public " + serializerName + "(){}"); writer.println("public JSONValue serialize(" + jClassType.getQualifiedSourceName() + " obj) {"); writer.indent(); writer.println("JSONObject jsonObj = new JSONObject();"); writer.println("String type;"); writer.println("String fieldName;"); for (FieldInfo fieldInfo : resolvedFields) { JParameterizedType parametrizedType = fieldInfo.getjField().getType().isParameterized(); generateGenericTypeSerializer(fieldInfo, parametrizedType != null ? parametrizedType.getTypeArgs() : null, writer); } writer.println("return jsonObj;"); writer.outdent(); writer.println("} "); writer.println(); writer.println("public " + jClassType.getQualifiedSourceName() + " deSerialize(JSONValue jsonObj) throws JSONException {"); writer.indent(); writer.println("JSONObject obj = jsonObj.isObject();"); writer.println(jClassType.getQualifiedSourceName() + " response = new " + jClassType.getQualifiedSourceName() + "();"); writer.println("String type;"); writer.println("String fieldName;"); for (FieldInfo fieldInfo : resolvedFields) { JParameterizedType parametrizedType = fieldInfo.getjField().getType().isParameterized(); generateGenericTypeDeSerializer(fieldInfo, parametrizedType != null ? parametrizedType.getTypeArgs() : null, writer); } writer.println("return response;"); writer.outdent(); writer.println("}"); writer.outdent(); writer.println("}"); return serializerName; }
From source file:org.gwt.json.serialization.SerializerGenerator.java
License:Apache License
protected void generateRegisterConstructor(Map<String, String> serializers, String simpleName, SourceWriter writer, TypeOracle typeOracle) { writer.println("public " + simpleName + "() {"); writer.indent();//from w w w .j a v a2 s .c om for (Map.Entry<String, String> entry : serializers.entrySet()) { writer.println("registerSerializer(\"" + entry.getKey() + "\", new " + entry.getValue() + "(" + constructorArgsForType(entry.getValue(), typeOracle) + "));"); } writer.outdent(); writer.println("}"); }
From source file:org.jboss.as.console.rebind.extensions.SubsystemExtensionProcessorGenerator.java
License:Apache License
private void generateClass(TreeLogger logger, GeneratorContext context, String packageName, String className, List<SubsystemExtension> subsystemExtensions) { PrintWriter pw = context.tryCreate(logger, packageName, className); if (pw == null) { return;//w ww . j a v a 2s . c om } ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName, className); // imports composerFactory.addImport(ArrayList.class.getCanonicalName()); composerFactory.addImport(LinkedHashMap.class.getCanonicalName()); composerFactory.addImport(List.class.getCanonicalName()); composerFactory.addImport(Map.class.getCanonicalName()); composerFactory.addImport(SubsystemExtensionProcessor.class.getCanonicalName()); composerFactory.addImport(SubsystemGroup.class.getCanonicalName()); composerFactory.addImport(SubsystemGroupItem.class.getCanonicalName()); composerFactory.addImport(Predicate.class.getCanonicalName()); composerFactory.addImport(LHSNavTreeItem.class.getCanonicalName()); // interface composerFactory.addImplementedInterface(SubsystemExtensionProcessor.class.getCanonicalName()); SourceWriter sw = composerFactory.createSourceWriter(context, pw); // begin class definition sw.indent(); // fields sw.println( "private final Map<String, SubsystemGroup> extensionGroups = new LinkedHashMap<String, SubsystemGroup>();"); sw.println("private final List<Predicate> _runtimeMetricsExtensions = new ArrayList<Predicate>();"); sw.println("private final List<Predicate> _runtimeOperationsExtensions = new ArrayList<Predicate>();"); // constructor sw.println("public " + className + "() {"); sw.indent(); for (SubsystemExtension extension : subsystemExtensions) { for (SubsystemGroupDefinition groupDef : extension.groups()) { sw.println("SubsystemGroup group;"); sw.println("if (extensionGroups.containsKey(\"%s\")) {", groupDef.name()); sw.indentln("group = extensionGroups.get(\"%s\");", groupDef.name()); sw.println("} else {"); sw.indent(); sw.println("group = new SubsystemGroup(\"%s\");", groupDef.name()); sw.println("extensionGroups.put(group.getName(), group);"); sw.outdent(); sw.println("}"); for (SubsystemItemDefinition itemDef : groupDef.items()) { sw.println("group.getItems().add(new SubsystemGroupItem(\"%s\", \"%s\", \"%s\"));", itemDef.name(), extension.subsystem(), itemDef.presenter()); } } for (SubsystemItemDefinition runtimeItemDef : extension.metrics()) { sw.println( "_runtimeMetricsExtensions.add(new Predicate(\"%s\", new LHSNavTreeItem(\"%s\", \"%s\")));", extension.subsystem(), runtimeItemDef.name(), runtimeItemDef.presenter()); } for (SubsystemItemDefinition runtimeItemDef : extension.runtime()) { sw.println( "_runtimeOperationsExtensions.add(new Predicate(\"%s\", new LHSNavTreeItem(\"%s\", \"%s\")));", extension.subsystem(), runtimeItemDef.name(), runtimeItemDef.presenter()); } } sw.outdent(); sw.println("}"); // methods // processProfileExtensions sw.println("public void processProfileExtensions(Map<String, SubsystemGroup> groups) {"); sw.indent(); sw.println("for (Map.Entry<String, SubsystemGroup> entry : extensionGroups.entrySet()) {"); sw.indent(); sw.println("if (groups.containsKey(entry.getKey())) {"); sw.indent(); sw.println("SubsystemGroup group = groups.get(entry.getKey());"); sw.println("group.getItems().addAll(entry.getValue().getItems());"); sw.outdent(); sw.println("} else {"); sw.indent(); sw.println("SubsystemGroup group = entry.getValue();"); sw.println("groups.put(group.getName(), group);"); sw.outdent(); sw.println("}"); sw.outdent(); sw.println("}"); sw.outdent(); sw.println("}"); // getRuntimeMetricsExtensions sw.println("public List<Predicate> getRuntimeMetricsExtensions() {"); sw.indentln("return _runtimeMetricsExtensions;"); sw.println("}"); // getRuntimeOperationsExtensions sw.println("public List<Predicate> getRuntimeOperationsExtensions() {"); sw.indentln("return _runtimeOperationsExtensions;"); sw.println("}"); // close it out sw.outdent(); sw.println("}"); context.commit(logger, pw); }
From source file:org.jboss.as.console.rebind.forms.ApplicationMetaDataGenerator.java
License:Open Source License
/** * Generate source code for new class. Class extends * <code>HashMap</code>./*from w w w . j a v a 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, 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.widgets.forms.*"); composerFactory.addImport("java.util.*"); // Interfaces composerFactory.addImplementedInterface("org.jboss.as.console.client.widgets.forms.ApplicationMetaData"); // SourceWriter SourceWriter sourceWriter = composerFactory.createSourceWriter(context, printWriter); // fields generateFields(sourceWriter); // ctor generateConstructor(logger, context, sourceWriter); // Methods generateMethods(sourceWriter); // close generated class sourceWriter.outdent(); sourceWriter.println("}"); // commit generated class context.commit(logger, printWriter); }
From source file:org.jboss.as.console.rebind.forms.ApplicationMetaDataGenerator.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 av a 2s .c o m*/ sourceWriter.println("super();"); /* sourceWriter.println("String label = \"\";"); sourceWriter.println("Class<?> listType = null;"); sourceWriter.println("String subgroup = \"\";"); sourceWriter.println("String localTabName = \"\";"); sourceWriter.println("String[] acceptedValues = null;"); */ try { Class<?> beanFactoryClass = getClass().getClassLoader().loadClass(BEAN_FACTORY_NAME); int idx = 0; for (Method method : beanFactoryClass.getMethods()) { Type returnType = method.getGenericReturnType(); if (!(returnType instanceof ParameterizedType)) continue; ParameterizedType type = (ParameterizedType) returnType; Type[] typeArguments = type.getActualTypeArguments(); if (!(typeArguments[0] instanceof Class)) continue; Class beanTypeClass = (Class) typeArguments[0]; //sourceWriter.println(beanTypeClass.getSimpleName() + "_" + idx + "();"); sourceWriter.println(beanTypeClass.getSimpleName() + "();"); idx++; } } catch (ClassNotFoundException e) { throw new RuntimeException("Failed to load " + BEAN_FACTORY_NAME); } sourceWriter.outdent(); sourceWriter.println("}"); }
From source file:org.jboss.as.console.rebind.forms.ApplicationMetaDataGenerator.java
License:Open Source License
private void generateMethods(SourceWriter sourceWriter) { sourceWriter.println("public List<PropertyBinding> getBindingsForType(Class<?> type) { "); sourceWriter.indent();/*from w ww. ja v a 2 s .com*/ sourceWriter.println("return registry.get(type);"); sourceWriter.outdent(); sourceWriter.println("}"); sourceWriter.println("public BeanMetaData getBeanMetaData(Class<?> type) { "); sourceWriter.indent(); sourceWriter.println("return new BeanMetaData(type, addressing.get(type), registry.get(type));"); sourceWriter.outdent(); sourceWriter.println("}"); sourceWriter.println("public FormMetaData getFormMetaData(Class<?> type) { "); sourceWriter.indent(); sourceWriter.println("return new FormMetaData(type, registry.get(type));"); sourceWriter.outdent(); sourceWriter.println("}"); sourceWriter.println("public Mutator getMutator(Class<?> type) { "); sourceWriter.indent(); sourceWriter.println("return mutators.get(type);"); sourceWriter.outdent(); sourceWriter.println("}"); sourceWriter.println("public <T> EntityFactory<T> getFactory(Class<T> type) {"); sourceWriter.indent(); sourceWriter.println("return factories.get(type);"); sourceWriter.outdent(); sourceWriter.println("}"); generateMethodMethods(sourceWriter); }