List of usage examples for com.google.gwt.user.rebind SourceWriter indent
void indent();
From source file:com.google.gwt.testing.easygwtmock.rebind.MocksControlGenerator.java
License:Apache License
private void printFactoryMethod(SourceWriter out, JMethod methodToImplement, JClassType mockControlInterface, String classToCreate) {//ww w . j av a 2 s . co m out.println("%s {", methodToImplement.getReadableDeclaration(false, false, false, false, true)); out.indent(); if (isNiceMock(methodToImplement, mockControlInterface)) { out.print("return this.setToNice(new %s(", classToCreate); printMatchingParameters(out, methodToImplement); out.println(").__mockInit(this));"); } else { out.print("return new %s(", classToCreate); printMatchingParameters(out, methodToImplement); out.println(").__mockInit(this);"); } out.outdent(); out.println("}"); }
From source file:com.google.gwt.testing.easygwtmock.rebind.MocksGenerator.java
License:Apache License
/** * Print the default implementation for {@link java.lang.Object} methods. * If typeToMock is a class it will only print default implementations for * methods of Object listed in the methods parameter. If typeToMock is an interface * it will always print default implementations for all methods of Object. *///from w w w . j av a 2 s. c o m private void printDefaultMethods(SourceWriter writer, JClassType typeToMock, Set<String> methods) { // always print default implementations for interfaces boolean isInterface = typeToMock.isInterface() != null; if (isInterface || methods.contains("equals")) { writer.println("public boolean equals(Object obj) {"); writer.indent(); writer.println("this.mocksControl.unmockableCallTo(\"equals()\");"); writer.println("return obj == this;"); // object identity since Mocks don't hold any data writer.outdent(); writer.println("}"); writer.println(); } if (isInterface || methods.contains("toString")) { writer.println("public String toString() {"); writer.indent(); writer.println("this.mocksControl.unmockableCallTo(\"toString()\");"); writer.println("return \"Mock for %s\";", typeToMock.getName()); writer.outdent(); writer.println("}"); writer.println(); } if (isInterface || methods.contains("hashCode")) { writer.println("public int hashCode() {"); writer.indent(); writer.println("this.mocksControl.unmockableCallTo(\"hashCode()\");"); writer.println("return System.identityHashCode(this);"); // hashCode of java.lang.Object writer.outdent(); writer.println("}"); writer.println(); } if (isInterface || methods.contains("finalize")) { writer.println("protected void finalize() throws Throwable {"); writer.indent(); writer.println("this.mocksControl.unmockableCallTo(\"finalize()\");"); writer.println("super.finalize();"); writer.outdent(); writer.println("}"); writer.println(); } }
From source file:com.google.gwt.testing.easygwtmock.rebind.MocksGenerator.java
License:Apache License
/** * Prints each constructor for the mock class, and a hidden init method. *///from ww w. j a va 2 s. c o m private void printConstructors(SourceWriter out, String newClassName, JConstructor[] constructors) { if (constructors.length == 0) { // probably an interface out.print("public %s() {}", newClassName); } for (JConstructor constructor : constructors) { out.print("public %s(", newClassName); printMatchingParameters(out, constructor); out.println(") {"); out.indent(); printMatchingSuperCall(out, constructor); out.outdent(); out.println("}"); out.println(); } out.println("public %s __mockInit(MocksControlBase newValue) {", newClassName); out.indent(); out.println("this.mocksControl = newValue;"); out.println("return this;"); out.outdent(); out.println("}"); out.println(); }
From source file:com.google.gwt.testing.easygwtmock.rebind.MocksGenerator.java
License:Apache License
/** * Generates and prints the actual mock versions of the methods. *//*from ww w. j a v a 2s . c o m*/ private void printMockMethods(SourceWriter sourceWriter, List<JMethod> methodsToMock, String newClassName) { int methodNo = 0; for (JMethod method : methodsToMock) { sourceWriter.println("%s {", method.getReadableDeclaration(false, true, false, false, true)); sourceWriter.indent(); printMockMethodBody(sourceWriter, method, methodNo++, newClassName); sourceWriter.outdent(); sourceWriter.println("}"); sourceWriter.println(); } }
From source file:com.google.gwt.testing.easygwtmock.rebind.MocksGenerator.java
License:Apache License
private void printMockMethodBody(SourceWriter sourceWriter, JMethod method, int methodNo, String newClassName) { JParameter[] args = method.getParameters(); String callVar = freeVariableName("call", args); sourceWriter.print("Call %s = new Call(this, %s.methods[%d]", callVar, newClassName, methodNo); int argsCount = method.isVarArgs() ? args.length - 1 : args.length; for (int i = 0; i < argsCount; i++) { sourceWriter.print(", %s", args[i].getName()); }//w w w . ja v a2 s. c om sourceWriter.println(");"); if (method.isVarArgs()) { sourceWriter.println("%s.addVarArgument(%s);", callVar, args[args.length - 1].getName()); } sourceWriter.println("try {"); sourceWriter.indent(); if (!isVoid(method)) { sourceWriter.print("return ("); JType returnType = method.getReturnType(); JPrimitiveType primitiveType = returnType.isPrimitive(); if (primitiveType != null) { sourceWriter.print(primitiveType.getQualifiedBoxedSourceName()); } else if (returnType.isTypeParameter() != null) { sourceWriter.print(returnType.isTypeParameter().getName()); } else { sourceWriter.print(returnType.getQualifiedSourceName()); } sourceWriter.print(") "); } sourceWriter.println("this.mocksControl.invoke(%s).answer(%s.getArguments().toArray());", callVar, callVar); sourceWriter.outdent(); String exceptionVar = freeVariableName("exception", args); sourceWriter.println("} catch (Throwable %s) {", exceptionVar); sourceWriter.indent(); String assertionError = AssertionErrorWrapper.class.getCanonicalName(); sourceWriter.println( "if (%s instanceof %s) throw (AssertionError) " + "((%s) %s).getAssertionError().fillInStackTrace();", exceptionVar, assertionError, assertionError, exceptionVar); for (JClassType exception : method.getThrows()) { printRethrowException(sourceWriter, exceptionVar, exception.getQualifiedSourceName()); } printRethrowException(sourceWriter, exceptionVar, "RuntimeException"); printRethrowException(sourceWriter, exceptionVar, "Error"); sourceWriter.println("throw new UndeclaredThrowableException(%s);", exceptionVar); sourceWriter.outdent(); sourceWriter.println("}"); }
From source file:com.google.web.bindery.autobean.gwt.rebind.AutoBeanFactoryGenerator.java
License:Apache License
/** * For interfaces that consist of nothing more than getters and setters, * create a map-based implementation that will allow the AutoBean's internal * state to be easily consumed./* ww w . j av a 2 s.co m*/ */ private void writeCreateSimpleBean(SourceWriter sw, AutoBeanType type) { sw.println("@Override protected %s createSimplePeer() {", type.getPeerType().getQualifiedSourceName()); sw.indent(); // return new FooIntf() { sw.println("return new %s() {", type.getPeerType().getQualifiedSourceName()); sw.indent(); sw.println("private final %s data = %s.this.data;", Splittable.class.getCanonicalName(), type.getQualifiedSourceName()); for (AutoBeanMethod method : type.getMethods()) { JMethod jmethod = method.getMethod(); JType returnType = jmethod.getReturnType(); sw.println("public %s {", getBaseMethodDeclaration(jmethod)); sw.indent(); switch (method.getAction()) { case GET: { String castType; if (returnType.isPrimitive() != null) { castType = returnType.isPrimitive().getQualifiedBoxedSourceName(); // Boolean toReturn = Other.this.getOrReify("foo"); sw.println("%s toReturn = %s.this.getOrReify(\"%s\");", castType, type.getSimpleSourceName(), method.getPropertyName()); // return toReturn == null ? false : toReturn; sw.println("return toReturn == null ? %s : toReturn;", returnType.isPrimitive().getUninitializedFieldExpression()); } else if (returnType .equals(context.getTypeOracle().findType(Splittable.class.getCanonicalName()))) { sw.println("return data.isNull(\"%1$s\") ? null : data.get(\"%1$s\");", method.getPropertyName()); } else { // return (ReturnType) Outer.this.getOrReify(\"foo\"); castType = ModelUtils.getQualifiedBaseSourceName(returnType); sw.println("return (%s) %s.this.getOrReify(\"%s\");", castType, type.getSimpleSourceName(), method.getPropertyName()); } } break; case SET: case SET_BUILDER: { JParameter param = jmethod.getParameters()[0]; // Other.this.setProperty("foo", parameter); sw.println("%s.this.setProperty(\"%s\", %s);", type.getSimpleSourceName(), method.getPropertyName(), param.getName()); if (JBeanMethod.SET_BUILDER.equals(method.getAction())) { sw.println("return this;"); } break; } case CALL: // return com.example.Owner.staticMethod(Outer.this, param, // param); JMethod staticImpl = method.getStaticImpl(); if (!returnType.equals(JPrimitiveType.VOID)) { sw.print("return "); } sw.print("%s.%s(%s.this", staticImpl.getEnclosingType().getQualifiedSourceName(), staticImpl.getName(), type.getSimpleSourceName()); for (JParameter param : jmethod.getParameters()) { sw.print(", %s", param.getName()); } sw.println(");"); break; default: throw new RuntimeException(); } sw.outdent(); sw.println("}"); } sw.outdent(); sw.println("};"); sw.outdent(); sw.println("}"); }
From source file:com.google.web.bindery.autobean.gwt.rebind.AutoBeanFactoryGenerator.java
License:Apache License
/** * Write an instance initializer block to populate the creators map. *//*from ww w . j a v a 2 s.c om*/ private void writeDynamicMethods(SourceWriter sw) { List<JClassType> privatePeers = new ArrayList<JClassType>(); sw.println("@Override protected void initializeCreatorMap(%s map) {", JsniCreatorMap.class.getCanonicalName()); sw.indent(); for (AutoBeanType type : model.getAllTypes()) { if (type.isNoWrap()) { continue; } String classLiteralAccessor; JClassType peer = type.getPeerType(); String peerName = ModelUtils.ensureBaseType(peer).getQualifiedSourceName(); if (peer.isPublic()) { classLiteralAccessor = peerName + ".class"; } else { privatePeers.add(peer); classLiteralAccessor = "classLit_" + peerName.replace('.', '_') + "()"; } // map.add(Foo.class, getConstructors_com_foo_Bar()); sw.println("map.add(%s, getConstructors_%s());", classLiteralAccessor, peerName.replace('.', '_')); } sw.outdent(); sw.println("}"); /* * Create a native method for each peer type that isn't public since Java * class literal references are scoped. */ for (JClassType peer : privatePeers) { String peerName = ModelUtils.ensureBaseType(peer).getQualifiedSourceName(); sw.println("private native Class<?> classLit_%s() /*-{return @%s::class;}-*/;", peerName.replace('.', '_'), peerName); } /* * Create a method that returns an array containing references to the * constructors. */ String factoryJNIName = context.getTypeOracle().findType(AutoBeanFactory.class.getCanonicalName()) .getJNISignature(); for (AutoBeanType type : model.getAllTypes()) { String peerName = ModelUtils.ensureBaseType(type.getPeerType()).getQualifiedSourceName(); String peerJNIName = ModelUtils.ensureBaseType(type.getPeerType()).getJNISignature(); /*- * private native JsArray<JSO> getConstructors_com_foo_Bar() { * return [ * BarProxyImpl::new(ABFactory), * BarProxyImpl::new(ABFactory, DelegateType) * ]; * } */ sw.println("private native %s<%s> getConstructors_%s() /*-{", JsArray.class.getCanonicalName(), JavaScriptObject.class.getCanonicalName(), peerName.replace('.', '_')); sw.indent(); sw.println("return ["); if (type.isSimpleBean()) { sw.indentln("@%s::new(%s),", type.getQualifiedSourceName(), factoryJNIName); } else { sw.indentln(","); } sw.indentln("@%s::new(%s%s)", type.getQualifiedSourceName(), factoryJNIName, peerJNIName); sw.println("];"); sw.outdent(); sw.println("}-*/;"); } }
From source file:com.google.web.bindery.autobean.gwt.rebind.AutoBeanFactoryGenerator.java
License:Apache License
private void writeEnumSetup(SourceWriter sw) { // Make the deobfuscation model Map<String, List<JEnumConstant>> map = new HashMap<String, List<JEnumConstant>>(); for (Map.Entry<JEnumConstant, String> entry : model.getEnumTokenMap().entrySet()) { List<JEnumConstant> list = map.get(entry.getValue()); if (list == null) { list = new ArrayList<JEnumConstant>(); map.put(entry.getValue(), list); }/*from w w w . j a v a 2 s . com*/ list.add(entry.getKey()); } sw.println("@Override protected void initializeEnumMap() {"); sw.indent(); for (Map.Entry<JEnumConstant, String> entry : model.getEnumTokenMap().entrySet()) { // enumToStringMap.put(Enum.FOO, "FOO"); sw.println("enumToStringMap.put(%s.%s, \"%s\");", entry.getKey().getEnclosingType().getQualifiedSourceName(), entry.getKey().getName(), entry.getValue()); } for (Map.Entry<String, List<JEnumConstant>> entry : map.entrySet()) { String listExpr; if (entry.getValue().size() == 1) { JEnumConstant e = entry.getValue().get(0); // Collections.singletonList(Enum.FOO) listExpr = String.format("%s.<%s<?>> singletonList(%s.%s)", Collections.class.getCanonicalName(), Enum.class.getCanonicalName(), e.getEnclosingType().getQualifiedSourceName(), e.getName()); } else { // Arrays.asList(Enum.FOO, OtherEnum.FOO, ThirdEnum,FOO) StringBuilder sb = new StringBuilder(); boolean needsComma = false; sb.append(String.format("%s.<%s<?>> asList(", Arrays.class.getCanonicalName(), Enum.class.getCanonicalName())); for (JEnumConstant e : entry.getValue()) { if (needsComma) { sb.append(","); } needsComma = true; sb.append(e.getEnclosingType().getQualifiedSourceName()).append(".").append(e.getName()); } sb.append(")"); listExpr = sb.toString(); } sw.println("stringsToEnumsMap.put(\"%s\", %s);", entry.getKey(), listExpr); } sw.outdent(); sw.println("}"); }
From source file:com.google.web.bindery.autobean.gwt.rebind.AutoBeanFactoryGenerator.java
License:Apache License
private void writeMethods(SourceWriter sw) throws UnableToCompleteException { for (AutoBeanFactoryMethod method : model.getMethods()) { AutoBeanType autoBeanType = method.getAutoBeanType(); // public AutoBean<Foo> foo(FooSubtype wrapped) { sw.println("public %s %s(%s) {", method.getReturnType().getQualifiedSourceName(), method.getName(), method.isWrapper() ? (method.getWrappedType().getQualifiedSourceName() + " wrapped") : ""); if (method.isWrapper()) { sw.indent(); // AutoBean<Foo> toReturn = AutoBeanUtils.getAutoBean(wrapped); sw.println("%s toReturn = %s.getAutoBean(wrapped);", method.getReturnType().getParameterizedQualifiedSourceName(), AutoBeanUtils.class.getCanonicalName()); sw.println("if (toReturn != null) {return toReturn;}"); // return new FooAutoBean(Factory.this, wrapped); sw.println("return new %s(%s.this, wrapped);", autoBeanType.getQualifiedSourceName(), simpleSourceName);//w w w.j a va2 s. co m sw.outdent(); } else { // return new FooAutoBean(Factory.this); sw.indentln("return new %s(%s.this);", autoBeanType.getQualifiedSourceName(), simpleSourceName); } sw.println("}"); } }
From source file:com.google.web.bindery.autobean.gwt.rebind.AutoBeanFactoryGenerator.java
License:Apache License
private void writeReturnWrapper(SourceWriter sw, AutoBeanType type, AutoBeanMethod method) throws UnableToCompleteException { if (!method.isValueType() && !method.isNoWrap()) { JMethod jmethod = method.getMethod(); JClassType returnClass = jmethod.getReturnType().isClassOrInterface(); AutoBeanType peer = model.getPeer(returnClass); sw.println("if (toReturn != null) {"); sw.indent(); sw.println("if (%s.this.isWrapped(toReturn)) {", type.getSimpleSourceName()); sw.indentln("toReturn = %s.this.getFromWrapper(toReturn);", type.getSimpleSourceName()); sw.println("} else {"); sw.indent();//from w ww . ja v a 2s .co m if (peer != null) { // toReturn = new FooAutoBean(getFactory(), toReturn).as(); sw.println("toReturn = new %s(getFactory(), toReturn).as();", peer.getQualifiedSourceName()); } sw.outdent(); sw.println("}"); sw.outdent(); sw.println("}"); } // Allow return values to be intercepted JMethod interceptor = type.getInterceptor(); if (interceptor != null) { // toReturn = FooCategory.__intercept(FooAutoBean.this, toReturn); sw.println("toReturn = %s.%s(%s.this, toReturn);", interceptor.getEnclosingType().getQualifiedSourceName(), interceptor.getName(), type.getSimpleSourceName()); } }