KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javassist > CtNewClass


1 /*
2  * Javassist, a Java-bytecode translator toolkit.
3  * Copyright (C) 1999-2005 Shigeru Chiba. All Rights Reserved.
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. Alternatively, the contents of this file may be used under
8  * the terms of the GNU Lesser General Public License Version 2.1 or later.
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  */

15
16 package javassist;
17
18 import java.io.DataOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import javassist.bytecode.ClassFile;
21
22 class CtNewClass extends CtClassType {
23     /* true if the class is an interface.
24      */

25     protected boolean hasConstructor;
26
27     CtNewClass(String JavaDoc name, ClassPool cp,
28                boolean isInterface, CtClass superclass) {
29         super(name, cp);
30         wasChanged = true;
31         eraseCache();
32         String JavaDoc superName;
33         if (superclass == null)
34             superName = null;
35         else
36             superName = superclass.getName();
37
38         classfile = new ClassFile(isInterface, name, superName);
39
40         setModifiers(Modifier.setPublic(getModifiers()));
41         hasConstructor = isInterface;
42     }
43
44     protected void extendToString(StringBuffer JavaDoc buffer) {
45         if (hasConstructor)
46             buffer.append("hasConstructor ");
47
48         super.extendToString(buffer);
49     }
50
51     public void addConstructor(CtConstructor c)
52         throws CannotCompileException
53     {
54         hasConstructor = true;
55         super.addConstructor(c);
56     }
57
58     public void toBytecode(DataOutputStream JavaDoc out)
59         throws CannotCompileException, IOException JavaDoc
60     {
61         if (!hasConstructor)
62             try {
63                 inheritAllConstructors();
64                 hasConstructor = true;
65             }
66             catch (NotFoundException e) {
67                 throw new CannotCompileException(e);
68             }
69
70         super.toBytecode(out);
71     }
72
73     /**
74      * Adds constructors inhrited from the super class.
75      *
76      * <p>After this method is called, the class inherits all the
77      * constructors from the super class. The added constructor
78      * calls the super's constructor with the same signature.
79      */

80     public void inheritAllConstructors()
81         throws CannotCompileException, NotFoundException
82     {
83         CtClass superclazz;
84         CtConstructor[] cs;
85
86         superclazz = getSuperclass();
87         cs = superclazz.getDeclaredConstructors();
88
89         int n = 0;
90         for (int i = 0; i < cs.length; ++i) {
91             CtConstructor c = cs[i];
92             if (Modifier.isPublic(c.getModifiers())) {
93                 CtConstructor cons
94                     = CtNewConstructor.make(c.getParameterTypes(),
95                                             c.getExceptionTypes(), this);
96                 addConstructor(cons);
97                 ++n;
98             }
99         }
100
101         if (n < 1)
102             throw new CannotCompileException(
103                         "no public constructor in " + superclazz.getName());
104
105     }
106 }
107
Popular Tags