Commit 244f6a90 authored by Andrea Rosà's avatar Andrea Rosà
Browse files

Reflective support for fields added

parent f76f9355
......@@ -21,9 +21,9 @@ import ch.usi.dag.disl.util.JavaNames;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.MethodNode;
/* TODO
* 1) Set up a policy to deal with unknown classloaders (i.e., when force-loading is disabled). At the moment, this issue is kind of ignored
* 2) Redesign classloading hierarchy. At the moment it is disabled completely. It can be possible to save space
......@@ -295,6 +295,7 @@ public final class Reflection {
}
void notifyClassLoaded (final ClassNode cn) {
//System.err.println ("CL: inserting class " + cn.name + " inside classloader " + __tag);
__classes.computeIfAbsent (
cn.name, k -> new RegularClass (this, cn)
);
......@@ -303,6 +304,7 @@ public final class Reflection {
//
void insertClass(final Class cl) {
//System.err.println ("CL: inserting class " + cl.internalName () + " inside classloader " + __tag);
__classes.putIfAbsent (cl.internalName (), cl);
}
......@@ -404,7 +406,7 @@ public final class Reflection {
private RegularClassLoader (final BootstrapClassLoader parent, final int tag) {
/* Hierarchy between classloader removed as on June 7, 2017.
* Might be added again in future for optimization (set EVERY other classloader apart
* from the bootstrap class loader to be child of the bootstrap class loader)
* from the bootstrap class loader to be children of the bootstrap class loader)
*/
//__parent = Optional.of (parent);
......@@ -591,7 +593,7 @@ public final class Reflection {
/**
* Returns an {@link Optional} containing the {@link Method}
* corresponding to the given signature. The {@link Optional} will be
* empty if the could not be found.
* empty if the method could not be found.
*
* @param sig
* the method signature to look for
......@@ -600,6 +602,19 @@ public final class Reflection {
*/
public abstract Optional <Method> methodForSignature (final String sig);
/**
* Returns an {@link Optional} containing the {@link Field}
* corresponding to the given name. The {@link Optional} will be
* empty if the field could not be found.
*
* @param name
* the field name to look for
* @return an {@link Optional} containing the {@link Field}
* corresponding to the given signature.
*/
public abstract Optional <Field> fieldForName (final String name);
// Two Class are equals if they have the same ClassLoader, as well as the same internalName
@Override
public final boolean equals(final Object o) {
......@@ -703,6 +718,10 @@ public final class Reflection {
return Optional.empty ();
}
@Override
public Optional <Field> fieldForName (final String name) {
return Optional.empty ();
}
}
......@@ -714,6 +733,7 @@ public final class Reflection {
private final int __modifiers;
private final Optional <String> __superName;
private final Map <String, Method> __methods;
private final Map <String, Field> __fields;
private final List <String> __interfaces;
//
......@@ -725,6 +745,7 @@ public final class Reflection {
__superName = Optional.ofNullable (node.superName);
__methods = __createMethods (node.methods);
__interfaces = new ArrayList <> (node.interfaces);
__fields = __createFields (node.fields);
}
private ConcurrentMap <String, Method> __createMethods (final List <MethodNode> methods) {
......@@ -735,6 +756,15 @@ public final class Reflection {
));
}
private ConcurrentMap<String, Field> __createFields (final List <FieldNode> fields) {
return fields.parallelStream ()
.map (fn -> new Field (this, fn))
.collect (Collectors.toConcurrentMap (
Field::name, Function.identity()
));
}
//
@Override
......@@ -824,6 +854,11 @@ public final class Reflection {
return Optional.ofNullable (__methods.get (sig));
}
@Override
public Optional <Field> fieldForName (final String name) {
return Optional.ofNullable (__fields.get (name));
}
}
//
......@@ -912,6 +947,10 @@ public final class Reflection {
return Optional.empty ();
}
@Override
public Optional <Field> fieldForName (final String name) {
return Optional.empty ();
}
}
......@@ -967,10 +1006,52 @@ public final class Reflection {
/**
* Returns {@code true} if this method is static.
*/
@Deprecated
public boolean isStatic () {
return Modifier.isStatic (__modifiers);
}
public int getModifiers() {
return __modifiers;
}
}
///
public static final class Field {
private final Class __class;
private final String __name;
private final Type __type;
private final int __modifiers;
//
private Field (final Class cls, final FieldNode fn) {
__class = cls;
__name = fn.name;
__type = Type.getType(fn.desc);
__modifiers = fn.access;
}
//
public String name () {
return __name;
}
public Type type () {
return __type;
}
public Class klass() {
return __class;
}
public int getModifiers() {
return __modifiers;
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment