1 19 package org.netbeans.mdr.handlers; 20 21 import org.netbeans.mdr.util.ImplGenerator; 22 23 import java.io.*; 24 import java.lang.reflect.Method ; 25 import java.util.*; 26 27 32 public class StructGenerator extends ImplGenerator { 33 private static final String GET_PREFIX = "get"; private static final String IS_PREFIX = "is"; 36 private static final String M_GET_NAME = "handleGet"; private static final String M_GET_DESC = "(Ljava/lang/String;)Ljava/lang/Object;"; 39 private static final String CONSTRUCTOR_DESCRIPTOR = "(Ljava/util/List;Ljava/util/Map;Ljava/util/List;)V"; 41 private StructGenerator(String className, Class ifc) { 42 super(className, ifc, StructImpl.class); 43 } 44 45 public static byte[] generateStruct(final String name, Class ifc) { 46 StructGenerator gen = new StructGenerator(name, ifc); 47 final byte[] classFile = gen.generateClassFile(); 48 49 return classFile; 50 } 51 52 protected Method [] getMethodsToImplement() { 53 Method ifcMethods[] = ifc.getMethods(); 54 Method objMethods[] = javax.jmi.reflect.RefStruct.class.getMethods(); 55 HashSet excludeMethods = new HashSet(); 56 ArrayList methodsToImplement = new ArrayList(); 57 58 for (int i = 0; i < objMethods.length; i++) { 59 excludeMethods.add(objMethods[i].getName() + getMethodDescriptor(objMethods[i].getParameterTypes(), objMethods[i].getReturnType())); 60 } 61 62 for (int i = 0; i < ifcMethods.length; i++) { 63 if (!excludeMethods.contains(ifcMethods[i].getName() + getMethodDescriptor(ifcMethods[i].getParameterTypes(), ifcMethods[i].getReturnType()))) { 64 methodsToImplement.add(ifcMethods[i]); 65 } 66 } 67 68 return (Method []) methodsToImplement.toArray(new Method [methodsToImplement.size()]); 69 } 70 71 74 protected MethodInfo generateConstructor() throws IOException { 75 MethodInfo minfo = new MethodInfo( 76 "<init>", CONSTRUCTOR_DESCRIPTOR, ACC_PUBLIC); 78 79 DataOutputStream out = new DataOutputStream(minfo.code); 80 81 code_aload(0, out); 83 code_aload(1, out); 85 code_aload(2, out); 87 code_aload(3, out); 89 90 out.writeByte(opc_invokespecial); 92 out.writeShort(cp.getMethodRef( 93 dotToSlash(superclass.getName()), 94 "<init>", CONSTRUCTOR_DESCRIPTOR)); 96 out.writeByte(opc_return); 97 98 minfo.maxStack = 10; 99 minfo.maxLocals = 4; 100 minfo.declaredExceptions = new short[0]; 101 102 return minfo; 103 } 104 105 protected ClassMethod getClassMethod(Method method, Class fromClass) { 106 String methodName = method.getName(); 107 ClassMethod am; 108 109 if (methodName.startsWith(GET_PREFIX)) { 110 am = new ClassMethod(method, cp.getMethodRef(dotToSlash(superclass.getName()), M_GET_NAME, M_GET_DESC), firstLower(strip(methodName, GET_PREFIX))); 111 } else if (methodName.startsWith(IS_PREFIX)) { 112 am = new ClassMethod(method, cp.getMethodRef(dotToSlash(superclass.getName()), M_GET_NAME, M_GET_DESC), firstLower(methodName)); 113 } else { 114 am = super.getClassMethod(method, fromClass); 115 } 116 117 return am; 118 } 119 } 120 | Popular Tags |