List of usage examples for com.google.gwt.core.ext.arguments JArguments getKey
public static String getKey(JArgument... arguments)
From source file:com.google.gwt.sample.hellorebinding.rebind.JsniSnippetGenerator.java
License:Apache License
@Override public String generate(TreeLogger logger, GeneratorContext context, String typeName, JArgument[] arguments) throws UnableToCompleteException { JClassType snippetType = context.getTypeOracle().findType(typeName); if (snippetType == null) { logger.log(TreeLogger.ERROR, "Rebinding type not found"); throw new UnableToCompleteException(); }/*w ww . ja v a2 s.c om*/ if (arguments.length < 1) { logger.log(TreeLogger.ERROR, "Missing JSNI literal argument"); throw new UnableToCompleteException(); } JArgument arg0 = arguments[0]; if (!(arg0 instanceof JStringArgument)) { logger.log(TreeLogger.ERROR, "JSNI literal argument must be a String literal"); throw new UnableToCompleteException(); } String argumentsKey = JArguments.getKey(arguments); String packageName = snippetType.getPackage().getName(); String snippetTypeName = snippetType.getSimpleSourceName() + "_impl_" + argumentsKey; String qualifiedSnippetTypeName = packageName + "." + snippetTypeName; PrintWriter pw = context.tryCreate(logger, packageName, snippetTypeName); if (pw != null) { int argCount = argumentsCount(snippetType); if (argCount < 0) { logger.log(TreeLogger.ERROR, "execute() method not found"); throw new UnableToCompleteException(); } String jsniTemplate = ((JStringArgument) arg0).getValue(); String[] parts = jsniTemplate.split("#", -1); if (parts.length != (argCount + 1)) { logger.log(TreeLogger.ERROR, "wrong number of snippet arguments"); throw new UnableToCompleteException(); } pw.printf("package %s;\n\n", packageName); pw.printf("public class %s implements %s {\n\n", snippetTypeName, snippetType.getSimpleSourceName()); pw.printf(" public %s(String jsniLiteral) {}\n", snippetTypeName); pw.println(); pw.print(" @Override public native <T> T execute("); boolean hasSeparator = false; for (int i = 0; i < argCount; i++) { if (hasSeparator) { pw.print(", "); } else { hasSeparator = true; } pw.printf("Object a%d", i); } pw.println(") /*-{"); pw.print(" return "); if (argCount == 0) { pw.print(jsniTemplate); } else { for (int i = 0; i < argCount; i++) { pw.printf("%sa%d", parts[i], i); } pw.print(parts[argCount]); } pw.println(";"); pw.println(" }-*/;"); pw.println("}"); context.commit(logger, pw); } return qualifiedSnippetTypeName; }
From source file:com.google.gwt.sample.hellorebinding.rebind.StringFormatterGenerator.java
License:Apache License
@Override public String generate(TreeLogger logger, GeneratorContext context, String typeName, JArgument[] arguments) throws UnableToCompleteException { if (arguments.length < 1) { logger.log(TreeLogger.ERROR, "Missing format argument"); throw new UnableToCompleteException(); }//from www. j a v a2 s.c o m JArgument arg0 = arguments[0]; if (!((arg0 instanceof JOpaqueArgument) || (arg0 instanceof JStringArgument))) { logger.log(TreeLogger.ERROR, "Format argument must be a String literal or reference"); throw new UnableToCompleteException(); } if (arguments.length < 2) { logger.log(TreeLogger.ERROR, "Missing argument list"); throw new UnableToCompleteException(); } JArgument arg1 = arguments[1]; if (!((arg1 instanceof JOpaqueArgument) || (arg1 instanceof JArrayArgument))) { logger.log(TreeLogger.ERROR, "Argument list must be a String array literal or reference"); throw new UnableToCompleteException(); } // If the format is opaque, it needs a runtime implementation. if (arg0 instanceof JOpaqueArgument) { return RuntimeStringFormatter.class.getCanonicalName(); } // If the argument list is opaque, it needs a runtime implementation. if (arg1 instanceof JOpaqueArgument) { return RuntimeStringFormatter.class.getCanonicalName(); } String argumentsKey = JArguments.getKey(arguments); String packageName = StringFormatter.class.getPackage().getName(); String formatterTypeName = StringFormatter.class.getSimpleName() + "_impl_" + argumentsKey; String qualifiedFormatterTypeName = packageName + "." + formatterTypeName; PrintWriter pw = context.tryCreate(logger, packageName, formatterTypeName); if (pw != null) { pw.printf("package %s;\n\n", packageName); pw.printf("import %s;\n\n", GWT.class.getCanonicalName()); pw.printf("public class %s implements StringFormatter {\n\n", formatterTypeName); pw.println(" private final String[] arguments;"); pw.println(); pw.printf(" public %s(String format, String[] arguments) {\n", formatterTypeName); pw.println(" this.arguments = arguments;"); pw.println(" }"); pw.println(); pw.printf(" @Override public String format() {\n"); String format = ((JStringArgument) arg0).getValue(); JArgument[] args = ((JArrayArgument) arg1).getElements(); String[] parts = format.split("%s", -1); if (parts.length < 2) { pw.printf(" return \"%s\";\n", escape(parts[0])); } else { pw.println(" StringBuilder sb = new StringBuilder();"); int argTop = args.length < parts.length ? args.length : parts.length - 1; for (int i = 0; i < parts.length; i++) { pw.printf(" sb.append(\"%s\");\n", escape(parts[i])); if (argTop > i) { JArgument a = args[i]; if (a instanceof JOpaqueArgument) { pw.printf(" sb.append(this.arguments[%d]);\n", i); } else if (a instanceof JStringArgument) { JStringArgument s = (JStringArgument) a; pw.printf(" sb.append(\"%s\");\n", escape(s.getValue())); } else if (a instanceof JNullArgument) { pw.printf(" sb.append(\"null\");\n"); } else { logger.log(TreeLogger.ERROR, "Argument must be of type String"); throw new UnableToCompleteException(); } } } pw.printf(" return sb.toString();\n"); } pw.println(" }"); pw.println("}"); context.commit(logger, pw); } return qualifiedFormatterTypeName; }
From source file:com.google.gwt.sample.hellorebinding.rebind.UiBinderCreatorGenerator.java
License:Apache License
@Override public String generate(TreeLogger logger, GeneratorContext context, String typeName, JArgument[] arguments) throws UnableToCompleteException { TypeOracle oracle = context.getTypeOracle(); if (arguments.length < 1) { logger.log(TreeLogger.ERROR, "Missing root type argument"); throw new UnableToCompleteException(); }//from w w w . j a v a2s . c o m if (!(arguments[0] instanceof JClassArgument)) { logger.log(TreeLogger.ERROR, "Root type argument must be a Class literal"); throw new UnableToCompleteException(); } JClassArgument rootTypeArg = (JClassArgument) arguments[0]; JClassType rootType = oracle.findType(rootTypeArg.getValue()); if (arguments.length < 2) { logger.log(TreeLogger.ERROR, "Missing owner type argument"); throw new UnableToCompleteException(); } if (!(arguments[1] instanceof JClassArgument)) { logger.log(TreeLogger.ERROR, "Root type argument must be a Class literal"); throw new UnableToCompleteException(); } JClassArgument ownerTypeArg = (JClassArgument) arguments[1]; JClassType ownerType = oracle.findType(ownerTypeArg.getValue()); String template; if (arguments.length > 2) { if (!(arguments[2] instanceof JStringArgument)) { logger.log(TreeLogger.ERROR, "Template argument must be a String literal"); throw new UnableToCompleteException(); } JStringArgument templateArg = (JStringArgument) arguments[2]; template = templateArg.getValue(); } else { template = ownerType.getSimpleSourceName() + ".ui.xml"; } if (arguments.length > 3) { logger.log(TreeLogger.ERROR, "Too much arguments"); throw new UnableToCompleteException(); } String argumentsKey = JArguments.getKey(arguments); String packageName = ownerType.getPackage().getName(); String creatorTypeName = ownerType.getSimpleSourceName() + "_UiBinderCreatorImpl_" + argumentsKey; String qualifiedCreatorTypeName = packageName + "." + creatorTypeName; String rootTypeName = rootType.getQualifiedSourceName(); String ownerTypeName = ownerType.getQualifiedSourceName(); PrintWriter pw = context.tryCreate(logger, packageName, creatorTypeName); if (pw != null) { pw.printf("package %s;\n", packageName); pw.println(); pw.printf("import %s;\n", GWT.class.getCanonicalName()); pw.printf("import %s;\n", UiBinder.class.getCanonicalName()); pw.printf("import %s;\n", UiTemplate.class.getCanonicalName()); pw.printf("import %s;\n", UiBinderCreator.class.getCanonicalName()); pw.println(); pw.printf("public class %s implements UiBinderCreator<%s, %s> {\n", creatorTypeName, rootTypeName, ownerTypeName); pw.println(); pw.printf(" @UiTemplate(\"%s\")\n", escape(template)); pw.printf(" public static interface Internal extends UiBinder<%s, %s> {}\n", rootTypeName, ownerTypeName); pw.println(); // Add constructor if (arguments.length > 2) { pw.printf(" public %s(Class<%s> rootType, Class<%s> ownerType, String template) {}\n", creatorTypeName, rootTypeName, ownerTypeName); } else { pw.printf(" public %s(Class<%s> rootType, Class<%s> ownerType) {}\n", creatorTypeName, rootTypeName, ownerTypeName); } pw.println(); // Implement create() method pw.printf(" @Override public UiBinder<%s, %s> createUiBinder() {\n", rootTypeName, ownerTypeName); pw.println(" return GWT.create(Internal.class);"); pw.println(" }"); pw.println("}"); context.commit(logger, pw); } return qualifiedCreatorTypeName; }