From bfdfdff690cdf56edb25ef28740da13dc2c86785 Mon Sep 17 00:00:00 2001 From: chiba Date: Mon, 31 Oct 2005 16:48:44 +0000 Subject: added makeUniqueName() in CtClass. git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@215 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- src/main/javassist/CtClass.java | 13 +++++++ src/main/javassist/CtClassType.java | 68 +++++++++++++++++++++++++++++++++++ src/main/javassist/CtConstructor.java | 21 +++++++++++ src/main/javassist/Modifier.java | 6 ++++ 4 files changed, 108 insertions(+) (limited to 'src') diff --git a/src/main/javassist/CtClass.java b/src/main/javassist/CtClass.java index 6e5196e5..c0962808 100644 --- a/src/main/javassist/CtClass.java +++ b/src/main/javassist/CtClass.java @@ -1199,4 +1199,17 @@ public abstract class CtClass { { throw new CannotCompileException("not a class"); } + + /** + * Makes a unique member name. This method guarantees that + * the returned name is not used as a prefix of any methods + * or fields visible in this class. + * If the returned name is XYZ, then any method or field names + * in this class do not start with XYZ. + * + * @param prefix the prefix of the member name. + */ + public String makeUniqueName(String prefix) { + throw new RuntimeException("not available in " + getName()); + } } diff --git a/src/main/javassist/CtClassType.java b/src/main/javassist/CtClassType.java index 0fb42def..bdf389cd 100644 --- a/src/main/javassist/CtClassType.java +++ b/src/main/javassist/CtClassType.java @@ -26,6 +26,8 @@ import java.util.Enumeration; import java.util.HashMap; import java.util.Hashtable; import java.util.List; +import java.util.Set; + import javassist.bytecode.AccessFlag; import javassist.bytecode.AttributeInfo; import javassist.bytecode.AnnotationsAttribute; @@ -1256,6 +1258,72 @@ class CtClassType extends CtClass { } int getUniqueNumber() { return uniqueNumberSeed++; } + + public String makeUniqueName(String prefix) { + HashMap table = new HashMap(); + makeMemberList(table); + Set keys = table.keySet(); + String[] methods = new String[keys.size()]; + keys.toArray(methods); + + if (notFindInArray(prefix, methods)) + return prefix; + + int i = 100; + String name; + do { + if (i > 999) + throw new RuntimeException("too many unique name"); + + name = prefix + i++; + } while (!notFindInArray(name, methods)); + return name; + } + + private static boolean notFindInArray(String prefix, String[] values) { + int len = values.length; + for (int i = 0; i < len; i++) + if (values[i].startsWith(prefix)) + return false; + + return true; + } + + private void makeMemberList(HashMap table) { + int mod = getModifiers(); + if (Modifier.isAbstract(mod) || Modifier.isInterface(mod)) + try { + CtClass[] ifs = getInterfaces(); + int size = ifs.length; + for (int i = 0; i < size; i++) { + CtClass ic =ifs[i]; + if (ic != null && ic instanceof CtClassType) + ((CtClassType)ic).makeMemberList(table); + } + } + catch (NotFoundException e) {} + + try { + CtClass s = getSuperclass(); + if (s != null && s instanceof CtClassType) + ((CtClassType)s).makeMemberList(table); + } + catch (NotFoundException e) {} + + List list = getClassFile2().getMethods(); + int n = list.size(); + for (int i = 0; i < n; i++) { + MethodInfo minfo = (MethodInfo)list.get(i); + table.put(minfo.getName(), this); + } + + list = getClassFile2().getFields(); + n = list.size(); + for (int i = 0; i < n; i++) { + FieldInfo finfo = (FieldInfo)list.get(i); + table.put(finfo.getName(), this); + } + } } class FieldInitLink { diff --git a/src/main/javassist/CtConstructor.java b/src/main/javassist/CtConstructor.java index 7bcf2622..fa139c79 100644 --- a/src/main/javassist/CtConstructor.java +++ b/src/main/javassist/CtConstructor.java @@ -153,6 +153,27 @@ public final class CtConstructor extends CtBehavior { return false; } + /** + * Returns true if this constructor calls a constructor + * of the super class. This method returns false if it + * calls another constructor of this class by this(). + */ + public boolean callsSuper() throws CannotCompileException { + CodeAttribute codeAttr = methodInfo.getCodeAttribute(); + if (codeAttr != null) { + CodeIterator it = codeAttr.iterator(); + try { + int index = it.skipSuperConstructor(); + return index >= 0; + } + catch (BadBytecode e) { + throw new CannotCompileException(e); + } + } + + return false; + } + /** * Sets a constructor body. * diff --git a/src/main/javassist/Modifier.java b/src/main/javassist/Modifier.java index 01ac1425..e41c7259 100644 --- a/src/main/javassist/Modifier.java +++ b/src/main/javassist/Modifier.java @@ -175,6 +175,12 @@ public class Modifier { return mod & ~clearBit; } + /** + * Return a string describing the access modifier flags in + * the specified modifier. + * + * @param mod modifier flags. + */ public static String toString(int mod) { return java.lang.reflect.Modifier.toString(mod); } -- cgit v1.2.3