diff options
author | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2004-03-14 17:15:16 +0000 |
---|---|---|
committer | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2004-03-14 17:15:16 +0000 |
commit | e057bbef56510c2fb8b850c27ef4217ff7bd602d (patch) | |
tree | 9fdefd76ab8b9be8f448a083fbca72d274fcece0 /src | |
parent | 805dd3c4f866a2840dcb7346983aa9135b8687d1 (diff) | |
download | javassist-e057bbef56510c2fb8b850c27ef4217ff7bd602d.tar.gz javassist-e057bbef56510c2fb8b850c27ef4217ff7bd602d.zip |
changed to throw an exception if a duplicate method/field is added.
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@73 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
Diffstat (limited to 'src')
-rw-r--r-- | src/main/javassist/CtNewWrappedMethod.java | 2 | ||||
-rw-r--r-- | src/main/javassist/bytecode/ClassFile.java | 42 | ||||
-rw-r--r-- | src/main/javassist/bytecode/Descriptor.java | 17 |
3 files changed, 56 insertions, 5 deletions
diff --git a/src/main/javassist/CtNewWrappedMethod.java b/src/main/javassist/CtNewWrappedMethod.java index 71f6287f..d960eb3c 100644 --- a/src/main/javassist/CtNewWrappedMethod.java +++ b/src/main/javassist/CtNewWrappedMethod.java @@ -131,7 +131,7 @@ class CtNewWrappedMethod { private static String addBodyMethod(CtClassType clazz, ClassFile classfile, CtMethod src) - throws BadBytecode + throws BadBytecode, CannotCompileException { Hashtable bodies = clazz.getHiddenMethods(); String bodyname = (String)bodies.get(src); diff --git a/src/main/javassist/bytecode/ClassFile.java b/src/main/javassist/bytecode/ClassFile.java index 85df2243..1cff98b2 100644 --- a/src/main/javassist/bytecode/ClassFile.java +++ b/src/main/javassist/bytecode/ClassFile.java @@ -20,6 +20,7 @@ import java.io.DataOutputStream; import java.io.IOException; import java.util.Map; import java.util.LinkedList; +import java.util.ListIterator; import java.util.List; import javassist.CannotCompileException; @@ -333,10 +334,26 @@ public final class ClassFile { /** * Appends a field to the class. */ - public void addField(FieldInfo finfo) { + public void addField(FieldInfo finfo) throws CannotCompileException { + testExistingField(finfo.getName(), finfo.getDescriptor()); fields.add(finfo); } + private void addField0(FieldInfo finfo) { + fields.add(finfo); + } + + private void testExistingField(String name, String descriptor) + throws CannotCompileException + { + ListIterator it = fields.listIterator(0); + while (it.hasNext()) { + FieldInfo minfo = (FieldInfo)it.next(); + if (minfo.getName().equals(name)) + throw new CannotCompileException("duplicated field: " + name); + } + } + /** * Returns all the methods declared in the class. * @@ -374,9 +391,26 @@ public final class ClassFile { /** * Appends a method to the class. */ - public void addMethod(MethodInfo minfo) { + public void addMethod(MethodInfo minfo) throws CannotCompileException { + testExistingMethod(minfo.getName(), minfo.getDescriptor()); + methods.add(minfo); + } + + private void addMethod0(MethodInfo minfo) { methods.add(minfo); } + + private void testExistingMethod(String name, String descriptor) + throws CannotCompileException + { + ListIterator it = methods.listIterator(0); + while (it.hasNext()) { + MethodInfo minfo = (MethodInfo)it.next(); + if (minfo.getName().equals(name) + && Descriptor.eqSignature(minfo.getDescriptor(), descriptor)) + throw new CannotCompileException("duplicated method: " + name); + } + } /** * Returns all the attributes. @@ -452,12 +486,12 @@ public final class ClassFile { n = in.readUnsignedShort(); fields = new LinkedList(); for (i = 0; i < n; ++i) - addField(new FieldInfo(cp, in)); + addField0(new FieldInfo(cp, in)); n = in.readUnsignedShort(); methods = new LinkedList(); for (i = 0; i < n; ++i) - addMethod(new MethodInfo(cp, in)); + addMethod0(new MethodInfo(cp, in)); attributes = new LinkedList(); n = in.readUnsignedShort(); diff --git a/src/main/javassist/bytecode/Descriptor.java b/src/main/javassist/bytecode/Descriptor.java index 0b9635ff..fd9663aa 100644 --- a/src/main/javassist/bytecode/Descriptor.java +++ b/src/main/javassist/bytecode/Descriptor.java @@ -322,6 +322,23 @@ public class Descriptor { } /** + * Returns true if desc1 and desc2 has the same signature. + */ + public static boolean eqSignature(String desc1, String desc2) { + if (desc1.charAt(0) != '(') + return false; + + for (int i = 0; true; ++i) { + char c = desc1.charAt(i); + if (c != desc2.charAt(i)) + return false; + + if (c == ')') + return true; + } + } + + /** * Returns the <code>CtClass</code> object representing the return * type specified by the given descriptor. * |