1 16 package com.google.gwt.i18n.rebind; 17 18 import com.google.gwt.core.ext.TreeLogger; 19 import com.google.gwt.core.ext.typeinfo.JMethod; 20 import com.google.gwt.core.ext.typeinfo.JPrimitiveType; 21 import com.google.gwt.core.ext.typeinfo.JType; 22 import com.google.gwt.user.rebind.AbstractGeneratorClassCreator; 23 import com.google.gwt.user.rebind.AbstractMethodCreator; 24 import com.google.gwt.user.rebind.AbstractSourceCreator; 25 26 import java.text.MessageFormat ; 27 28 31 class LookupMethodCreator extends AbstractMethodCreator { 32 private JType returnType; 33 34 40 public LookupMethodCreator(AbstractGeneratorClassCreator classCreator, 41 JType returnType) { 42 super(classCreator); 43 this.returnType = returnType; 44 } 45 46 public void createMethodFor(TreeLogger logger, JMethod targetMethod, 47 String value) { 48 createMethodFor(targetMethod); 49 } 50 51 void createMethodFor(JMethod targetMethod) { 52 String template = "{0} target = ({0})cache.get(arg0);"; 53 String type; 54 JPrimitiveType s = returnType.isPrimitive(); 55 if (s != null) { 56 type = AbstractSourceCreator.getJavaObjectTypeFor(s); 57 } else { 58 type = returnType.getQualifiedSourceName(); 59 } 60 Object [] args = {type}; 61 String lookup = MessageFormat.format(template, args); 62 println(lookup); 63 println("if (target != null)"); 64 printReturnTarget(); 65 JMethod[] methods = ((ConstantsWithLookupImplCreator) currentCreator).allInterfaceMethods; 66 for (int i = 0; i < methods.length; i++) { 67 if (methods[i].getReturnType().equals(returnType) 68 && methods[i] != targetMethod) { 69 String methodName = methods[i].getName(); 70 String body = "if(arg0.equals(" + wrap(methodName) + ")){"; 71 println(body); 72 printFound(methodName); 73 println("}"); 74 } 75 } 76 String format = "throw new java.util.MissingResourceException(\"Cannot find constant ''\" + {0} + \"''; expecting a method name\", \"{1}\", {0});"; 77 String [] throwArgs = { 78 "arg0", this.currentCreator.getTarget().getQualifiedSourceName()}; 79 String result = MessageFormat.format(format, throwArgs); 80 println(result); 81 } 82 83 void printFound(String methodName) { 84 Object [] args2 = {methodName}; 85 println(MessageFormat.format(returnTemplate(), args2)); 86 } 87 88 void printReturnTarget() { 89 println("return target;"); 90 } 91 92 String returnTemplate() { 93 return "return {0}();"; 94 } 95 } 96 | Popular Tags |