1 16 package com.google.gwt.user.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 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 29 32 public abstract class AbstractGeneratorClassCreator extends 33 AbstractSourceCreator { 34 35 41 public static JMethod[] getAllInterfaceMethods(JClassType type) { 42 Map methods = new HashMap (); 43 getAllInterfaceMethodsAux(type, methods); 44 return (JMethod[]) methods.values().toArray(new JMethod[methods.size()]); 45 } 46 47 private static void getAllInterfaceMethodsAux(JClassType type, Map m) { 48 if (type.isInterface() != null) { 49 JMethod[] methods = type.getMethods(); 50 for (int i = 0; i < methods.length; i++) { 51 String s = uniqueMethodKey(methods[i]); 52 if (m.get(s) == null) { 53 m.put(s, methods[i]); 54 } 55 } 56 JClassType[] supers = type.getImplementedInterfaces(); 57 for (int i = 0; i < supers.length; i++) { 58 getAllInterfaceMethodsAux(supers[i], m); 59 } 60 } 61 } 62 63 private static String uniqueMethodKey(JMethod method) { 64 String name = method.getName(); 65 name += "("; 66 JParameter[] m = method.getParameters(); 67 for (int i = 0; i < m.length; i++) { 68 name += m[i].getType() + " "; 69 } 70 name += ")"; 71 return name; 72 } 73 74 78 protected Map methodFactories = new HashMap (); 79 80 83 JClassType targetClass; 84 85 private final SourceWriter writer; 86 87 94 public AbstractGeneratorClassCreator(SourceWriter writer, 95 JClassType targetClass) { 96 this.targetClass = targetClass; 97 this.writer = writer; 98 } 99 100 106 public void emitClass(TreeLogger logger) throws UnableToCompleteException { 107 logger = branch(logger, branchMessage()); 108 classPrologue(); 109 emitMethods(logger, targetClass); 110 classEpilog(); 111 getWriter().println("}"); 112 } 113 114 public JClassType getTarget() { 115 return targetClass; 116 } 117 118 124 public void register(JType returnType, AbstractMethodCreator creator) { 125 methodFactories.put(returnType, creator); 126 } 127 128 133 protected String branchMessage() { 134 return "Constructing " + targetClass; 135 } 136 137 140 protected void classEpilog() { 141 } 142 143 146 protected void classPrologue() { 147 } 148 149 156 protected abstract void emitMethodBody(TreeLogger logger, JMethod method) 157 throws UnableToCompleteException; 158 159 167 protected AbstractMethodCreator getMethodCreator(TreeLogger logger, 168 JMethod method) throws UnableToCompleteException { 169 JType type = method.getReturnType(); 170 AbstractMethodCreator methodCreator = (AbstractMethodCreator) methodFactories.get(type); 171 if (methodCreator == null) { 172 String msg = "No current method creator exists for " + method 173 + " only methods with return types of "; 174 Iterator iter = this.methodFactories.keySet().iterator(); 175 while (iter.hasNext()) { 176 msg += ((JType) iter.next()).getSimpleSourceName(); 177 if (iter.hasNext()) { 178 msg += ", "; 179 } 180 } 181 msg += " can be created"; 182 throw error(logger, msg); 183 } 184 return methodCreator; 185 } 186 187 192 protected SourceWriter getWriter() { 193 return writer; 194 } 195 196 private void emitMethods(TreeLogger logger, JClassType cur) 197 throws UnableToCompleteException { 198 JMethod[] x = getAllInterfaceMethods(cur); 199 for (int i = 0; i < x.length; i++) { 200 genMethod(logger, x[i]); 201 getWriter().println(); 202 } 203 } 204 205 211 private void genMethod(TreeLogger logger, JMethod method) 212 throws UnableToCompleteException { 213 String name = method.getName(); 214 String returnType = method.getReturnType().getQualifiedSourceName(); 215 getWriter().print("public " + returnType + " " + name + "("); 216 JParameter[] params = method.getParameters(); 217 for (int i = 0; i < params.length; i++) { 218 if (i != 0) { 219 getWriter().print(","); 220 } 221 getWriter().print( 222 params[i].getType().getParameterizedQualifiedSourceName() + " arg" 223 + (i)); 224 } 225 getWriter().println(") {"); 226 getWriter().indent(); 227 String methodName = method.getName(); 228 TreeLogger branch = branch(logger, "Generating method body for " 229 + methodName + "()"); 230 emitMethodBody(branch, method); 231 getWriter().outdent(); 232 getWriter().println("}"); 233 } 234 } 235 | Popular Tags |