aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>2005-10-31 16:48:44 +0000
committerchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>2005-10-31 16:48:44 +0000
commitbfdfdff690cdf56edb25ef28740da13dc2c86785 (patch)
tree835c3b9dae546c7e74e820670ba76569d62d165b /src
parent361e1587797db769c1688f40e9f1cd4a9156701c (diff)
downloadjavassist-bfdfdff690cdf56edb25ef28740da13dc2c86785.tar.gz
javassist-bfdfdff690cdf56edb25ef28740da13dc2c86785.zip
added makeUniqueName() in CtClass.
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@215 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
Diffstat (limited to 'src')
-rw-r--r--src/main/javassist/CtClass.java13
-rw-r--r--src/main/javassist/CtClassType.java68
-rw-r--r--src/main/javassist/CtConstructor.java21
-rw-r--r--src/main/javassist/Modifier.java6
4 files changed, 108 insertions, 0 deletions
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
@@ -154,6 +154,27 @@ public final class CtConstructor extends CtBehavior {
}
/**
+ * 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 <code>this()</code>.
+ */
+ 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.
*
* @param src the source code representing the 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);
}