Make Definition's public API completely static.

TODO: Remove Definition arguments everywhere and hide INSTANCE field!
This commit is contained in:
Uwe Schindler 2016-05-20 11:22:03 +02:00
parent f5fc60ac67
commit 91f4bba042
10 changed files with 91 additions and 70 deletions

View File

@ -41,10 +41,10 @@ public final class AnalyzerCaster {
return null; return null;
} }
Cast transform = definition.transformsMap.get(cast); Cast transform = Definition.getTransform(cast);
if (transform == null && explicit) { if (transform == null && explicit) {
transform = definition.transformsMap.get(new Cast(actual, expected, false)); transform = Definition.getTransform(new Cast(actual, expected, false));
} }
if (transform != null) { if (transform != null) {

View File

@ -145,7 +145,7 @@ public final class Def {
Definition.MethodKey key = new Definition.MethodKey(name, type.parameterCount()); Definition.MethodKey key = new Definition.MethodKey(name, type.parameterCount());
// check whitelist for matching method // check whitelist for matching method
for (Class<?> clazz = receiverClass; clazz != null; clazz = clazz.getSuperclass()) { for (Class<?> clazz = receiverClass; clazz != null; clazz = clazz.getSuperclass()) {
RuntimeClass struct = definition.runtimeMap.get(clazz); RuntimeClass struct = Definition.getRuntimeClass(clazz);
if (struct != null) { if (struct != null) {
Method method = struct.methods.get(key); Method method = struct.methods.get(key);
@ -155,7 +155,7 @@ public final class Def {
} }
for (final Class<?> iface : clazz.getInterfaces()) { for (final Class<?> iface : clazz.getInterfaces()) {
struct = definition.runtimeMap.get(iface); struct = Definition.getRuntimeClass(iface);
if (struct != null) { if (struct != null) {
Method method = struct.methods.get(key); Method method = struct.methods.get(key);
@ -200,7 +200,7 @@ public final class Def {
static MethodHandle lookupGetter(Class<?> receiverClass, String name, Definition definition) { static MethodHandle lookupGetter(Class<?> receiverClass, String name, Definition definition) {
// first try whitelist // first try whitelist
for (Class<?> clazz = receiverClass; clazz != null; clazz = clazz.getSuperclass()) { for (Class<?> clazz = receiverClass; clazz != null; clazz = clazz.getSuperclass()) {
RuntimeClass struct = definition.runtimeMap.get(clazz); RuntimeClass struct = Definition.getRuntimeClass(clazz);
if (struct != null) { if (struct != null) {
MethodHandle handle = struct.getters.get(name); MethodHandle handle = struct.getters.get(name);
@ -210,7 +210,7 @@ public final class Def {
} }
for (final Class<?> iface : clazz.getInterfaces()) { for (final Class<?> iface : clazz.getInterfaces()) {
struct = definition.runtimeMap.get(iface); struct = Definition.getRuntimeClass(iface);
if (struct != null) { if (struct != null) {
MethodHandle handle = struct.getters.get(name); MethodHandle handle = struct.getters.get(name);
@ -271,7 +271,7 @@ public final class Def {
static MethodHandle lookupSetter(Class<?> receiverClass, String name, Definition definition) { static MethodHandle lookupSetter(Class<?> receiverClass, String name, Definition definition) {
// first try whitelist // first try whitelist
for (Class<?> clazz = receiverClass; clazz != null; clazz = clazz.getSuperclass()) { for (Class<?> clazz = receiverClass; clazz != null; clazz = clazz.getSuperclass()) {
RuntimeClass struct = definition.runtimeMap.get(clazz); RuntimeClass struct = Definition.getRuntimeClass(clazz);
if (struct != null) { if (struct != null) {
MethodHandle handle = struct.setters.get(name); MethodHandle handle = struct.setters.get(name);
@ -281,7 +281,7 @@ public final class Def {
} }
for (final Class<?> iface : clazz.getInterfaces()) { for (final Class<?> iface : clazz.getInterfaces()) {
struct = definition.runtimeMap.get(iface); struct = Definition.getRuntimeClass(iface);
if (struct != null) { if (struct != null) {
MethodHandle handle = struct.setters.get(name); MethodHandle handle = struct.setters.get(name);

View File

@ -47,29 +47,29 @@ public final class Definition {
public static final Definition INSTANCE = new Definition(new Definition()); public static final Definition INSTANCE = new Definition(new Definition());
/** Some native types as constants: */ /** Some native types as constants: */
public static final Type voidType = INSTANCE.getType("void"); public static final Type voidType = getType("void");
public static final Type booleanType = INSTANCE.getType("boolean"); public static final Type booleanType = getType("boolean");
public static final Type booleanobjType = INSTANCE.getType("Boolean"); public static final Type booleanobjType = getType("Boolean");
public static final Type byteType = INSTANCE.getType("byte"); public static final Type byteType = getType("byte");
public static final Type byteobjType = INSTANCE.getType("Byte"); public static final Type byteobjType = getType("Byte");
public static final Type shortType = INSTANCE.getType("short"); public static final Type shortType = getType("short");
public static final Type shortobjType = INSTANCE.getType("Short"); public static final Type shortobjType = getType("Short");
public static final Type intType = INSTANCE.getType("int"); public static final Type intType = getType("int");
public static final Type intobjType = INSTANCE.getType("Integer"); public static final Type intobjType = getType("Integer");
public static final Type longType = INSTANCE.getType("long"); public static final Type longType = getType("long");
public static final Type longobjType = INSTANCE.getType("Long"); public static final Type longobjType = getType("Long");
public static final Type floatType = INSTANCE.getType("float"); public static final Type floatType = getType("float");
public static final Type floatobjType = INSTANCE.getType("Float"); public static final Type floatobjType = getType("Float");
public static final Type doubleType = INSTANCE.getType("double"); public static final Type doubleType = getType("double");
public static final Type doubleobjType = INSTANCE.getType("Double"); public static final Type doubleobjType = getType("Double");
public static final Type charType = INSTANCE.getType("char"); public static final Type charType = getType("char");
public static final Type charobjType = INSTANCE.getType("Character"); public static final Type charobjType = getType("Character");
public static final Type objectType = INSTANCE.getType("Object"); public static final Type objectType = getType("Object");
public static final Type defType = INSTANCE.getType("def"); public static final Type defType = getType("def");
public static final Type defobjType = INSTANCE.getType("Def"); public static final Type defobjType = getType("Def");
public static final Type stringType = INSTANCE.getType("String"); public static final Type stringType = getType("String");
public static final Type exceptionType = INSTANCE.getType("Exception"); public static final Type exceptionType = getType("Exception");
public static final Type utilityType = INSTANCE.getType("Utility"); public static final Type utilityType = getType("Utility");
public enum Sort { public enum Sort {
VOID( void.class , 0 , true , false , false , false ), VOID( void.class , 0 , true , false , false , false ),
@ -394,8 +394,29 @@ public final class Definition {
} }
} }
final Map<Cast, Cast> transformsMap;
final Map<Class<?>, RuntimeClass> runtimeMap; /** Gets the type given by its name */
public static Type getType(final String name) {
return INSTANCE.getTypeInternal(name);
}
/** Creates an array type from the given Struct. */
public static Type getType(final Struct struct, final int dimensions) {
return INSTANCE.getTypeInternal(struct, dimensions);
}
public static RuntimeClass getRuntimeClass(Class<?> clazz) {
return INSTANCE.runtimeMap.get(clazz);
}
public static Cast getTransform(Cast cast) {
return INSTANCE.transformsMap.get(cast);
}
// INTERNAL IMPLEMENTATION:
private final Map<Cast, Cast> transformsMap;
private final Map<Class<?>, RuntimeClass> runtimeMap;
private final Map<String, Struct> structsMap; private final Map<String, Struct> structsMap;
private final Map<String, Type> simpleTypesMap; private final Map<String, Type> simpleTypesMap;
@ -531,26 +552,26 @@ public final class Definition {
} }
private void addTransforms() { private void addTransforms() {
Type booleanType = getType("boolean"); Type booleanType = getTypeInternal("boolean");
Type objectType = getType("Object"); Type objectType = getTypeInternal("Object");
Type defType = getType("def"); Type defType = getTypeInternal("def");
Type booleanobjType = getType("Boolean"); Type booleanobjType = getTypeInternal("Boolean");
Type byteType = getType("byte"); Type byteType = getTypeInternal("byte");
Type shortType = getType("short"); Type shortType = getTypeInternal("short");
Type intType = getType("int"); Type intType = getTypeInternal("int");
Type charType = getType("char"); Type charType = getTypeInternal("char");
Type longType = getType("long"); Type longType = getTypeInternal("long");
Type floatType = getType("float"); Type floatType = getTypeInternal("float");
Type doubleType = getType("double"); Type doubleType = getTypeInternal("double");
Type numberType = getType("Number"); Type numberType = getTypeInternal("Number");
Type byteobjType = getType("Byte"); Type byteobjType = getTypeInternal("Byte");
Type shortobjType = getType("Short"); Type shortobjType = getTypeInternal("Short");
Type charobjType = getType("Character"); Type charobjType = getTypeInternal("Character");
Type intobjType = getType("Integer"); Type intobjType = getTypeInternal("Integer");
Type longobjType = getType("Long"); Type longobjType = getTypeInternal("Long");
Type floatobjType = getType("Float"); Type floatobjType = getTypeInternal("Float");
Type doubleobjType = getType("Double"); Type doubleobjType = getTypeInternal("Double");
Type stringType = getType("String"); Type stringType = getTypeInternal("String");
addTransform(booleanType, objectType, "Boolean", "valueOf", true, false); addTransform(booleanType, objectType, "Boolean", "valueOf", true, false);
addTransform(booleanType, defType, "Boolean", "valueOf", true, false); addTransform(booleanType, defType, "Boolean", "valueOf", true, false);
@ -848,7 +869,7 @@ public final class Definition {
final Struct struct = new Struct(name, clazz, org.objectweb.asm.Type.getType(clazz)); final Struct struct = new Struct(name, clazz, org.objectweb.asm.Type.getType(clazz));
structsMap.put(name, struct); structsMap.put(name, struct);
simpleTypesMap.put(name, getType(name)); simpleTypesMap.put(name, getTypeInternal(name));
} }
private final void addConstructorInternal(final String struct, final String name, final Type[] args) { private final void addConstructorInternal(final String struct, final String name, final Type[] args) {
@ -919,7 +940,7 @@ public final class Definition {
throw new IllegalArgumentException("Malformed signature: " + signature); throw new IllegalArgumentException("Malformed signature: " + signature);
} }
// method or field type (e.g. return type) // method or field type (e.g. return type)
Type rtn = getType(elements[0]); Type rtn = getTypeInternal(elements[0]);
int parenIndex = elements[1].indexOf('('); int parenIndex = elements[1].indexOf('(');
if (parenIndex != -1) { if (parenIndex != -1) {
// method or ctor // method or ctor
@ -929,7 +950,7 @@ public final class Definition {
String arguments[] = elements[1].substring(parenIndex + 1, parenEnd).split(","); String arguments[] = elements[1].substring(parenIndex + 1, parenEnd).split(",");
args = new Type[arguments.length]; args = new Type[arguments.length];
for (int i = 0; i < arguments.length; i++) { for (int i = 0; i < arguments.length; i++) {
args[i] = getType(arguments[i]); args[i] = getTypeInternal(arguments[i]);
} }
} else { } else {
args = new Type[0]; args = new Type[0];
@ -1275,7 +1296,7 @@ public final class Definition {
if (!owner.clazz.isAssignableFrom(from.clazz)) { if (!owner.clazz.isAssignableFrom(from.clazz)) {
if (from.clazz.isAssignableFrom(owner.clazz)) { if (from.clazz.isAssignableFrom(owner.clazz)) {
upcast = getType(owner.name); upcast = getTypeInternal(owner.name);
} else { } else {
throw new ClassCastException("Transform with owner struct [" + owner.name + "]" + throw new ClassCastException("Transform with owner struct [" + owner.name + "]" +
" and cast type from [" + from.name + "] to cast type to [" + to.name + "] using" + " and cast type from [" + from.name + "] to cast type to [" + to.name + "] using" +
@ -1351,7 +1372,7 @@ public final class Definition {
runtimeMap.put(struct.clazz, new RuntimeClass(methods, getters, setters)); runtimeMap.put(struct.clazz, new RuntimeClass(methods, getters, setters));
} }
public final Type getType(final String name) { private Type getTypeInternal(final String name) {
// simple types (e.g. 0 array dimensions) are a simple hash lookup for speed // simple types (e.g. 0 array dimensions) are a simple hash lookup for speed
Type simple = simpleTypesMap.get(name); Type simple = simpleTypesMap.get(name);
if (simple != null) { if (simple != null) {
@ -1365,10 +1386,10 @@ public final class Definition {
throw new IllegalArgumentException("The struct with name [" + name + "] has not been defined."); throw new IllegalArgumentException("The struct with name [" + name + "] has not been defined.");
} }
return getType(struct, dimensions); return getTypeInternal(struct, dimensions);
} }
public final Type getType(final Struct struct, final int dimensions) { private Type getTypeInternal(final Struct struct, final int dimensions) {
String name = struct.name; String name = struct.name;
org.objectweb.asm.Type type = struct.type; org.objectweb.asm.Type type = struct.type;
Class<?> clazz = struct.clazz; Class<?> clazz = struct.clazz;

View File

@ -182,7 +182,7 @@ public final class Variables {
final Type type; final Type type;
try { try {
type = definition.getType(typestr); type = Definition.getType(typestr);
} catch (final IllegalArgumentException exception) { } catch (final IllegalArgumentException exception) {
throw new IllegalArgumentException("Error " + location + ": Not a type [" + typestr + "]."); throw new IllegalArgumentException("Error " + location + ": Not a type [" + typestr + "].");
} }
@ -190,7 +190,7 @@ public final class Variables {
boolean legal = !name.contains("<"); boolean legal = !name.contains("<");
try { try {
definition.getType(name); Definition.getType(name);
legal = false; legal = false;
} catch (final IllegalArgumentException exception) { } catch (final IllegalArgumentException exception) {
// Do nothing. // Do nothing.

View File

@ -42,7 +42,7 @@ public final class EExplicit extends AExpression {
@Override @Override
void analyze(final CompilerSettings settings, final Definition definition, final Variables variables) { void analyze(final CompilerSettings settings, final Definition definition, final Variables variables) {
try { try {
actual = definition.getType(this.type); actual = Definition.getType(this.type);
} catch (final IllegalArgumentException exception) { } catch (final IllegalArgumentException exception) {
throw new IllegalArgumentException(error("Not a type [" + this.type + "].")); throw new IllegalArgumentException(error("Not a type [" + this.type + "]."));
} }

View File

@ -54,7 +54,7 @@ public final class LBrace extends ALink {
index.analyze(settings, definition, variables); index.analyze(settings, definition, variables);
index = index.cast(settings, definition, variables); index = index.cast(settings, definition, variables);
after = definition.getType(before.struct, before.dimensions - 1); after = Definition.getType(before.struct, before.dimensions - 1);
return this; return this;
} else if (sort == Sort.DEF) { } else if (sort == Sort.DEF) {

View File

@ -50,7 +50,7 @@ public final class LCast extends ALink {
} }
try { try {
after = definition.getType(type); after = Definition.getType(type);
} catch (final IllegalArgumentException exception) { } catch (final IllegalArgumentException exception) {
throw new IllegalArgumentException(error("Not a type [" + type + "].")); throw new IllegalArgumentException(error("Not a type [" + type + "]."));
} }

View File

@ -55,7 +55,7 @@ public final class LNewArray extends ALink {
final Type type; final Type type;
try { try {
type = definition.getType(this.type); type = Definition.getType(this.type);
} catch (final IllegalArgumentException exception) { } catch (final IllegalArgumentException exception) {
throw new IllegalArgumentException(error("Not a type [" + this.type + "].")); throw new IllegalArgumentException(error("Not a type [" + this.type + "]."));
} }
@ -68,7 +68,7 @@ public final class LNewArray extends ALink {
arguments.set(argument, expression.cast(settings, definition, variables)); arguments.set(argument, expression.cast(settings, definition, variables));
} }
after = definition.getType(type.struct, arguments.size()); after = Definition.getType(type.struct, arguments.size());
return this; return this;
} }
@ -87,7 +87,7 @@ public final class LNewArray extends ALink {
if (arguments.size() > 1) { if (arguments.size() > 1) {
adapter.visitMultiANewArrayInsn(after.type.getDescriptor(), after.type.getDimensions()); adapter.visitMultiANewArrayInsn(after.type.getDescriptor(), after.type.getDimensions());
} else { } else {
adapter.newArray(definition.getType(after.struct, 0).type); adapter.newArray(Definition.getType(after.struct, 0).type);
} }
} }

View File

@ -57,7 +57,7 @@ public final class LNewObj extends ALink {
final Type type; final Type type;
try { try {
type = definition.getType(this.type); type = Definition.getType(this.type);
} catch (final IllegalArgumentException exception) { } catch (final IllegalArgumentException exception) {
throw new IllegalArgumentException(error("Not a type [" + this.type + "].")); throw new IllegalArgumentException(error("Not a type [" + this.type + "]."));
} }

View File

@ -51,7 +51,7 @@ public final class LVariable extends ALink {
Type type = null; Type type = null;
try { try {
type = definition.getType(name); type = Definition.getType(name);
} catch (final IllegalArgumentException exception) { } catch (final IllegalArgumentException exception) {
// Do nothing. // Do nothing.
} }