Add support of Metaprogramming.getLocation to metaprogramming API

This commit is contained in:
Alexey Andreev 2017-02-26 00:07:40 +03:00
parent 929e77bf69
commit af9a01628f
4 changed files with 34 additions and 2 deletions

View File

@ -55,6 +55,11 @@ public final class Metaprogramming {
unsupported(); unsupported();
} }
public static SourceLocation getLocation() {
unsupported();
return null;
}
public static ReflectClass<?> findClass(String name) { public static ReflectClass<?> findClass(String name) {
unsupported(); unsupported();
return null; return null;

View File

@ -183,6 +183,29 @@ public final class MetaprogrammingImpl {
generator.forcedLocation = null; generator.forcedLocation = null;
} }
public static SourceLocation getLocation() {
TextLocation location = generator.forcedLocation;
if (location == null) {
location = generator.location;
}
if (location == null) {
return null;
}
ReflectClassImpl<?> cls = reflectContext.findClass(templateMethod.getClassName());
if (cls == null) {
return null;
}
cls.resolve();
MethodReader methodReader = cls.classReader.getMethod(templateMethod.getDescriptor());
if (methodReader == null) {
return null;
}
ReflectMethod method = new ReflectMethodImpl(cls, methodReader);
return new SourceLocation(method, location != null ? location.getFileName() : null,
location != null ? location.getLine() : null);
}
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
public static ReflectClass<?> findClass(String name) { public static ReflectClass<?> findClass(String name) {
return reflectContext.findClass(name); return reflectContext.findClass(name);
@ -405,6 +428,9 @@ public final class MetaprogrammingImpl {
} }
private CallLocation convertLocation(SourceLocation location) { private CallLocation convertLocation(SourceLocation location) {
if (location == null) {
return null;
}
MethodReader method = ((ReflectMethodImpl) location.getMethod()).method; MethodReader method = ((ReflectMethodImpl) location.getMethod()).method;
return location.getFileName() != null return location.getFileName() != null
? new CallLocation(method.getReference(), ? new CallLocation(method.getReference(),

View File

@ -129,6 +129,7 @@ class UsageGenerator {
MetaprogrammingImpl.generator = new CompositeMethodGenerator(varContext); MetaprogrammingImpl.generator = new CompositeMethodGenerator(varContext);
MetaprogrammingImpl.varContext = varContext; MetaprogrammingImpl.varContext = varContext;
MetaprogrammingImpl.returnType = model.getMethod().getReturnType(); MetaprogrammingImpl.returnType = model.getMethod().getReturnType();
MetaprogrammingImpl.generator.location = location != null ? location.getSourceLocation() : null;
for (int i = 0; i <= model.getMetaParameterCount(); ++i) { for (int i = 0; i <= model.getMetaParameterCount(); ++i) {
MetaprogrammingImpl.generator.getProgram().createVariable(); MetaprogrammingImpl.generator.getProgram().createVariable();

View File

@ -39,7 +39,7 @@ import org.teavm.model.ValueType;
public class ReflectClassImpl<T> implements ReflectClass<T> { public class ReflectClassImpl<T> implements ReflectClass<T> {
public final ValueType type; public final ValueType type;
private ReflectContext context; private ReflectContext context;
private ClassReader classReader; public ClassReader classReader;
private boolean resolved; private boolean resolved;
private Class<?> cls; private Class<?> cls;
private Map<String, ReflectFieldImpl> declaredFields = new HashMap<>(); private Map<String, ReflectFieldImpl> declaredFields = new HashMap<>();
@ -380,7 +380,7 @@ public class ReflectClassImpl<T> implements ReflectClass<T> {
return annotations.getAnnotation(type); return annotations.getAnnotation(type);
} }
private void resolve() { public void resolve() {
if (resolved) { if (resolved) {
return; return;
} }