Latest bnd code
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1350613 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/bundleplugin/src/main/java/aQute/bnd/compatibility/GenericParameter.java b/bundleplugin/src/main/java/aQute/bnd/compatibility/GenericParameter.java
index e187f3c..5118bf3 100644
--- a/bundleplugin/src/main/java/aQute/bnd/compatibility/GenericParameter.java
+++ b/bundleplugin/src/main/java/aQute/bnd/compatibility/GenericParameter.java
@@ -1,22 +1,24 @@
package aQute.bnd.compatibility;
public class GenericParameter {
- String name;
- GenericType bounds[];
-
+ String name;
+ GenericType bounds[];
+
public GenericParameter(String name, GenericType[] bounds) {
this.name = name;
this.bounds = bounds;
if (bounds == null || bounds.length == 0)
- this.bounds = new GenericType[] { new GenericType( Object.class) };
+ this.bounds = new GenericType[] {
+ new GenericType(Object.class)
+ };
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(name);
- if ( bounds != null && bounds.length > 0) {
- for ( GenericType gtype : bounds ) {
- sb.append( ":");
+ if (bounds != null && bounds.length > 0) {
+ for (GenericType gtype : bounds) {
+ sb.append(":");
sb.append(gtype);
}
}
diff --git a/bundleplugin/src/main/java/aQute/bnd/compatibility/GenericType.java b/bundleplugin/src/main/java/aQute/bnd/compatibility/GenericType.java
index 847a358..d16affe 100644
--- a/bundleplugin/src/main/java/aQute/bnd/compatibility/GenericType.java
+++ b/bundleplugin/src/main/java/aQute/bnd/compatibility/GenericType.java
@@ -1,6 +1,5 @@
package aQute.bnd.compatibility;
-
public class GenericType {
public GenericType(Class<Object> class1) {
// TODO Auto-generated constructor stub
@@ -11,27 +10,25 @@
GenericType[] a;
GenericType[] b;
int array;
-
- Scope scope;
-
- static public class GenericWildcard extends GenericType{
+
+ Scope scope;
+
+ static public class GenericWildcard extends GenericType {
public GenericWildcard(Class<Object> class1) {
super(class1);
// TODO Auto-generated constructor stub
}
-
+
}
-
+
static public class GenericArray extends GenericType {
public GenericArray(Class<Object> class1) {
super(class1);
// TODO Auto-generated constructor stub
}
-
+
}
-
-
}
diff --git a/bundleplugin/src/main/java/aQute/bnd/compatibility/Kind.java b/bundleplugin/src/main/java/aQute/bnd/compatibility/Kind.java
index 1e84030..42626f1 100644
--- a/bundleplugin/src/main/java/aQute/bnd/compatibility/Kind.java
+++ b/bundleplugin/src/main/java/aQute/bnd/compatibility/Kind.java
@@ -2,7 +2,6 @@
/**
* The kind of thing we scope
- *
*/
public enum Kind {
ROOT, CLASS, FIELD, CONSTRUCTOR, METHOD, UNKNOWN;
diff --git a/bundleplugin/src/main/java/aQute/bnd/compatibility/ParseSignatureBuilder.java b/bundleplugin/src/main/java/aQute/bnd/compatibility/ParseSignatureBuilder.java
index f1f91d1..133abb1 100644
--- a/bundleplugin/src/main/java/aQute/bnd/compatibility/ParseSignatureBuilder.java
+++ b/bundleplugin/src/main/java/aQute/bnd/compatibility/ParseSignatureBuilder.java
@@ -6,30 +6,32 @@
import aQute.lib.osgi.Descriptors.TypeRef;
public class ParseSignatureBuilder {
- final Scope root;
-
+ final Scope root;
+
public ParseSignatureBuilder(Scope root) {
this.root = root;
}
-
- public void add( Jar jar ) throws Exception {
- for ( Resource r : jar.getResources().values()) {
+
+ public void add(Jar jar) throws Exception {
+ for (Resource r : jar.getResources().values()) {
InputStream in = r.openInputStream();
try {
parse(in);
- } finally {
+ }
+ finally {
in.close();
}
}
}
-
- public Scope getRoot() { return root; }
-
-
+
+ public Scope getRoot() {
+ return root;
+ }
+
public void parse(InputStream in) throws Exception {
Analyzer analyzer = new Analyzer();
Clazz clazz = new Clazz(analyzer, "", null);
-
+
clazz.parseClassFile(in, new ClassDataCollector() {
Scope s;
Scope enclosing;
@@ -44,7 +46,7 @@
@Override
public void extendsClass(TypeRef name) {
-// s.setBase(new GenericType(name));
+ // s.setBase(new GenericType(name));
}
@Override
@@ -55,7 +57,7 @@
GenericType[] convert(TypeRef names[]) {
GenericType tss[] = new GenericType[names.length];
for (int i = 0; i < names.length; i++) {
-// tss[i] = new GenericType(names[i]);
+ // tss[i] = new GenericType(names[i]);
}
return tss;
}
@@ -92,9 +94,9 @@
@Override
public void classEnd() {
if (enclosing != null)
- s.setEnclosing( enclosing );
+ s.setEnclosing(enclosing);
if (declaring != null)
- s.setDeclaring( declaring );
+ s.setDeclaring(declaring);
}
@Override
@@ -106,14 +108,11 @@
}
@Override
- public void innerClass(TypeRef innerClass, TypeRef outerClass, String innerName,
- int innerClassAccessFlags) {
+ public void innerClass(TypeRef innerClass, TypeRef outerClass, String innerName, int innerClassAccessFlags) {
if (outerClass != null && innerClass != null && innerClass.getBinary().equals(s.name))
declaring = root.getScope(outerClass.getBinary());
}
});
-
-
+
}
}
-
diff --git a/bundleplugin/src/main/java/aQute/bnd/compatibility/RuntimeSignatureBuilder.java b/bundleplugin/src/main/java/aQute/bnd/compatibility/RuntimeSignatureBuilder.java
index 32ae94f..9e6b090 100644
--- a/bundleplugin/src/main/java/aQute/bnd/compatibility/RuntimeSignatureBuilder.java
+++ b/bundleplugin/src/main/java/aQute/bnd/compatibility/RuntimeSignatureBuilder.java
@@ -9,16 +9,15 @@
this.root = root;
}
- static public String identity(Class<?> c) {
+ static public String identity(Class< ? > c) {
return Scope.classIdentity(c.getName());
}
static public String identity(Method m) {
- return Scope.methodIdentity(m.getName(), getDescriptor(m.getReturnType(), m
- .getParameterTypes()));
+ return Scope.methodIdentity(m.getName(), getDescriptor(m.getReturnType(), m.getParameterTypes()));
}
- static public String identity(Constructor<?> m) {
+ static public String identity(Constructor< ? > m) {
return Scope.constructorIdentity(getDescriptor(void.class, m.getParameterTypes()));
}
@@ -26,11 +25,11 @@
return Scope.fieldIdentity(m.getName(), getDescriptor(m.getType(), null));
}
- static public String getDescriptor(Class<?> base, Class<?>[] parameters) {
+ static public String getDescriptor(Class< ? > base, Class< ? >[] parameters) {
StringBuilder sb = new StringBuilder();
if (parameters != null) {
sb.append("(");
- for (Class<?> parameter : parameters) {
+ for (Class< ? > parameter : parameters) {
sb.append(getDescriptor(parameter));
}
sb.append(")");
@@ -39,9 +38,9 @@
return sb.toString();
}
- public Scope add(Class<?> c) {
- Scope local = add(root, getEnclosingScope(c), c.getModifiers(), c.getTypeParameters(),
- Kind.CLASS, identity(c), c.getGenericSuperclass(), c.getGenericInterfaces(), null);
+ public Scope add(Class< ? > c) {
+ Scope local = add(root, getEnclosingScope(c), c.getModifiers(), c.getTypeParameters(), Kind.CLASS, identity(c),
+ c.getGenericSuperclass(), c.getGenericInterfaces(), null);
for (Field f : c.getDeclaredFields()) {
add(local, // declaring scope
@@ -56,7 +55,7 @@
);
}
- for (Constructor<?> constr : c.getConstructors()) {
+ for (Constructor< ? > constr : c.getConstructors()) {
add(local, // class scope
local, // enclosing
constr.getModifiers(), // access modifiers
@@ -86,20 +85,20 @@
return local;
}
- private Scope getEnclosingScope(Class<?> c) {
+ private Scope getEnclosingScope(Class< ? > c) {
Method m = c.getEnclosingMethod();
if (m != null) {
Scope s = getGlobalScope(m.getDeclaringClass());
return s.getScope(identity(m));
}
-// TODO
-// Constructor cnstr = c.getEnclosingConstructor();
-// if (m != null) {
-// Scope s = getGlobalScope(cnstr.getDeclaringClass());
-// return s.getScope(identity(cnstr));
-//
-// }
- Class<?> enclosingClass = c.getEnclosingClass();
+ // TODO
+ // Constructor cnstr = c.getEnclosingConstructor();
+ // if (m != null) {
+ // Scope s = getGlobalScope(cnstr.getDeclaringClass());
+ // return s.getScope(identity(cnstr));
+ //
+ // }
+ Class< ? > enclosingClass = c.getEnclosingClass();
if (enclosingClass != null) {
return getGlobalScope(enclosingClass);
}
@@ -107,23 +106,22 @@
return null;
}
- private Scope getGlobalScope(Class<?> c) {
+ private Scope getGlobalScope(Class< ? > c) {
if (c == null)
return null;
String id = identity(c);
return root.getScope(id);
}
- private Scope add(Scope declaring, Scope enclosing, int modifiers,
- TypeVariable<?>[] typeVariables, Kind kind, String id, Type mainType,
- Type[] parameterTypes, Type exceptionTypes[]) {
+ private Scope add(Scope declaring, Scope enclosing, int modifiers, TypeVariable< ? >[] typeVariables, Kind kind,
+ String id, Type mainType, Type[] parameterTypes, Type exceptionTypes[]) {
Scope scope = declaring.getScope(id);
assert scope.access == Access.UNKNOWN;
scope.setAccess(Access.modifier(modifiers));
scope.setKind(kind);
scope.setGenericParameter(convert(typeVariables));
- scope.setBase(convert(scope,mainType));
+ scope.setBase(convert(scope, mainType));
scope.setParameterTypes(convert(parameterTypes));
scope.setExceptionTypes(convert(exceptionTypes));
scope.setDeclaring(declaring);
@@ -135,32 +133,32 @@
if (t instanceof ParameterizedType) {
// C<P..>
ParameterizedType pt = (ParameterizedType) t;
- /*Scope reference =*/ root.getScope(identity((Class<?>)pt.getRawType()));
+ /* Scope reference = */root.getScope(identity((Class< ? >) pt.getRawType()));
Type args[] = pt.getActualTypeArguments();
GenericType[] arguments = new GenericType[args.length];
int n = 0;
for (Type arg : args)
- arguments[n++] = convert(source,arg);
-// return new GenericType(reference,null,arguments);
-
+ arguments[n++] = convert(source, arg);
+ // return new GenericType(reference,null,arguments);
+
} else if (t instanceof TypeVariable) {
-// TypeVariable tv = (TypeVariable) t;
-// return new GenericType(source,tv.getName(), null);
+ // TypeVariable tv = (TypeVariable) t;
+ // return new GenericType(source,tv.getName(), null);
} else if (t instanceof WildcardType) {
-// WildcardType wc = (WildcardType) t;
-// wc.
+ // WildcardType wc = (WildcardType) t;
+ // wc.
} else if (t instanceof GenericArrayType) {
}
- if (t instanceof Class<?>) {
-// raw = ((Class<?>) t).getName() + ";";
+ if (t instanceof Class< ? >) {
+ // raw = ((Class<?>) t).getName() + ";";
} else
throw new IllegalArgumentException(t.toString());
return null;
}
- private GenericParameter[] convert(TypeVariable<?> vars[]) {
+ private GenericParameter[] convert(TypeVariable< ? > vars[]) {
if (vars == null)
return null;
@@ -178,12 +176,12 @@
GenericType tss[] = new GenericType[parameterTypes.length];
for (int i = 0; i < parameterTypes.length; i++) {
- //tss[i] = new GenericType(parameterTypes[i]);
+ // tss[i] = new GenericType(parameterTypes[i]);
}
return tss;
}
- private static String getDescriptor(Class<?> c) {
+ private static String getDescriptor(Class< ? > c) {
StringBuilder sb = new StringBuilder();
if (c.isPrimitive()) {
if (c == boolean.class)
diff --git a/bundleplugin/src/main/java/aQute/bnd/compatibility/Scope.java b/bundleplugin/src/main/java/aQute/bnd/compatibility/Scope.java
index 7f22f35..43dccbd 100644
--- a/bundleplugin/src/main/java/aQute/bnd/compatibility/Scope.java
+++ b/bundleplugin/src/main/java/aQute/bnd/compatibility/Scope.java
@@ -4,20 +4,20 @@
import java.util.*;
public class Scope {
- final Map<String, Scope> children = new LinkedHashMap<String, Scope>();
+ final Map<String,Scope> children = new LinkedHashMap<String,Scope>();
// class: slashed name
// field: name ":" typed
// constructor: ":(" typed* ")" typed
// method: name ":(" typed* ")" typed
- final String name;
+ final String name;
- Access access;
- Kind kind;
- Scope enclosing;
- Scope declaring;
- GenericParameter typeVars[];
- Map<String, String[]> name2bounds;
+ Access access;
+ Kind kind;
+ Scope enclosing;
+ Scope declaring;
+ GenericParameter typeVars[];
+ Map<String,String[]> name2bounds;
// class: super
// field: type
@@ -28,11 +28,11 @@
// class: interfaces
// constructor: args
// method: args
- GenericType[] parameters;
+ GenericType[] parameters;
// constructor: exceptions
// method: exceptions
- GenericType[] exceptions;
+ GenericType[] exceptions;
// class: super interfaces*
// field: type
@@ -70,10 +70,10 @@
public String toString() {
StringBuilder sb = new StringBuilder();
-
- if ( typeVars != null && typeVars.length !=0) {
+
+ if (typeVars != null && typeVars.length != 0) {
sb.append("<");
- for ( GenericParameter v : typeVars) {
+ for (GenericParameter v : typeVars) {
sb.append(v);
}
sb.append(">");
@@ -81,8 +81,8 @@
sb.append(access.toString());
sb.append(" ");
sb.append(kind.toString());
- sb.append( " ");
- sb.append( name );
+ sb.append(" ");
+ sb.append(name);
return sb.toString();
}
@@ -124,7 +124,7 @@
}
static public String classIdentity(String name2) {
- return name2.replace('.','/');
+ return name2.replace('.', '/');
}
static public String methodIdentity(String name, String descriptor) {
@@ -140,18 +140,18 @@
}
public void cleanRoot() {
- Iterator<Map.Entry<String, Scope>> i = children.entrySet().iterator();
+ Iterator<Map.Entry<String,Scope>> i = children.entrySet().iterator();
while (i.hasNext()) {
- Map.Entry<String, Scope> entry = i.next();
+ Map.Entry<String,Scope> entry = i.next();
if (!entry.getValue().isTop())
i.remove();
}
}
public void prune(EnumSet<Access> level) {
- Iterator<Map.Entry<String, Scope>> i = children.entrySet().iterator();
+ Iterator<Map.Entry<String,Scope>> i = children.entrySet().iterator();
while (i.hasNext()) {
- Map.Entry<String, Scope> entry = i.next();
+ Map.Entry<String,Scope> entry = i.next();
if (!level.contains(entry.getValue().access))
i.remove();
else
diff --git a/bundleplugin/src/main/java/aQute/bnd/compatibility/SignatureGenerator.java b/bundleplugin/src/main/java/aQute/bnd/compatibility/SignatureGenerator.java
index 9b31f7f..6567ef8 100644
--- a/bundleplugin/src/main/java/aQute/bnd/compatibility/SignatureGenerator.java
+++ b/bundleplugin/src/main/java/aQute/bnd/compatibility/SignatureGenerator.java
@@ -1,125 +1,126 @@
package aQute.bnd.compatibility;
-
public class SignatureGenerator {
-// enum ACCESS {
-//// PUBLIC("+"), PROTECTED("|"), PACKAGE_PRIVATE(""), PRIVATE("-");
-//// final String repr;
-////
-//// ACCESS(String s) {
-//// repr = s;
-//// }
-////
-//// public String toString() {
-//// return repr;
-//// }
-// }
-//
-// public static void main(String args[]) throws Exception {
-// final PrintStream out = System.err;
-//
-// Clazz c = new Clazz("x", new FileResource(new File(
-// "src/aQute/bnd/compatibility/SignatureGenerator.class")));
-// c.parseClassFileWithCollector(new ClassDataCollector() {
-// public void classBegin(int access, String name) {
-// out.print(name);
-// out.println(access(access));
-// }
-//
-// private ACCESS access(int access) {
-// if (Modifier.isPublic(access))
-// return ACCESS.PUBLIC;
-//
-// throw new IllegalArgumentException();
-// }
-//
-// public void extendsClass(String name) {
-// }
-//
-// public void implementsInterfaces(String name[]) {
-// }
-//
-// public void addReference(String token) {
-// }
-//
-// public void annotation(Annotation annotation) {
-// }
-//
-// public void parameter(int p) {
-// }
-//
-// public void method(Clazz.MethodDef defined) {
-// if (defined.isConstructor())
-// constructor(defined.access, defined.descriptor);
-// else
-// method(defined.access, defined.name, defined.descriptor);
-// }
-//
-// public void field(Clazz.FieldDef defined) {
-// field(defined.access, defined.name, defined.descriptor);
-// }
-//
-// public void reference(Clazz.MethodDef referenced) {
-// }
-//
-// public void reference(Clazz.FieldDef referenced) {
-// }
-//
-// public void classEnd() {
-// }
-//
-// @Deprecated// Will really be removed!
-// public void field(int access, String name, String descriptor) {
-// }
-//
-// @Deprecated// Will really be removed!
-// public void constructor(int access, String descriptor) {
-// }
-//
-// @Deprecated// Will really be removed!
-// public void method(int access, String name, String descriptor) {
-// }
-//
-// /**
-// * The EnclosingMethod attribute
-// *
-// * @param cName
-// * The name of the enclosing class, never null. Name is
-// * with slashes.
-// * @param mName
-// * The name of the enclosing method in the class with
-// * cName or null
-// * @param mDescriptor
-// * The descriptor of this type
-// */
-// public void enclosingMethod(String cName, String mName, String mDescriptor) {
-//
-// }
-//
-// /**
-// * The InnerClass attribute
-// *
-// * @param innerClass
-// * The name of the inner class (with slashes). Can be
-// * null.
-// * @param outerClass
-// * The name of the outer class (with slashes) Can be
-// * null.
-// * @param innerName
-// * The name inside the outer class, can be null.
-// * @param modifiers
-// * The access flags
-// */
-// public void innerClass(String innerClass, String outerClass, String innerName,
-// int innerClassAccessFlags) {
-// }
-//
-// public void signature(String signature) {
-// }
-//
-// public void constant(Object object) {
-// }
-//
-// });
-// }
+ // enum ACCESS {
+ // // PUBLIC("+"), PROTECTED("|"), PACKAGE_PRIVATE(""), PRIVATE("-");
+ // // final String repr;
+ // //
+ // // ACCESS(String s) {
+ // // repr = s;
+ // // }
+ // //
+ // // public String toString() {
+ // // return repr;
+ // // }
+ // }
+ //
+ // public static void main(String args[]) throws Exception {
+ // final PrintStream out = System.err;
+ //
+ // Clazz c = new Clazz("x", new FileResource(new File(
+ // "src/aQute/bnd/compatibility/SignatureGenerator.class")));
+ // c.parseClassFileWithCollector(new ClassDataCollector() {
+ // public void classBegin(int access, String name) {
+ // out.print(name);
+ // out.println(access(access));
+ // }
+ //
+ // private ACCESS access(int access) {
+ // if (Modifier.isPublic(access))
+ // return ACCESS.PUBLIC;
+ //
+ // throw new IllegalArgumentException();
+ // }
+ //
+ // public void extendsClass(String name) {
+ // }
+ //
+ // public void implementsInterfaces(String name[]) {
+ // }
+ //
+ // public void addReference(String token) {
+ // }
+ //
+ // public void annotation(Annotation annotation) {
+ // }
+ //
+ // public void parameter(int p) {
+ // }
+ //
+ // public void method(Clazz.MethodDef defined) {
+ // if (defined.isConstructor())
+ // constructor(defined.access, defined.descriptor);
+ // else
+ // method(defined.access, defined.name, defined.descriptor);
+ // }
+ //
+ // public void field(Clazz.FieldDef defined) {
+ // field(defined.access, defined.name, defined.descriptor);
+ // }
+ //
+ // public void reference(Clazz.MethodDef referenced) {
+ // }
+ //
+ // public void reference(Clazz.FieldDef referenced) {
+ // }
+ //
+ // public void classEnd() {
+ // }
+ //
+ // @Deprecated// Will really be removed!
+ // public void field(int access, String name, String descriptor) {
+ // }
+ //
+ // @Deprecated// Will really be removed!
+ // public void constructor(int access, String descriptor) {
+ // }
+ //
+ // @Deprecated// Will really be removed!
+ // public void method(int access, String name, String descriptor) {
+ // }
+ //
+ // /**
+ // * The EnclosingMethod attribute
+ // *
+ // * @param cName
+ // * The name of the enclosing class, never null. Name is
+ // * with slashes.
+ // * @param mName
+ // * The name of the enclosing method in the class with
+ // * cName or null
+ // * @param mDescriptor
+ // * The descriptor of this type
+ // */
+ // public void enclosingMethod(String cName, String mName, String
+ // mDescriptor) {
+ //
+ // }
+ //
+ // /**
+ // * The InnerClass attribute
+ // *
+ // * @param innerClass
+ // * The name of the inner class (with slashes). Can be
+ // * null.
+ // * @param outerClass
+ // * The name of the outer class (with slashes) Can be
+ // * null.
+ // * @param innerName
+ // * The name inside the outer class, can be null.
+ // * @param modifiers
+ // * The access flags
+ // */
+ // public void innerClass(String innerClass, String outerClass, String
+ // innerName,
+ // int innerClassAccessFlags) {
+ // }
+ //
+ // public void signature(String signature) {
+ // }
+ //
+ // public void constant(Object object) {
+ // }
+ //
+ // });
+ // }
}
diff --git a/bundleplugin/src/main/java/aQute/bnd/compatibility/Signatures.java b/bundleplugin/src/main/java/aQute/bnd/compatibility/Signatures.java
index 2b9ce53..18b89a9 100644
--- a/bundleplugin/src/main/java/aQute/bnd/compatibility/Signatures.java
+++ b/bundleplugin/src/main/java/aQute/bnd/compatibility/Signatures.java
@@ -29,26 +29,23 @@
* @version $Id$
*/
public class Signatures {
-
-
+
/**
- * Check if the environment has generics, i.e. later than
- * Java 5 VM.
+ * Check if the environment has generics, i.e. later than Java 5 VM.
*
* @return true if generics are supported
* @throws Exception
*/
public boolean hasGenerics() throws Exception {
try {
- call( Signatures.class, "getGenericSuperClass");
+ call(Signatures.class, "getGenericSuperClass");
return true;
- } catch( NoSuchMethodException mnfe ) {
+ }
+ catch (NoSuchMethodException mnfe) {
return false;
}
}
-
-
-
+
/**
* Helper class to track an index in a string.
*/
@@ -72,8 +69,7 @@
char take(char c) {
char x = s.charAt(i++);
if (c != x)
- throw new IllegalStateException("get() expected " + c
- + " but got + " + x);
+ throw new IllegalStateException("get() expected " + c + " but got + " + x);
return x;
}
@@ -92,20 +88,21 @@
/**
* Calculate the generic signature of a Class,Method,Field, or Constructor.
+ *
* @param f
* @return
- * @throws Exception
+ * @throws Exception
*/
public String getSignature(Object c) throws Exception {
- if( c instanceof Class<?>)
- return getSignature((Class<?>)c);
- if( c instanceof Constructor<?>)
- return getSignature((Constructor<?>)c);
- if( c instanceof Method)
- return getSignature((Method)c);
- if( c instanceof Field)
- return getSignature((Field)c);
-
+ if (c instanceof Class< ? >)
+ return getSignature((Class< ? >) c);
+ if (c instanceof Constructor< ? >)
+ return getSignature((Constructor< ? >) c);
+ if (c instanceof Method)
+ return getSignature((Method) c);
+ if (c instanceof Field)
+ return getSignature((Field) c);
+
throw new IllegalArgumentException(c.toString());
}
@@ -116,16 +113,15 @@
* class ::= declaration? reference reference*
* </pre>
*
- *
* @param f
* @return
- * @throws Exception
+ * @throws Exception
*/
public String getSignature(Class< ? > c) throws Exception {
StringBuilder sb = new StringBuilder();
declaration(sb, c);
reference(sb, call(c, "getGenericSuperclass"));
- for (Object type : (Object[]) call(c,"getGenericInterfaces")) {
+ for (Object type : (Object[]) call(c, "getGenericInterfaces")) {
reference(sb, type);
}
return sb.toString();
@@ -140,17 +136,17 @@
*
* @param c
* @return
- * @throws Exception
+ * @throws Exception
*/
public String getSignature(Method m) throws Exception {
StringBuilder sb = new StringBuilder();
declaration(sb, m);
sb.append('(');
- for (Object type : (Object[]) call(m,"getGenericParameterTypes")) {
+ for (Object type : (Object[]) call(m, "getGenericParameterTypes")) {
reference(sb, type);
}
sb.append(')');
- reference(sb, call(m,"getGenericReturnType"));
+ reference(sb, call(m, "getGenericReturnType"));
return sb.toString();
}
@@ -164,13 +160,13 @@
*
* @param c
* @return
- * @throws Exception
+ * @throws Exception
*/
public String getSignature(Constructor< ? > c) throws Exception {
StringBuilder sb = new StringBuilder();
declaration(sb, c);
sb.append('(');
- for (Object type : (Object[]) call(c,"getGenericParameterTypes")) {
+ for (Object type : (Object[]) call(c, "getGenericParameterTypes")) {
reference(sb, type);
}
sb.append(')');
@@ -187,11 +183,11 @@
*
* @param c
* @return
- * @throws Exception
+ * @throws Exception
*/
public String getSignature(Field f) throws Exception {
StringBuilder sb = new StringBuilder();
- Object t = call(f,"getGenericType");
+ Object t = call(f, "getGenericType");
reference(sb, t);
return sb.toString();
}
@@ -215,13 +211,13 @@
* @throws Exception
*/
private void declaration(StringBuilder sb, Object gd) throws Exception {
- Object[] typeParameters = (Object[]) call(gd,"getTypeParameters");
+ Object[] typeParameters = (Object[]) call(gd, "getTypeParameters");
if (typeParameters.length > 0) {
sb.append('<');
for (Object tv : typeParameters) {
- sb.append( call(tv,"getName"));
+ sb.append(call(tv, "getName"));
- Object[] bounds = (Object[]) call(tv,"getBounds");
+ Object[] bounds = (Object[]) call(tv, "getBounds");
if (bounds.length > 0 && isInterface(bounds[0])) {
sb.append(':');
}
@@ -237,22 +233,22 @@
/**
* Verify that the type is an interface.
*
- * @param type the type to check.
+ * @param type
+ * the type to check.
* @return true if this is a class that is an interface or a Parameterized
* Type that is an interface
- * @throws Exception
+ * @throws Exception
*/
private boolean isInterface(Object type) throws Exception {
if (type instanceof Class)
return (((Class< ? >) type).isInterface());
- if ( isInstance(type.getClass(), "java.lang.reflect.ParameterizedType"))
- return isInterface(call(type,"getRawType"));
+ if (isInstance(type.getClass(), "java.lang.reflect.ParameterizedType"))
+ return isInterface(call(type, "getRawType"));
return false;
}
-
/**
* This is the heart of the signature builder. A reference is used
* in a lot of places. It referes to another type.
@@ -271,80 +267,65 @@
*/
private void reference(StringBuilder sb, Object t) throws Exception {
- if ( isInstance(t.getClass(),"java.lang.reflect.ParameterizedType")) {
+ if (isInstance(t.getClass(), "java.lang.reflect.ParameterizedType")) {
sb.append('L');
parameterizedType(sb, t);
sb.append(';');
return;
- }
- else
- if ( isInstance(t.getClass(), "java.lang.reflect.GenericArrayType")) {
- sb.append('[');
- reference(sb, call(t,"getGenericComponentType"));
- }
- else
- if ( isInstance(t.getClass(), "java.lang.reflect.WildcardType")) {
- Object[] lowerBounds = (Object[]) call(t, "getLowerBounds");
- Object[] upperBounds = (Object[]) call(t, "getUpperBounds");
+ } else if (isInstance(t.getClass(), "java.lang.reflect.GenericArrayType")) {
+ sb.append('[');
+ reference(sb, call(t, "getGenericComponentType"));
+ } else if (isInstance(t.getClass(), "java.lang.reflect.WildcardType")) {
+ Object[] lowerBounds = (Object[]) call(t, "getLowerBounds");
+ Object[] upperBounds = (Object[]) call(t, "getUpperBounds");
- if (upperBounds.length == 1
- && upperBounds[0] == Object.class)
- upperBounds = new Object[0];
+ if (upperBounds.length == 1 && upperBounds[0] == Object.class)
+ upperBounds = new Object[0];
- if (upperBounds.length != 0) {
- // extend
- for (Object upper : upperBounds) {
- sb.append('+');
- reference(sb, upper);
- }
- }
- else
- if (lowerBounds.length != 0) {
- // super, can only be one by the language
- for (Object lower : lowerBounds) {
- sb.append('-');
- reference(sb, lower);
- }
- }
- else
- sb.append('*');
+ if (upperBounds.length != 0) {
+ // extend
+ for (Object upper : upperBounds) {
+ sb.append('+');
+ reference(sb, upper);
}
- else
- if ( isInstance(t.getClass(),"java.lang.reflect.TypeVariable")) {
- sb.append('T');
- sb.append( call(t,"getName"));
- sb.append(';');
- }
- else
- if (t instanceof Class< ? >) {
- Class< ? > c = (Class< ? >) t;
- if (c.isPrimitive()) {
- sb.append(primitive(c));
- }
- else {
- sb.append('L');
- String name = c.getName().replace('.', '/');
- sb.append(name);
- sb.append(';');
- }
- }
+ } else if (lowerBounds.length != 0) {
+ // super, can only be one by the language
+ for (Object lower : lowerBounds) {
+ sb.append('-');
+ reference(sb, lower);
+ }
+ } else
+ sb.append('*');
+ } else if (isInstance(t.getClass(), "java.lang.reflect.TypeVariable")) {
+ sb.append('T');
+ sb.append(call(t, "getName"));
+ sb.append(';');
+ } else if (t instanceof Class< ? >) {
+ Class< ? > c = (Class< ? >) t;
+ if (c.isPrimitive()) {
+ sb.append(primitive(c));
+ } else {
+ sb.append('L');
+ String name = c.getName().replace('.', '/');
+ sb.append(name);
+ sb.append(';');
+ }
+ }
}
/**
- * Creates the signature for a Parameterized Type.
- *
- * A Parameterized Type has a raw class and a set of type variables.
+ * Creates the signature for a Parameterized Type. A Parameterized Type has
+ * a raw class and a set of type variables.
*
* @param sb
* @param pt
- * @throws Exception
+ * @throws Exception
*/
private void parameterizedType(StringBuilder sb, Object pt) throws Exception {
- Object owner = call(pt,"getOwnerType");
- String name = ((Class< ? >) call(pt,"getRawType")).getName()
- .replace('.', '/');
+ Object owner = call(pt, "getOwnerType");
+ String name = ((Class< ? >) call(pt, "getRawType")).getName().replace('.', '/');
if (owner != null) {
- if ( isInstance(owner.getClass(), "java.lang.reflect.ParameterizedType"))
+ if (isInstance(owner.getClass(), "java.lang.reflect.ParameterizedType"))
parameterizedType(sb, owner);
else
sb.append(((Class< ? >) owner).getName().replace('.', '/'));
@@ -355,7 +336,7 @@
sb.append(name);
sb.append('<');
- for (Object parameterType : (Object[]) call(pt,"getActualTypeArguments")) {
+ for (Object parameterType : (Object[]) call(pt, "getActualTypeArguments")) {
reference(sb, parameterType);
}
sb.append('>');
@@ -365,40 +346,31 @@
/**
* Handle primitives, these need to be translated to a single char.
*
- * @param type the primitive class
+ * @param type
+ * the primitive class
* @return the single char associated with the primitive
*/
private char primitive(Class< ? > type) {
if (type == byte.class)
return 'B';
+ else if (type == char.class)
+ return 'C';
+ else if (type == double.class)
+ return 'D';
+ else if (type == float.class)
+ return 'F';
+ else if (type == int.class)
+ return 'I';
+ else if (type == long.class)
+ return 'J';
+ else if (type == short.class)
+ return 'S';
+ else if (type == boolean.class)
+ return 'Z';
+ else if (type == void.class)
+ return 'V';
else
- if (type == char.class)
- return 'C';
- else
- if (type == double.class)
- return 'D';
- else
- if (type == float.class)
- return 'F';
- else
- if (type == int.class)
- return 'I';
- else
- if (type == long.class)
- return 'J';
- else
- if (type == short.class)
- return 'S';
- else
- if (type == boolean.class)
- return 'Z';
- else
- if (type == void.class)
- return 'V';
- else
- throw new IllegalArgumentException(
- "Unknown primitive type "
- + type);
+ throw new IllegalArgumentException("Unknown primitive type " + type);
}
/**
@@ -412,7 +384,7 @@
public String normalize(String signature) {
StringBuilder sb = new StringBuilder();
- Map<String, String> map = new HashMap<String, String>();
+ Map<String,String> map = new HashMap<String,String>();
Rover rover = new Rover(signature);
declare(sb, map, rover);
@@ -424,8 +396,7 @@
}
sb.append(rover.take(')'));
reference(sb, map, rover, true); // return type
- }
- else {
+ } else {
// field or class
reference(sb, map, rover, true); // field type or super class
while (!rover.isEOF()) {
@@ -436,58 +407,51 @@
}
/**
- * The heart of the routine. Handle a reference to a type. Can be
- * an array, a class, a type variable, or a primitive.
+ * The heart of the routine. Handle a reference to a type. Can be an array,
+ * a class, a type variable, or a primitive.
*
* @param sb
* @param map
* @param rover
* @param primitivesAllowed
*/
- private void reference(StringBuilder sb, Map<String, String> map,
- Rover rover, boolean primitivesAllowed) {
+ private void reference(StringBuilder sb, Map<String,String> map, Rover rover, boolean primitivesAllowed) {
char type = rover.take();
sb.append(type);
if (type == '[') {
reference(sb, map, rover, true);
- }
- else
- if (type == 'L') {
- String fqnb = rover.upTo("<;.");
- sb.append(fqnb);
+ } else if (type == 'L') {
+ String fqnb = rover.upTo("<;.");
+ sb.append(fqnb);
+ body(sb, map, rover);
+ while (rover.peek() == '.') {
+ sb.append(rover.take('.'));
+ sb.append(rover.upTo("<;."));
body(sb, map, rover);
- while (rover.peek() == '.') {
- sb.append(rover.take('.'));
- sb.append(rover.upTo("<;."));
- body(sb, map, rover);
- }
- sb.append(rover.take(';'));
}
- else
- if (type == 'T') {
- String name = rover.upTo(";");
- name = assign(map, name);
- sb.append(name);
- sb.append(rover.take(';'));
- }
- else {
- if (!primitivesAllowed)
- throw new IllegalStateException(
- "Primitives are not allowed without an array");
- }
+ sb.append(rover.take(';'));
+ } else if (type == 'T') {
+ String name = rover.upTo(";");
+ name = assign(map, name);
+ sb.append(name);
+ sb.append(rover.take(';'));
+ } else {
+ if (!primitivesAllowed)
+ throw new IllegalStateException("Primitives are not allowed without an array");
+ }
}
/**
- * Because classes can be nested the body handles the part that can
- * be nested, the reference handles the enclosing L ... ;
+ * Because classes can be nested the body handles the part that can be
+ * nested, the reference handles the enclosing L ... ;
*
* @param sb
* @param map
* @param rover
*/
- private void body(StringBuilder sb, Map<String, String> map, Rover rover) {
+ private void body(StringBuilder sb, Map<String,String> map, Rover rover) {
if (rover.peek() == '<') {
sb.append(rover.take('<'));
while (rover.peek() != '>') {
@@ -528,7 +492,7 @@
* @param map
* @param rover
*/
- private void declare(StringBuilder sb, Map<String, String> map, Rover rover) {
+ private void declare(StringBuilder sb, Map<String,String> map, Rover rover) {
char c = rover.peek();
if (c == '<') {
sb.append(rover.take('<'));
@@ -554,14 +518,16 @@
}
/**
- * Handles the assignment of type variables to index names so that
- * we have a normalized name for each type var.
+ * Handles the assignment of type variables to index names so that we have a
+ * normalized name for each type var.
*
- * @param map the map with variables.
- * @param name The name of the variable
+ * @param map
+ * the map with variables.
+ * @param name
+ * The name of the variable
* @return the index name, like _1
*/
- private String assign(Map<String, String> map, String name) {
+ private String assign(Map<String,String> map, String name) {
if (map.containsKey(name))
return map.get(name);
else {
@@ -571,23 +537,23 @@
}
}
- private boolean isInstance(Class<?> type, String string) {
- if ( type == null)
+ private boolean isInstance(Class< ? > type, String string) {
+ if (type == null)
return false;
-
- if ( type.getName().equals(string))
+
+ if (type.getName().equals(string))
return true;
-
- if ( isInstance( type.getSuperclass(), string))
+
+ if (isInstance(type.getSuperclass(), string))
return true;
-
- for ( Class<?> intf : type.getInterfaces()) {
- if ( isInstance(intf,string))
+
+ for (Class< ? > intf : type.getInterfaces()) {
+ if (isInstance(intf, string))
return true;
}
return false;
}
-
+
private Object call(Object gd, String string) throws Exception {
Method m = gd.getClass().getMethod(string);
return m.invoke(gd);