List of usage examples for com.google.gwt.user.rebind SourceWriter outdent
void outdent();
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();/*from w w w . ja v a 2 s. com*/ 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 void createGetMethods(List<JMethod> getters, SourceWriter sw, String typeName) { sw.println("public <X> X get(String s) {"); sw.println("if (allowNestedValues && NestedModelUtil.isNestedProperty(s)) {"); sw.indentln("return (X)NestedModelUtil.getNestedValue(this, s);"); sw.println("}"); for (JMethod method : getters) { JClassType returnType = method.getReturnType().isClassOrInterface(); String s = method.getName(); String p = lowerFirst(s.substring(s.startsWith("g") ? 3 : 2)); // get or sw.println("if (s.equals(\"" + p + "\")) {"); sw.println("Object value = ((" + typeName + ")bean)." + s + "();"); try {/*from www . j av a 2 s .c o m*/ if (returnType != null && returnType.isAssignableTo(oracle.getType(List.class.getName())) && returnType.isParameterized() != null) { JParameterizedType type = returnType.isParameterized(); JClassType[] params = type.getTypeArgs(); if (beans.contains(params[0])) { sw.println("if (value != null) {"); sw.indent(); sw.println("java.util.List list = (java.util.List)value;"); sw.println("java.util.List list2 = " + BeanModelLookup.class.getCanonicalName() + ".get().getFactory(" + params[0].getQualifiedSourceName() + ".CLASS_NAME).createModel((java.util.Collection) list);"); sw.outdent(); sw.println("return (X) list2;"); sw.println("}"); } } else { // swap returnType as generic types were not matching // (beans.contains(returnType)) if (returnType != null) { String t = returnType.getQualifiedSourceName(); if (t.indexOf("extends") == -1) { returnType = oracle.getType(t); } } if (beans.contains(returnType)) { sw.println("if (value != null) {"); sw.println(" BeanModel nestedModel = nestedModels.get(s);"); sw.println(" if (nestedModel != null) {"); sw.println(" Object bean = nestedModel.getBean();"); sw.println(" if (!bean.equals(value)){"); sw.println(" nestedModel = null;"); sw.println(" }"); sw.println(" }"); sw.println(" if (nestedModel == null) {"); sw.println(" nestedModel = " + BeanModelLookup.class.getCanonicalName() + ".get().getFactory(" + returnType.getQualifiedSourceName() + ".CLASS_NAME).createModel(value);"); sw.println(" nestedModels.put(s, nestedModel);"); sw.println(" }"); sw.println(" return (X)processValue(nestedModel);"); sw.println("}"); } } } catch (Exception e) { e.printStackTrace(); } sw.println("return (X)processValue(value);"); sw.println("}"); } sw.println("return super.get(s);"); sw.println("}"); }
From source file:de.csenk.gwt.commons.bean.rebind.observe.impl.ObservableBeanFactoryCreator.java
License:Apache License
/** * @param beanWriter //from w w w . j a va2 s.c o m * @param beanModel * */ private void writeBeanShimField(SourceWriter beanWriter, ObservableBeanModel beanModel) { beanWriter.println("private final %1$s shim = new %1$s() {", beanModel.getType().getQualifiedSourceName()); beanWriter.indent(); beanWriter.println(); writeShimFields(beanWriter, beanModel); writeShimMethods(beanWriter, beanModel); beanWriter.outdent(); beanWriter.println("};"); beanWriter.println(); beanWriter.println("{ %s.set(shim, %s.class.getName(), this); }", WeakMapping.class.getCanonicalName(), ObservableBean.class.getCanonicalName()); beanWriter.println(); }
From source file:de.csenk.gwt.commons.bean.rebind.observe.impl.ObservableBeanFactoryCreator.java
License:Apache License
/** * @param beanWriter/* ww w.j a v a2 s . com*/ * @param beanModel */ private void writeShimFields(SourceWriter beanWriter, ObservableBeanModel beanModel) { for (ObservableBeanPropertyModel propertyModel : beanModel.getProperties().values()) { beanWriter.println("private final %1$s<%2$s> %3$s = new %1$s<%2$s>() {", AbstractObservableProperty.class.getSimpleName(), ModelUtils.getQualifiedBaseSourceName(propertyModel.getType()), propertyModel.getName()); beanWriter.indent(); beanWriter.println(); beanWriter.println("public void setValue(%s value) {", ModelUtils.getQualifiedBaseSourceName(propertyModel.getType())); beanWriter.indentln("%s.this.observed.%s(value);", beanModel.getSimpleImplementationTypeName(), propertyModel.getSetter().getMethod().getName()); beanWriter.println("}"); beanWriter.println(); beanWriter.println("public %s getValue() {", ModelUtils.getQualifiedBaseSourceName(propertyModel.getType())); beanWriter.indentln("return %s.this.observed.%s();", beanModel.getSimpleImplementationTypeName(), propertyModel.getGetter().getMethod().getName()); beanWriter.println("}"); beanWriter.println(); beanWriter.outdent(); beanWriter.println("};"); beanWriter.println(); } }
From source file:de.csenk.gwt.commons.bean.rebind.observe.impl.ObservableBeanFactoryCreator.java
License:Apache License
/** * @param beanFactoryWriter/*from w w w . j av a 2 s. c o m*/ */ private void writeFactoryMethods(SourceWriter beanFactoryWriter) { for (ObservableBeanFactoryMethodModel methodModel : model.getBeanFactoryMethods()) { final JMethod method = methodModel.getMethod(); final String parameterDeclaration = methodModel.getParameterType() != null ? String.format("%s parameter", methodModel.getParameterType().getQualifiedSourceName()) : ""; beanFactoryWriter.println("public %s %s(%s) {", method.getReturnType().getParameterizedQualifiedSourceName(), method.getName(), parameterDeclaration); beanFactoryWriter.indent(); final String variableAssignment = methodModel.getParameterType() == null ? String .format("autoBeanFactory.create%s().as()", methodModel.getBeanModel().getTopLevelTypeName()) : "parameter"; beanFactoryWriter.println("final %s observed = %s;", methodModel.getBeanModel().getType().getQualifiedSourceName(), variableAssignment); final ObservableBeanModel beanModel = methodModel.getBeanModel(); beanFactoryWriter.println("return new %s(observed);", beanModel.getQualifiedImplementationTypeName()); beanFactoryWriter.outdent(); beanFactoryWriter.println("}"); beanFactoryWriter.println(); } }
From source file:de.csenk.gwt.ws.rebind.filter.serialization.GWTSerializerGenerator.java
License:Open Source License
/** * Generate the proxy constructor and delegate to the superclass constructor * using the default address for the// w w w .j a v a 2 s . c o m * {@link com.google.gwt.user.client.rpc.RemoteService RemoteService}. */ private void generateContructor(SourceWriter srcWriter, JClassType serializerInterface, Map<JType, String> typeStrings) { srcWriter.println("public " + getImplementationSimpleName(serializerInterface) + "() {"); srcWriter.indent(); srcWriter.println("super(GWT.getModuleBaseURL(),"); srcWriter.indent(); srcWriter.println("SERIALIZATION_POLICY, "); srcWriter.println("SERIALIZER);"); srcWriter.outdent(); srcWriter.println(); for (Entry<JType, String> typeEntry : typeStrings.entrySet()) { srcWriter.print("typeStringMap.put(\""); srcWriter.print(typeEntry.getKey().getQualifiedSourceName() + "\", \""); srcWriter.print(typeEntry.getValue()); srcWriter.println("\");"); } srcWriter.outdent(); srcWriter.println("}"); }
From source file:de.knightsoftnet.validators.rebind.GwtSpecificValidatorCreator.java
License:Apache License
private void writeBeanDescriptor(final SourceWriter sw) { final BeanDescriptor beanDescriptor = this.beanHelper.getBeanDescriptor(); // private final GwtBeanDescriptor <MyBean> beanDescriptor = sw.print("private final "); sw.print(GwtBeanDescriptor.class.getCanonicalName()); sw.print("<" + this.beanHelper.getTypeCanonicalName() + ">"); sw.println(" beanDescriptor = "); sw.indent();/*w w w. j a v a 2 s. c om*/ sw.indent(); // GwtBeanDescriptorImpl.builder(Order.class) sw.print(GwtBeanDescriptorImpl.class.getCanonicalName()); sw.println(".builder(" + this.beanHelper.getTypeCanonicalName() + ".class)"); sw.indent(); sw.indent(); // .setConstrained(true) sw.println(".setConstrained(" + beanDescriptor.isBeanConstrained() + ")"); int count = 0; for (final ConstraintDescriptor<?> constraint : beanDescriptor.getConstraintDescriptors()) { if (this.areConstraintDescriptorGroupsValid(constraint)) { // .add(c0) sw.println(".add(" + this.constraintDescriptorVar("this", count) + ")"); count++; } } // .put("myProperty", myProperty_pd) for (final PropertyDescriptor p : beanDescriptor.getConstrainedProperties()) { sw.print(".put(\""); sw.print(p.getPropertyName()); sw.print("\", "); sw.print(p.getPropertyName()); sw.println("_pd)"); } // .setBeanMetadata(beanMetadata) sw.println(".setBeanMetadata(beanMetadata)"); // .build(); sw.println(".build();"); sw.outdent(); sw.outdent(); sw.outdent(); sw.outdent(); }
From source file:de.knightsoftnet.validators.rebind.GwtSpecificValidatorCreator.java
License:Apache License
private void writeBeanMetadata(final SourceWriter sw) throws UnableToCompleteException { // private final BeanMetadata beanMetadata = sw.println("private final BeanMetadata beanMetadata ="); sw.indent();/*from w w w .j a v a 2 s .co m*/ sw.indent(); // new BeanMetadata( sw.println("new " + BeanMetadata.class.getSimpleName() + "("); sw.indent(); sw.indent(); // <<bean class>>, <<default group seq class 1>>, <<default group seq class 2>>, ... final Class<?> beanClazz = this.beanHelper.getClazz(); sw.print(asLiteral(beanClazz)); final GroupSequence groupSeqAnnotation = beanClazz.getAnnotation(GroupSequence.class); final List<Class<?>> groupSequence = new ArrayList<>(); if (groupSeqAnnotation == null) { groupSequence.add(beanClazz); } else { groupSequence.addAll(Arrays.asList(groupSeqAnnotation.value())); } boolean groupSequenceContainsDefault = false; for (final Class<?> group : groupSequence) { sw.println(","); if (group.getName().equals(beanClazz.getName())) { sw.print(asLiteral(Default.class)); groupSequenceContainsDefault = true; } else if (group.getName().equals(Default.class.getName())) { throw error(this.logger, "'Default.class' cannot appear in default group sequence list."); } else { sw.print(asLiteral(group)); } } if (!groupSequenceContainsDefault) { throw error(this.logger, beanClazz.getName() + " must be part of the redefined default group " + "sequence."); } sw.println(");"); sw.outdent(); sw.outdent(); sw.outdent(); sw.outdent(); }
From source file:de.knightsoftnet.validators.rebind.GwtSpecificValidatorCreator.java
License:Apache License
private void writeConstraintDescriptor(final SourceWriter sw, final ConstraintDescriptor<? extends Annotation> constraint, final ElementType elementType, final ConstraintOrigin origin, final String constraintDescripotorVar) throws UnableToCompleteException { final Class<? extends Annotation> annotationType = constraint.getAnnotation().annotationType(); // First list all composing constraints int count = 0; for (final ConstraintDescriptor<?> composingConstraint : constraint.getComposingConstraints()) { this.writeConstraintDescriptor(sw, composingConstraint, elementType, origin, constraintDescripotorVar + "_" + count++); }//from w ww .java 2s . c o m // private final ConstraintDescriptorImpl<MyAnnotation> constraintDescriptor = ; sw.print("private final "); sw.print(ConstraintDescriptorImpl.class.getCanonicalName()); sw.print("<"); sw.print(annotationType.getCanonicalName()); sw.print(">"); sw.println(" " + constraintDescripotorVar + " = "); sw.indent(); sw.indent(); // ConstraintDescriptorImpl.<MyConstraint> builder() sw.print(ConstraintDescriptorImpl.class.getCanonicalName()); sw.print(".<"); sw.print(annotationType.getCanonicalName()); sw.println("> builder()"); sw.indent(); sw.indent(); // .setAnnotation(new MyAnnotation ) sw.println(".setAnnotation( "); sw.indent(); sw.indent(); this.writeNewAnnotation(sw, constraint); sw.println(")"); sw.outdent(); sw.outdent(); // .setAttributes(builder() sw.println(".setAttributes(attributeBuilder()"); sw.indent(); for (final Map.Entry<String, Object> entry : constraint.getAttributes().entrySet()) { // .put(key, value) sw.print(".put("); final String key = entry.getKey(); sw.print(asLiteral(key)); sw.print(", "); Object value = entry.getValue(); // Add the Default group if it is not already present if ("groups".equals(key) && value instanceof Class[] && ((Class[]) value).length == 0) { value = new Class[] { Default.class }; } sw.print(asLiteral(value)); sw.println(")"); } // .build()) sw.println(".build())"); sw.outdent(); // .setConstraintValidatorClasses(classes ) sw.print(".setConstraintValidatorClasses("); sw.print(asLiteral(this.asArray(constraint.getConstraintValidatorClasses(), new Class[0]))); sw.println(")"); final int ccCount = constraint.getComposingConstraints().size(); for (int i = 0; i < ccCount; i++) { // .addComposingConstraint(cX_X) sw.print(".addComposingConstraint("); sw.print(constraintDescripotorVar + "_" + i); sw.println(")"); } // .getGroups(groups) sw.print(".setGroups("); final Set<Class<?>> groups = constraint.getGroups(); sw.print(asLiteral(this.asArray(groups, new Class<?>[0]))); sw.println(")"); // .setPayload(payload) sw.print(".setPayload("); final Set<Class<? extends Payload>> payload = constraint.getPayload(); sw.print(asLiteral(this.asArray(payload, new Class[0]))); sw.println(")"); // .setReportAsSingleViolation(boolean ) sw.print(".setReportAsSingleViolation("); sw.print(Boolean.toString(constraint.isReportAsSingleViolation())); sw.println(")"); // .setElementType(elementType) sw.print(".setElementType("); sw.print(asLiteral(elementType)); sw.println(")"); // .setDefinedOn(origin) sw.print(".setDefinedOn("); sw.print(asLiteral(origin)); sw.println(")"); // .build(); sw.println(".build();"); sw.outdent(); sw.outdent(); sw.outdent(); sw.outdent(); sw.println(); }
From source file:de.knightsoftnet.validators.rebind.GwtSpecificValidatorCreator.java
License:Apache License
private void writeExpandDefaultAndValidate(final SourceWriter sw, final Stage stage) throws UnableToCompleteException { final Class<?> clazz = this.beanHelper.getClazz(); // ArrayList<Class<?>> justGroups = new ArrayList<Class<?>>(); sw.println("ArrayList<Class<?>> justGroups = new ArrayList<Class<?>>();"); // for (Group g : groups) { sw.println("for (Group g : groups) {"); sw.indent();//from w w w.ja v a 2 s . c om // if (!g.isDefaultGroup() || !getBeanMetadata().defaultGroupSequenceIsRedefined()) { sw.println("if (!g.isDefaultGroup() || !getBeanMetadata().defaultGroupSequenceIsRedefined()) {"); sw.indent(); // justGroups.add(g.getGroup()); sw.println("justGroups.add(g.getGroup());"); sw.outdent(); // } sw.println("}"); sw.outdent(); // } sw.println("}"); // Class<?>[] justGroupsArray = justGroups.toArray(new Class<?>[justGroups.size()]); sw.println("Class<?>[] justGroupsArray = justGroups.toArray(new Class<?>[justGroups.size()]);"); switch (stage) { case OBJECT: // validateAllNonInheritedProperties(context, object, violations, justGroupsArray); sw.println("validateAllNonInheritedProperties(context, object, violations, " + "justGroupsArray);"); this.writeClassLevelConstraintsValidation(sw, "justGroupsArray"); break; case PROPERTY: // validatePropertyGroups(context, object, propertyName, violations, justGroupsArray); sw.println("validatePropertyGroups(context, object, propertyName, violations, " + "justGroupsArray);"); break; case VALUE: // validateValueGroups(context, beanType, propertyName, value, violations, // justGroupsArray); sw.println("validateValueGroups(context, beanType, propertyName, value, violations, " + "justGroupsArray);"); break; default: throw new IllegalStateException(); } // if (getBeanMetadata().defaultGroupSequenceIsRedefined()) { sw.println("if (getBeanMetadata().defaultGroupSequenceIsRedefined()) {"); sw.indent(); // for (Class<?> g : beanMetadata.getDefaultGroupSequence()) { sw.println("for (Class<?> g : beanMetadata.getDefaultGroupSequence()) {"); sw.indent(); // int numberOfViolations = violations.size(); sw.println("int numberOfViolations = violations.size();"); switch (stage) { case OBJECT: // validateAllNonInheritedProperties(context, object, violations, g); sw.println("validateAllNonInheritedProperties(context, object, violations, g);"); this.writeClassLevelConstraintsValidation(sw, "g"); // validate super classes and super interfaces this.writeValidateInheritance(sw, clazz, Stage.OBJECT, null, false, "g"); break; case PROPERTY: // validatePropertyGroups(context, object, propertyName, violations, g); sw.println("validatePropertyGroups(context, object, propertyName, violations, g);"); break; case VALUE: // validateValueGroups(context, beanType, propertyName, value, violations, g); sw.println("validateValueGroups(context, beanType, propertyName, value, violations, g);"); break; default: throw new IllegalStateException(); } // if (violations.size() > numberOfViolations) { sw.println("if (violations.size() > numberOfViolations) {"); sw.indent(); // break; sw.println("break;"); sw.outdent(); // } sw.println("}"); sw.outdent(); // } sw.println("}"); sw.outdent(); // } sw.println("}"); if (stage == Stage.OBJECT) { // else { sw.println("else {"); sw.indent(); // validate super classes and super interfaces this.writeValidateInheritance(sw, clazz, Stage.OBJECT, null, true, "groups"); // } sw.outdent(); sw.println("}"); } }