1 16 package com.google.gwt.i18n.rebind; 17 18 import com.google.gwt.core.ext.TreeLogger; 19 import com.google.gwt.core.ext.UnableToCompleteException; 20 import com.google.gwt.core.ext.typeinfo.JClassType; 21 import com.google.gwt.core.ext.typeinfo.JMethod; 22 import com.google.gwt.core.ext.typeinfo.JParameter; 23 import com.google.gwt.core.ext.typeinfo.JType; 24 import com.google.gwt.core.ext.typeinfo.TypeOracle; 25 import com.google.gwt.core.ext.typeinfo.TypeOracleException; 26 import com.google.gwt.i18n.rebind.util.AbstractResource; 27 import com.google.gwt.user.rebind.AbstractMethodCreator; 28 import com.google.gwt.user.rebind.SourceWriter; 29 30 import java.util.HashMap ; 31 import java.util.Map ; 32 33 class ConstantsWithLookupImplCreator extends ConstantsImplCreator { 34 final JMethod[] allInterfaceMethods; 35 36 private final Map namesToMethodCreators = new HashMap (); 37 38 ConstantsWithLookupImplCreator(TreeLogger logger, SourceWriter writer, 39 JClassType localizableClass, AbstractResource messageBindings, 40 TypeOracle oracle) throws UnableToCompleteException { 41 super(logger, writer, localizableClass, messageBindings, oracle); 42 try { 43 44 JType booleanType = oracle.parse(boolean.class.getName()); 46 LookupMethodCreator booleanMethod = new LookupMethodCreator(this, 47 booleanType) { 48 public void printReturnTarget() { 49 println("return target.booleanValue();"); 50 } 51 52 public String returnTemplate() { 53 return "boolean answer = {0}();\n cache.put(\"{0}\",new Boolean(answer));return answer;"; 54 } 55 }; 56 namesToMethodCreators.put("getBoolean", booleanMethod); 57 58 JType doubleType = oracle.parse(double.class.getName()); 60 LookupMethodCreator doubleMethod = new LookupMethodCreator(this, 61 doubleType) { 62 public void printReturnTarget() { 63 println("return target.doubleValue();"); 64 } 65 66 public String returnTemplate() { 67 return "double answer = {0}();\n cache.put(\"{0}\",new Double(answer));return answer;"; 68 } 69 }; 70 namesToMethodCreators.put("getDouble", doubleMethod); 71 72 JType intType = oracle.parse(int.class.getName()); 74 LookupMethodCreator intMethod = new LookupMethodCreator(this, intType) { 75 public void printReturnTarget() { 76 println("return target.intValue();"); 77 } 78 79 public String returnTemplate() { 80 return "int answer = {0}();\n cache.put(\"{0}\",new Integer(answer));return answer;"; 81 } 82 }; 83 84 namesToMethodCreators.put("getInt", intMethod); 85 86 JType floatType = oracle.parse(float.class.getName()); 88 LookupMethodCreator floatMethod = new LookupMethodCreator(this, floatType) { 89 public String returnTemplate() { 90 String val = "float v ={0}(); cache.put(\"{0}\", new Float(v));return v;"; 91 return val; 92 } 93 94 protected void printReturnTarget() { 95 println("return target.floatValue();"); 96 } 97 }; 98 namesToMethodCreators.put("getFloat", floatMethod); 99 100 JType mapType = oracle.parse(Map .class.getName()); 102 namesToMethodCreators.put("getMap", 103 new LookupMethodCreator(this, mapType)); 104 105 JType stringType = oracle.parse(String .class.getName()); 107 LookupMethodCreator stringMethod = new LookupMethodCreator(this, 108 stringType) { 109 public String returnTemplate() { 110 return "String answer = {0}();\n cache.put(\"{0}\",answer);return answer;"; 111 } 112 }; 113 namesToMethodCreators.put("getString", stringMethod); 114 115 JType stringArray = oracle.getArrayType(stringType); 117 namesToMethodCreators.put("getStringArray", new LookupMethodCreator(this, 118 stringArray)); 119 120 setNeedCache(true); 121 allInterfaceMethods = getAllInterfaceMethods(localizableClass); 122 } catch (TypeOracleException e) { 123 throw error(logger, e); 124 } 125 } 126 127 131 protected void emitMethodBody(TreeLogger logger, JMethod method) 132 throws UnableToCompleteException { 133 checkMethod(logger, method); 134 if (method.getParameters().length == 1) { 135 String name = method.getName(); 136 AbstractMethodCreator c = (AbstractMethodCreator) namesToMethodCreators.get(name); 137 if (c != null) { 138 c.createMethodFor(logger, method, null); 139 return; 140 } 141 } 142 super.emitMethodBody(logger, method); 144 } 145 146 152 private void checkMethod(TreeLogger logger, JMethod method) 153 throws UnableToCompleteException { 154 if (namesToMethodCreators.get(method.getName()) != null) { 155 JParameter[] params = method.getParameters(); 156 if (params.length == 0) { 158 return; 159 } 160 if (params.length != 1 161 || !params[0].getType().getQualifiedSourceName().equals( 162 "java.lang.String")) { 163 String s = method + " must have a single String argument."; 164 throw error(logger, s); 165 } 166 } else { 167 if (method.getParameters().length > 0) { 168 throw error( 169 logger, 170 "User-defined methods in interfaces extending ConstantsWithLookup must have no parameters and a return type of int, String, String[], ..."); 171 } 172 } 173 } 174 } 175 | Popular Tags |