KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > handlers > StructGenerator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
25 import java.util.*;
26
27 /**
28  *
29  * @author Martin Matula
30  * @version
31  */

32 public class StructGenerator extends ImplGenerator {
33     private static final String JavaDoc GET_PREFIX = "get"; //NOI18N
34
private static final String JavaDoc IS_PREFIX = "is"; //NOI18N
35

36     private static final String JavaDoc M_GET_NAME = "handleGet"; //NOI18N
37
private static final String JavaDoc M_GET_DESC = "(Ljava/lang/String;)Ljava/lang/Object;"; //NOI18N
38

39     private static final String JavaDoc CONSTRUCTOR_DESCRIPTOR = "(Ljava/util/List;Ljava/util/Map;Ljava/util/List;)V"; //NOI18N
40

41     private StructGenerator(String JavaDoc className, Class JavaDoc ifc) {
42         super(className, ifc, StructImpl.class);
43     }
44
45     public static byte[] generateStruct(final String JavaDoc name, Class JavaDoc ifc) {
46     StructGenerator gen = new StructGenerator(name, ifc);
47     final byte[] classFile = gen.generateClassFile();
48
49     return classFile;
50     }
51
52     protected Method JavaDoc[] getMethodsToImplement() {
53         Method JavaDoc ifcMethods[] = ifc.getMethods();
54         Method JavaDoc 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 JavaDoc[]) methodsToImplement.toArray(new Method JavaDoc[methodsToImplement.size()]);
69     }
70     
71     /**
72      * Generate the constructor method for the proxy class.
73      */

74     protected MethodInfo generateConstructor() throws IOException {
75     MethodInfo minfo = new MethodInfo(
76         "<init>", CONSTRUCTOR_DESCRIPTOR, //NOI18N
77
ACC_PUBLIC);
78
79     DataOutputStream out = new DataOutputStream(minfo.code);
80
81         // this
82
code_aload(0, out);
83         // list containing field names
84
code_aload(1, out);
85         // map containing fields and their values
86
code_aload(2, out);
87         // type qualified name
88
code_aload(3, out);
89
90         // just call super.<init>(String);
91
out.writeByte(opc_invokespecial);
92     out.writeShort(cp.getMethodRef(
93         dotToSlash(superclass.getName()),
94         "<init>", CONSTRUCTOR_DESCRIPTOR)); //NOI18N
95

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 JavaDoc method, Class JavaDoc fromClass) {
106         String JavaDoc 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