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();
}
public static SourceLocation getLocation() {
unsupported();
return null;
}
public static ReflectClass<?> findClass(String name) {
unsupported();
return null;

View File

@ -183,6 +183,29 @@ public final class MetaprogrammingImpl {
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")
public static ReflectClass<?> findClass(String name) {
return reflectContext.findClass(name);
@ -405,6 +428,9 @@ public final class MetaprogrammingImpl {
}
private CallLocation convertLocation(SourceLocation location) {
if (location == null) {
return null;
}
MethodReader method = ((ReflectMethodImpl) location.getMethod()).method;
return location.getFileName() != null
? new CallLocation(method.getReference(),

View File

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

View File

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