001: /*
002: * Javassist, a Java-bytecode translator toolkit.
003: * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.
004: *
005: * The contents of this file are subject to the Mozilla Public License Version
006: * 1.1 (the "License"); you may not use this file except in compliance with
007: * the License. Alternatively, the contents of this file may be used under
008: * the terms of the GNU Lesser General Public License Version 2.1 or later.
009: *
010: * Software distributed under the License is distributed on an "AS IS" basis,
011: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012: * for the specific language governing rights and limitations under the
013: * License.
014: */
015:
016: package javassist;
017:
018: import java.io.DataOutputStream;
019: import java.io.IOException;
020: import javassist.bytecode.ClassFile;
021:
022: class CtNewClass extends CtClassType {
023: /* true if the class is an interface.
024: */
025: protected boolean hasConstructor;
026:
027: CtNewClass(String name, ClassPool cp, boolean isInterface,
028: CtClass super class) {
029: super (name, cp);
030: wasChanged = true;
031: eraseCache();
032: String super Name;
033: if (super class == null)
034: super Name = null;
035: else
036: super Name = super class.getName();
037:
038: classfile = new ClassFile(isInterface, name, super Name);
039:
040: setModifiers(Modifier.setPublic(getModifiers()));
041: hasConstructor = isInterface;
042: }
043:
044: protected void extendToString(StringBuffer buffer) {
045: if (hasConstructor)
046: buffer.append("hasConstructor ");
047:
048: super .extendToString(buffer);
049: }
050:
051: public void addConstructor(CtConstructor c)
052: throws CannotCompileException {
053: hasConstructor = true;
054: super .addConstructor(c);
055: }
056:
057: public void toBytecode(DataOutputStream out)
058: throws CannotCompileException, IOException {
059: if (!hasConstructor)
060: try {
061: inheritAllConstructors();
062: hasConstructor = true;
063: } catch (NotFoundException e) {
064: throw new CannotCompileException(e);
065: }
066:
067: super .toBytecode(out);
068: }
069:
070: /**
071: * Adds constructors inhrited from the super class.
072: *
073: * <p>After this method is called, the class inherits all the
074: * constructors from the super class. The added constructor
075: * calls the super's constructor with the same signature.
076: */
077: public void inheritAllConstructors() throws CannotCompileException,
078: NotFoundException {
079: CtClass super clazz;
080: CtConstructor[] cs;
081:
082: super clazz = getSuperclass();
083: cs = super clazz.getDeclaredConstructors();
084:
085: int n = 0;
086: for (int i = 0; i < cs.length; ++i) {
087: CtConstructor c = cs[i];
088: if (Modifier.isPublic(c.getModifiers())) {
089: CtConstructor cons = CtNewConstructor.make(c
090: .getParameterTypes(), c.getExceptionTypes(),
091: this );
092: addConstructor(cons);
093: ++n;
094: }
095: }
096:
097: if (n < 1)
098: throw new CannotCompileException(
099: "no public constructor in " + superclazz.getName());
100:
101: }
102: }
|