Selaa lähdekoodia

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
tags/rel_3_17_1_ga
chiba 20 vuotta sitten
vanhempi
commit
e057bbef56

+ 1
- 1
src/main/javassist/CtNewWrappedMethod.java Näytä tiedosto

@@ -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);

+ 38
- 4
src/main/javassist/bytecode/ClassFile.java Näytä tiedosto

@@ -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();

+ 17
- 0
src/main/javassist/bytecode/Descriptor.java Näytä tiedosto

@@ -321,6 +321,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.

Loading…
Peruuta
Tallenna