From 066ce8b8722d191874c53870096d3cb2e1056f1d Mon Sep 17 00:00:00 2001 From: chiba Date: Thu, 8 Jul 2010 11:01:36 +0000 Subject: [PATCH] fixed JASSIST-119 git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@552 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- src/main/javassist/CtClass.java | 37 +++++++++++++++++++++++-- src/main/javassist/CtClassType.java | 43 +++++++++++++++++++---------- src/main/javassist/CtField.java | 2 ++ 3 files changed, 64 insertions(+), 18 deletions(-) diff --git a/src/main/javassist/CtClass.java b/src/main/javassist/CtClass.java index 5a11b999..bbf21b87 100644 --- a/src/main/javassist/CtClass.java +++ b/src/main/javassist/CtClass.java @@ -52,7 +52,7 @@ public abstract class CtClass { /** * The version number of this release. */ - public static final String version = "3.12.0.GA"; + public static final String version = "3.13.0.GA"; /** * Prints the version number and the copyright notice. @@ -681,13 +681,28 @@ public abstract class CtClass { * may be a private field declared in a super class or interface. */ public CtField getField(String name) throws NotFoundException { + return getField(name, null); + } + + /** + * Returns the field with the specified name and type. The returned field + * may be a private field declared in a super class or interface. + * Unlike Java, the JVM allows a class to have + * multiple fields with the same name but different types. + * + * @param name the field name. + * @param desc the type descriptor of the field. It is available by + * {@link CtField#getSignature()}. + * @see CtField#getSignature() + */ + public CtField getField(String name, String desc) throws NotFoundException { throw new NotFoundException(name); } /** * @return null if the specified field is not found. */ - CtField getField2(String name) { return null; } + CtField getField2(String name, String desc) { return null; } /** * Gets all the fields declared in the class. The inherited fields @@ -701,12 +716,28 @@ public abstract class CtClass { * Retrieves the field with the specified name among the fields * declared in the class. * - *

Note: this method does not search the superclasses. + *

Note: this method does not search the super classes. */ public CtField getDeclaredField(String name) throws NotFoundException { throw new NotFoundException(name); } + /** + * Retrieves the field with the specified name and type among the fields + * declared in the class. Unlike Java, the JVM allows a class to have + * multiple fields with the same name but different types. + * + *

Note: this method does not search the super classes. + * + * @param name the field name. + * @param desc the type descriptor of the field. It is available by + * {@link CtField#getSignature()}. + * @see CtField#getSignature() + */ + public CtField getDeclaredField(String name, String desc) throws NotFoundException { + throw new NotFoundException(name); + } + /** * Gets all the constructors and methods declared in the class. */ diff --git a/src/main/javassist/CtClassType.java b/src/main/javassist/CtClassType.java index 46a7361f..0dcbc82b 100644 --- a/src/main/javassist/CtClassType.java +++ b/src/main/javassist/CtClassType.java @@ -893,16 +893,27 @@ class CtClassType extends CtClass { } } - public CtField getField(String name) throws NotFoundException { - CtField f = getField2(name); - if (f == null) - throw new NotFoundException("field: " + name + " in " + getName()); + public CtField getField(String name, String desc) throws NotFoundException { + CtField f = getField2(name, desc); + return checkGetField(f, name, desc); + } + + private CtField checkGetField(CtField f, String name, String desc) + throws NotFoundException + { + if (f == null) { + String msg = "field: " + name; + if (desc != null) + msg += " type " + desc; + + throw new NotFoundException(msg + " in " + getName()); + } else return f; } - CtField getField2(String name) { - CtField df = getDeclaredField2(name); + CtField getField2(String name, String desc) { + CtField df = getDeclaredField2(name, desc); if (df != null) return df; @@ -910,14 +921,14 @@ class CtClassType extends CtClass { CtClass[] ifs = getInterfaces(); int num = ifs.length; for (int i = 0; i < num; ++i) { - CtField f = ifs[i].getField2(name); + CtField f = ifs[i].getField2(name, desc); if (f != null) return f; } CtClass s = getSuperclass(); if (s != null) - return s.getField2(name); + return s.getField2(name, desc); } catch (NotFoundException e) {} return null; @@ -939,20 +950,22 @@ class CtClassType extends CtClass { } public CtField getDeclaredField(String name) throws NotFoundException { - CtField f = getDeclaredField2(name); - if (f == null) - throw new NotFoundException("field: " + name + " in " + getName()); - else - return f; + return getDeclaredField(name, null); + } + + public CtField getDeclaredField(String name, String desc) throws NotFoundException { + CtField f = getDeclaredField2(name, desc); + return checkGetField(f, name, desc); } - private CtField getDeclaredField2(String name) { + private CtField getDeclaredField2(String name, String desc) { CtMember.Cache memCache = getMembers(); CtMember field = memCache.fieldHead(); CtMember tail = memCache.lastField(); while (field != tail) { field = field.next(); - if (field.getName().equals(name)) + if (field.getName().equals(name) + && (desc == null || desc.equals(field.getSignature()))) return (CtField)field; } diff --git a/src/main/javassist/CtField.java b/src/main/javassist/CtField.java index af573d2c..c4af7e5d 100644 --- a/src/main/javassist/CtField.java +++ b/src/main/javassist/CtField.java @@ -319,6 +319,8 @@ public class CtField extends CtMember { /** * Returns the character string representing the type of the field. + * The field signature is represented by a character string + * called a field descriptor, which is defined in the JVM specification. * If two fields have the same type, * getSignature() returns the same string. * -- 2.39.5