package org.graalvm.compiler.replacements.classfile;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import jdk.internal.vm.compiler.collections.EconomicMap;
import jdk.internal.vm.compiler.collections.Equivalence;
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
import org.graalvm.compiler.bytecode.Bytecode;
import org.graalvm.compiler.bytecode.BytecodeProvider;
import org.graalvm.compiler.serviceprovider.GraalServices;
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.MetaAccessProvider;
import jdk.vm.ci.meta.ResolvedJavaField;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaType;
public final class ClassfileBytecodeProvider implements BytecodeProvider {
private final ClassLoader loader;
private final EconomicMap<Class<?>, Classfile> classfiles = EconomicMap.create(Equivalence.IDENTITY);
private final EconomicMap<String, Class<?>> classes = EconomicMap.create();
private final EconomicMap<ResolvedJavaType, FieldsCache> fields = EconomicMap.create();
private final EconomicMap<ResolvedJavaType, MethodsCache> methods = EconomicMap.create();
final MetaAccessProvider metaAccess;
final SnippetReflectionProvider snippetReflection;
public ClassfileBytecodeProvider(MetaAccessProvider metaAccess, SnippetReflectionProvider snippetReflection) {
this.metaAccess = metaAccess;
this.snippetReflection = snippetReflection;
ClassLoader cl = getClass().getClassLoader();
this.loader = cl == null ? ClassLoader.getSystemClassLoader() : cl;
}
public ClassfileBytecodeProvider(MetaAccessProvider metaAccess, SnippetReflectionProvider snippetReflection, ClassLoader loader) {
this.metaAccess = metaAccess;
this.snippetReflection = snippetReflection;
this.loader = loader;
}
@Override
public Bytecode getBytecode(ResolvedJavaMethod method) {
Classfile classfile = getClassfile(resolveToClass(method.getDeclaringClass().getName()));
return classfile.getCode(method.getName(), method.getSignature().toMethodDescriptor());
}
@Override
public boolean supportsInvokedynamic() {
return false;
}
@Override
public boolean shouldRecordMethodDependencies() {
return false;
}
private synchronized Classfile getClassfile(Class<?> c) {
assert !c.isPrimitive() && !c.isArray() : c;
Classfile classfile = classfiles.get(c);
if (classfile == null) {
try {
ResolvedJavaType type = metaAccess.lookupJavaType(c);
InputStream in = GraalServices.getClassfileAsStream(c);
if (in != null) {
DataInputStream stream = new DataInputStream(in);
classfile = new Classfile(type, stream, this);
classfiles.put(c, classfile);
return classfile;
}
throw new NoClassDefFoundError(c.getName());
} catch (IOException e) {
throw (NoClassDefFoundError) new NoClassDefFoundError(c.getName()).initCause(e);
}
}
return classfile;
}
synchronized Class<?> resolveToClass(String descriptor) {
Class<?> c = classes.get(descriptor);
if (c == null) {
if (descriptor.length() == 1) {
c = JavaKind.fromPrimitiveOrVoidTypeChar(descriptor.charAt(0)).toJavaClass();
} else {
int dimensions = 0;
while (descriptor.charAt(dimensions) == '[') {
dimensions++;
}
String name;
if (dimensions == 0 && descriptor.startsWith("L") && descriptor.endsWith(";")) {
name = descriptor.substring(1, descriptor.length() - 1).replace('/', '.');
} else {
name = descriptor.replace('/', '.');
}
try {
c = Class.forName(name, true, loader);
classes.put(descriptor, c);
} catch (ClassNotFoundException e) {
throw new NoClassDefFoundError(descriptor);
}
}
}
return c;
}
static final class FieldKey {
final String name;
final String type;
FieldKey(String name, String type) {
this.name = name;
this.type = type;
}
@Override
public String toString() {
return name + ":" + type;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof FieldKey) {
FieldKey that = (FieldKey) obj;
return that.name.equals(this.name) && that.type.equals(this.type);
}
return false;
}
@Override
public int hashCode() {
return name.hashCode() ^ type.hashCode();
}
}
static final class MethodKey {
final String name;
final String descriptor;
MethodKey(String name, String descriptor) {
this.name = name;
this.descriptor = descriptor;
}
@Override
public String toString() {
return name + ":" + descriptor;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof MethodKey) {
MethodKey that = (MethodKey) obj;
return that.name.equals(this.name) && that.descriptor.equals(this.descriptor);
}
return false;
}
@Override
public int hashCode() {
return name.hashCode() ^ descriptor.hashCode();
}
}
static final class MethodsCache {
volatile EconomicMap<MethodKey, ResolvedJavaMethod> constructors;
volatile EconomicMap<MethodKey, ResolvedJavaMethod> methods;
ResolvedJavaMethod lookup(ResolvedJavaType type, String name, String descriptor) {
MethodKey key = new MethodKey(name, descriptor);
if (name.equals("<clinit>")) {
return type.getClassInitializer();
}
if (!name.equals("<init>")) {
if (methods == null) {
methods = createMethodMap(type.getDeclaredMethods());
}
return methods.get(key);
} else {
if (constructors == null) {
constructors = createMethodMap(type.getDeclaredConstructors());
}
return constructors.get(key);
}
}
private static EconomicMap<MethodKey, ResolvedJavaMethod> createMethodMap(ResolvedJavaMethod[] methodArray) {
EconomicMap<MethodKey, ResolvedJavaMethod> map = EconomicMap.create();
for (ResolvedJavaMethod m : methodArray) {
map.put(new MethodKey(m.getName(), m.getSignature().toMethodDescriptor()), m);
}
return map;
}
}
static final class FieldsCache {
volatile EconomicMap<FieldKey, ResolvedJavaField> instanceFields;
volatile EconomicMap<FieldKey, ResolvedJavaField> staticFields;
ResolvedJavaField lookup(ResolvedJavaType type, String name, String fieldType, boolean isStatic) {
FieldKey key = new FieldKey(name, fieldType);
if (isStatic) {
if (staticFields == null) {
staticFields = createFieldMap(type.getStaticFields());
}
return staticFields.get(key);
} else {
if (instanceFields == null) {
instanceFields = createFieldMap(type.getInstanceFields(false));
}
return instanceFields.get(key);
}
}
private static EconomicMap<FieldKey, ResolvedJavaField> createFieldMap(ResolvedJavaField[] fieldArray) {
EconomicMap<FieldKey, ResolvedJavaField> map = EconomicMap.create();
for (ResolvedJavaField f : fieldArray) {
map.put(new FieldKey(f.getName(), f.getType().getName()), f);
}
return map;
}
}
private synchronized MethodsCache getMethods(ResolvedJavaType type) {
MethodsCache methodsCache = methods.get(type);
if (methodsCache == null) {
methodsCache = new MethodsCache();
methods.put(type, methodsCache);
}
return methodsCache;
}
private synchronized FieldsCache getFields(ResolvedJavaType type) {
FieldsCache fieldsCache = fields.get(type);
if (fieldsCache == null) {
fieldsCache = new FieldsCache();
fields.put(type, fieldsCache);
}
return fieldsCache;
}
ResolvedJavaField findField(ResolvedJavaType type, String name, String fieldType, boolean isStatic) {
return getFields(type).lookup(type, name, fieldType, isStatic);
}
ResolvedJavaMethod findMethod(ResolvedJavaType type, String name, String descriptor, boolean isStatic) {
ResolvedJavaMethod method = getMethods(type).lookup(type, name, descriptor);
if (method != null && method.isStatic() == isStatic) {
return method;
}
return null;
}
}