1 17 package org.apache.ws.jaxme.js.pattern; 18 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import org.apache.ws.jaxme.js.JavaConstructor; 25 import org.apache.ws.jaxme.js.JavaField; 26 import org.apache.ws.jaxme.js.JavaMethod; 27 import org.apache.ws.jaxme.js.JavaQName; 28 import org.apache.ws.jaxme.js.JavaQNameImpl; 29 import org.apache.ws.jaxme.js.JavaSource; 30 import org.apache.ws.jaxme.js.JavaSourceFactory; 31 import org.apache.ws.jaxme.js.Parameter; 32 33 34 47 public class ProxyGenerator { 48 private JavaQName extendedClass; 49 50 53 public JavaQName getExtendedClass() { 54 return extendedClass; 55 } 56 57 60 public void setExtendedClass(JavaQName pExtendedClass) { 61 extendedClass = pExtendedClass; 62 } 63 64 67 protected JavaMethod getInterfaceMethod(JavaSource pJs, 68 JavaMethod pMethod) { 69 JavaMethod jm = pJs.newJavaMethod(pMethod); 70 Parameter[] parameters = jm.getParams(); 71 List callParameters = new ArrayList (); 72 for (int i = 0; i < parameters.length; i++) { 73 Parameter parameter = parameters[i]; 74 if (callParameters.size() > 0) { 75 callParameters.add(", "); 76 } 77 callParameters.add(parameter.getName()); 78 } 79 if (pMethod.getType().equals(JavaQNameImpl.VOID)) { 80 jm.addLine("((", pMethod.getJavaSource().getQName(), ") backingObject).", 81 pMethod.getName(), "(", callParameters, ");"); 82 } else { 83 jm.addLine("return ", 84 "((", pMethod.getJavaSource().getQName(), ") backingObject).", 85 pMethod.getName(), "(", callParameters, ");"); 86 } 87 return jm; 88 } 89 90 99 protected void generateInterfaceMethods(JavaSource pJs, Map pGeneratedMethods, 100 JavaSource pInterface) 101 throws ClassNotFoundException { 102 JavaMethod[] methods = pInterface.getMethods(); 103 for (int i = 0; i < methods.length; i++) { 104 JavaMethod method = methods[i]; 105 if (method.isStatic()) { 106 continue; 107 } 108 if (!JavaSource.PUBLIC.equals(method.getProtection())) { 109 continue; 110 } 111 MethodKey key = new MethodKey(method); 112 JavaMethod existingMethod = (JavaMethod) pGeneratedMethods.get(key); 113 if (existingMethod == null) { 114 JavaMethod generatedMethod = getInterfaceMethod(pJs, method); 115 pGeneratedMethods.put(key, generatedMethod); 116 } else { 117 System.err.println("The methods " 118 + existingMethod.getJavaSource().getQName() 119 + "." + existingMethod.getName() + " and " 120 + pInterface.getQName() + "." 121 + method.getName() + " are identical, ignoring the latter."); 122 } 123 } 124 } 125 126 129 protected JavaConstructor getConstructor(JavaSource pJs, 130 InterfaceDescription[] pInterfaces) 131 throws Exception { 132 JavaConstructor jcon = pJs.newJavaConstructor(JavaSource.PROTECTED); 133 jcon.addParam(Object .class, "o"); 134 jcon.addIf("o == null"); 135 jcon.addThrowNew(NullPointerException .class, 136 JavaSource.getQuoted("The supplied object must not be null.")); 137 jcon.addEndIf(); 138 for (int i = 0; i < pInterfaces.length; i++) { 139 if (pInterfaces[i].isMandatory()) { 140 JavaSource js = pInterfaces[i].getJavaSource(); 141 jcon.addIf("!(o instanceof ", js.getQName(), ")"); 142 jcon.addThrowNew(ClassCastException .class, 143 JavaSource.getQuoted("The supplied instance of "), 144 " + o.getClass().getName() + ", 145 JavaSource.getQuoted(" is not implementing "), 146 " + ", js.getQName(), ".class.getName()"); 147 jcon.addEndIf(); 148 } 149 } 150 jcon.addLine("backingObject = o;"); 151 return jcon; 152 } 153 154 156 protected JavaSource getJavaSource(JavaSourceFactory pFactory, JavaQName pTargetName) { 157 return pFactory.newJavaSource(pTargetName, JavaSource.PUBLIC); 158 } 159 160 162 protected JavaField getBackingObjectField(JavaSource pJs, InterfaceDescription[] pInterfaces) { 163 return pJs.newJavaField("backingObject", Object .class, JavaSource.PRIVATE); 164 } 165 166 173 public JavaSource generate(JavaSourceFactory pFactory, JavaQName pTargetName, 174 InterfaceDescription[] pInterfaces) 175 throws Exception { 176 JavaSource js = getJavaSource(pFactory, pTargetName); 177 if (getExtendedClass() != null) { 178 js.addExtends(getExtendedClass()); 179 } 180 for (int i = 0; i < pInterfaces.length; i++) { 181 JavaQName qName = pInterfaces[i].getJavaSource().getQName(); 182 js.addImplements(qName); 183 } 184 185 getBackingObjectField(js, pInterfaces); 186 getConstructor(js, pInterfaces); 187 188 Map methods = new HashMap (); 189 for (int i = 0; i < pInterfaces.length; i++) { 190 generateInterfaceMethods(js, methods, pInterfaces[i].getJavaSource()); 191 } 192 193 return js; 194 } 195 } 196 | Popular Tags |