001: /*
002: * transformica 2
003: * Code generator
004: * Copyright (C) 2004 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.pavelvlasov.com/pv/content/menu.show@id=products.transformica.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.transformica.jdbc;
024:
025: import java.io.IOException;
026:
027: import org.apache.bcel.Constants;
028: import org.apache.bcel.classfile.JavaClass;
029: import org.apache.bcel.generic.ClassGen;
030: import org.apache.bcel.generic.InstructionList;
031: import org.apache.bcel.generic.MethodGen;
032: import org.apache.bcel.generic.Type;
033: import org.apache.bcel.util.ClassLoaderRepository;
034:
035: /**
036: * Class for testing BCEL API.
037: * @author Pavel Vlasov
038: * @version $Revision: 1.1 $
039: */
040: public class ClassGenTest {
041:
042: /**
043: *
044: */
045: public ClassGenTest() {
046: super ();
047: // TODO Auto-generated constructor stub
048: }
049:
050: public static void main(String[] args) throws IOException,
051: ClassNotFoundException {
052: String className = "biz.hammurapi.transformica.jdbc.ClassGenTestInterface";
053: Class baseClass = ResultSetInvocationHandler.class;
054:
055: ClassLoaderRepository clr = new ClassLoaderRepository(Thread
056: .currentThread().getContextClassLoader());
057:
058: JavaClass baseJavaClass = clr.loadClass(baseClass);
059:
060: ClassGen cg = new ClassGen(className, "java.lang.Object", //baseClass.getName(),
061: "<generated>", Constants.ACC_PUBLIC
062: | Constants.ACC_INTERFACE, null);
063:
064: System.out.println("Is interface: " + cg.isInterface());
065:
066: // Method[] m=baseJavaClass.getMethods();
067: // Method getXXX=null;
068: //
069: // for (int i=0; i<m.length; i++) {
070: // if ("getXXX".equals(m[i].getName())) {
071: // ConstantPoolGen methodPool=new ConstantPoolGen();
072: // MethodGen mg = new MethodGen(m[i], className, methodPool);
073: // //mg.setName("getZZZ");
074: // System.out.println(methodPool);
075: //
076: // cg.addMethod(mg.getMethod());
077: // break;
078: // }
079: // }
080:
081: MethodGen mg = new MethodGen(Constants.ACC_PUBLIC
082: | Constants.ACC_ABSTRACT,
083: Type.STRING, // return type
084: null, null, "getName", className,
085: new InstructionList(), cg.getConstantPool());
086:
087: cg.addMethod(mg.getMethod());
088:
089: class InjectingClassLoader extends ClassLoader {
090: public InjectingClassLoader(ClassLoader parent) {
091: super (parent);
092: }
093:
094: public void defineClass(ClassGen classGen) {
095: JavaClass jc = classGen.getJavaClass();
096: byte[] classBytes = jc.getBytes();
097: defineClass(jc.getClassName(), classBytes, 0,
098: classBytes.length);
099: }
100: }
101: ;
102:
103: InjectingClassLoader icl = new InjectingClassLoader(Thread
104: .currentThread().getContextClassLoader());
105: icl.defineClass(cg);
106: Class c = icl.loadClass(className);
107:
108: System.out.println(c);
109:
110: // Method[] m=jc.getMethods();
111: //
112: // for (int i=0; i<m.length; i++) {
113: // System.out.println(m[i].getName());
114: // System.out.println(m[i]);
115: // }
116: //
117: // System.out.println(jc.getBytes().length);
118: //
119: // jc.dump("ClassGenTestInterface.class");
120: //
121: // jc=new ClassParser("ClassGenTestInterface.class").parse();
122: //
123: // m=jc.getMethods();
124: //
125: // for (int i=0; i<m.length; i++) {
126: // System.out.println(m[i].getName());
127: // System.out.println(m[i]);
128: // }
129: }
130: }
|