Add reflection support for constructors

This commit is contained in:
Alexey Andreev 2016-10-18 23:51:31 +03:00
parent 31f9ca57ce
commit c4c5635f88
16 changed files with 417 additions and 69 deletions

View File

@ -16,7 +16,10 @@
package org.teavm.classlib; package org.teavm.classlib;
import java.util.Collection; import java.util.Collection;
import org.teavm.model.MethodDescriptor;
public interface ReflectionSupplier { public interface ReflectionSupplier {
Collection<String> getAccessibleFields(ReflectionContext context, String className); Collection<String> getAccessibleFields(ReflectionContext context, String className);
Collection<MethodDescriptor> getAccessibleMethods(ReflectionContext context, String className);
} }

View File

@ -15,9 +15,10 @@
*/ */
package org.teavm.classlib.impl; package org.teavm.classlib.impl;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.HashMap; import java.util.LinkedHashMap;
import java.util.HashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -33,6 +34,9 @@ import org.teavm.model.ClassReader;
import org.teavm.model.ClassReaderSource; import org.teavm.model.ClassReaderSource;
import org.teavm.model.ElementModifier; import org.teavm.model.ElementModifier;
import org.teavm.model.FieldReader; import org.teavm.model.FieldReader;
import org.teavm.model.MemberReader;
import org.teavm.model.MethodDescriptor;
import org.teavm.model.MethodReader;
import org.teavm.model.MethodReference; import org.teavm.model.MethodReference;
import org.teavm.model.ValueType; import org.teavm.model.ValueType;
@ -40,11 +44,18 @@ public class ReflectionDependencyListener extends AbstractDependencyListener {
private List<ReflectionSupplier> reflectionSuppliers; private List<ReflectionSupplier> reflectionSuppliers;
private MethodReference fieldGet = new MethodReference(Field.class, "get", Object.class, Object.class); private MethodReference fieldGet = new MethodReference(Field.class, "get", Object.class, Object.class);
private MethodReference fieldSet = new MethodReference(Field.class, "set", Object.class, Object.class, void.class); private MethodReference fieldSet = new MethodReference(Field.class, "set", Object.class, Object.class, void.class);
private MethodReference newInstance = new MethodReference(Constructor.class, "newInstance", Object[].class,
Object.class);
private MethodReference getFields = new MethodReference(Class.class, "getDeclaredFields", Field[].class); private MethodReference getFields = new MethodReference(Class.class, "getDeclaredFields", Field[].class);
private MethodReference getConstructors = new MethodReference(Class.class, "getDeclaredConstructors",
Constructor[].class);
private boolean fieldGetHandled; private boolean fieldGetHandled;
private boolean fieldSetHandled; private boolean fieldSetHandled;
private Map<String, Set<String>> accessibleFieldCache = new HashMap<>(); private boolean newInstanceHandled;
private Set<String> classesWithReflectableFields = new HashSet<>(); private Map<String, Set<String>> accessibleFieldCache = new LinkedHashMap<>();
private Map<String, Set<MethodDescriptor>> accessibleMethodCache = new LinkedHashMap<>();
private Set<String> classesWithReflectableFields = new LinkedHashSet<>();
private Set<String> classesWithReflectableMethods = new LinkedHashSet<>();
public ReflectionDependencyListener(List<ReflectionSupplier> reflectionSuppliers) { public ReflectionDependencyListener(List<ReflectionSupplier> reflectionSuppliers) {
this.reflectionSuppliers = reflectionSuppliers; this.reflectionSuppliers = reflectionSuppliers;
@ -54,22 +65,38 @@ public class ReflectionDependencyListener extends AbstractDependencyListener {
return classesWithReflectableFields; return classesWithReflectableFields;
} }
public Set<String> getClassesWithReflectableMethods() {
return classesWithReflectableMethods;
}
public Set<String> getAccessibleFields(String className) { public Set<String> getAccessibleFields(String className) {
return accessibleFieldCache.get(className); return accessibleFieldCache.get(className);
} }
public Set<MethodDescriptor> getAccessibleMethods(String className) {
return accessibleMethodCache.get(className);
}
@Override @Override
public void methodReached(DependencyAgent agent, MethodDependency method, CallLocation location) { public void methodReached(DependencyAgent agent, MethodDependency method, CallLocation location) {
if (method.getReference().equals(fieldGet)) { if (method.getReference().equals(fieldGet)) {
handleFieldGet(agent, method, location); handleFieldGet(agent, method, location);
} else if (method.getReference().equals(fieldSet)) { } else if (method.getReference().equals(fieldSet)) {
handleFieldSet(agent, method, location); handleFieldSet(agent, method, location);
} else if (method.getReference().equals(newInstance)) {
handleNewInstance(agent, method, location);
} else if (method.getReference().equals(getFields)) { } else if (method.getReference().equals(getFields)) {
method.getVariable(0).getClassValueNode().addConsumer(type -> { method.getVariable(0).getClassValueNode().addConsumer(type -> {
if (!type.getName().startsWith("[")) { if (!type.getName().startsWith("[")) {
classesWithReflectableFields.add(type.getName()); classesWithReflectableFields.add(type.getName());
} }
}); });
} else if (method.getReference().equals(getConstructors)) {
method.getVariable(0).getClassValueNode().addConsumer(type -> {
if (!type.getName().startsWith("[")) {
classesWithReflectableMethods.add(type.getName());
}
});
} }
} }
@ -118,9 +145,37 @@ public class ReflectionDependencyListener extends AbstractDependencyListener {
}); });
} }
private void linkClassIfNecessary(DependencyAgent agent, FieldReader field, CallLocation location) { private void handleNewInstance(DependencyAgent agent, MethodDependency method, CallLocation location) {
if (field.hasModifier(ElementModifier.STATIC)) { if (newInstanceHandled) {
agent.linkClass(field.getOwnerName(), location).initClass(location); return;
}
newInstanceHandled = true;
DependencyNode classValueNode = agent.linkMethod(getConstructors, location).getVariable(0).getClassValueNode();
classValueNode.addConsumer(reflectedType -> {
if (reflectedType.getName().startsWith("[")) {
return;
}
Set<MethodDescriptor> accessibleMethods = getAccessibleMethods(agent, reflectedType.getName());
ClassReader cls = agent.getClassSource().get(reflectedType.getName());
for (MethodDescriptor methodDescriptor : accessibleMethods) {
MethodReader calledMethod = cls.getMethod(methodDescriptor);
MethodDependency calledMethodDep = agent.linkMethod(method.getReference(), location);
calledMethodDep.use();
for (int i = 0; i < methodDescriptor.parameterCount(); ++i) {
propagateSet(agent, methodDescriptor.parameterType(i), method.getVariable(1).getArrayItem(),
calledMethodDep.getVariable(i + 1), location);
}
calledMethodDep.getVariable(0).propagate(reflectedType);
linkClassIfNecessary(agent, calledMethod, location);
}
});
}
private void linkClassIfNecessary(DependencyAgent agent, MemberReader member, CallLocation location) {
if (member.hasModifier(ElementModifier.STATIC)) {
agent.linkClass(member.getOwnerName(), location).initClass(location);
} }
} }
@ -128,15 +183,28 @@ public class ReflectionDependencyListener extends AbstractDependencyListener {
return accessibleFieldCache.computeIfAbsent(className, key -> gatherAccessibleFields(agent, key)); return accessibleFieldCache.computeIfAbsent(className, key -> gatherAccessibleFields(agent, key));
} }
private Set<MethodDescriptor> getAccessibleMethods(DependencyAgent agent, String className) {
return accessibleMethodCache.computeIfAbsent(className, key -> gatherAccessibleMethods(agent, key));
}
private Set<String> gatherAccessibleFields(DependencyAgent agent, String className) { private Set<String> gatherAccessibleFields(DependencyAgent agent, String className) {
ReflectionContextImpl context = new ReflectionContextImpl(agent); ReflectionContextImpl context = new ReflectionContextImpl(agent);
Set<String> fields = new HashSet<>(); Set<String> fields = new LinkedHashSet<>();
for (ReflectionSupplier supplier : reflectionSuppliers) { for (ReflectionSupplier supplier : reflectionSuppliers) {
fields.addAll(supplier.getAccessibleFields(context, className)); fields.addAll(supplier.getAccessibleFields(context, className));
} }
return fields; return fields;
} }
private Set<MethodDescriptor> gatherAccessibleMethods(DependencyAgent agent, String className) {
ReflectionContextImpl context = new ReflectionContextImpl(agent);
Set<MethodDescriptor> methods = new LinkedHashSet<>();
for (ReflectionSupplier supplier : reflectionSuppliers) {
methods.addAll(supplier.getAccessibleMethods(context, className));
}
return methods;
}
private void propagateGet(DependencyAgent agent, ValueType type, DependencyNode sourceNode, private void propagateGet(DependencyAgent agent, ValueType type, DependencyNode sourceNode,
DependencyNode targetNode, CallLocation location) { DependencyNode targetNode, CallLocation location) {
if (type instanceof ValueType.Primitive) { if (type instanceof ValueType.Primitive) {

View File

@ -16,15 +16,19 @@
package org.teavm.classlib.impl.reflection; package org.teavm.classlib.impl.reflection;
import org.teavm.backend.javascript.spi.InjectedBy; import org.teavm.backend.javascript.spi.InjectedBy;
import org.teavm.jso.JSObject; import org.teavm.platform.PlatformObject;
import org.teavm.platform.PlatformSequence;
public final class Converter { public final class Converter {
private Converter() { private Converter() {
} }
@InjectedBy(ConverterInjector.class) @InjectedBy(ConverterInjector.class)
public static native Object toJava(JSObject jsObject); public static native Object toJava(PlatformObject jsObject);
@InjectedBy(ConverterInjector.class) @InjectedBy(ConverterInjector.class)
public static native JSObject fromJava(Object object); public static native PlatformObject fromJava(Object object);
@InjectedBy(ConverterInjector.class)
public static native PlatformSequence<PlatformObject> arrayFromJava(Object[] objects);
} }

View File

@ -0,0 +1,26 @@
/*
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.impl.reflection;
import org.teavm.jso.JSFunctor;
import org.teavm.jso.JSObject;
import org.teavm.platform.PlatformObject;
import org.teavm.platform.PlatformSequence;
@JSFunctor
public interface JSCallable extends JSObject {
PlatformObject call(PlatformObject object, PlatformSequence<PlatformObject> arguments);
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.impl.reflection;
import org.teavm.jso.JSProperty;
import org.teavm.platform.PlatformClass;
import org.teavm.platform.PlatformSequence;
public interface JSConstructor extends JSMember {
@JSProperty
PlatformSequence<PlatformClass> getParameterTypes();
@JSProperty
JSCallable getCallable();
}

View File

@ -15,20 +15,10 @@
*/ */
package org.teavm.classlib.impl.reflection; package org.teavm.classlib.impl.reflection;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty; import org.teavm.jso.JSProperty;
import org.teavm.platform.PlatformClass; import org.teavm.platform.PlatformClass;
public interface JSField extends JSObject { public interface JSField extends JSMember {
@JSProperty
String getName();
@JSProperty
int getModifiers();
@JSProperty
int getAccessLevel();
@JSProperty @JSProperty
PlatformClass getType(); PlatformClass getType();

View File

@ -21,5 +21,5 @@ import org.teavm.platform.PlatformObject;
@JSFunctor @JSFunctor
public interface JSFieldGetter extends JSObject { public interface JSFieldGetter extends JSObject {
JSObject get(PlatformObject instance); PlatformObject get(PlatformObject instance);
} }

View File

@ -21,5 +21,5 @@ import org.teavm.platform.PlatformObject;
@JSFunctor @JSFunctor
public interface JSFieldSetter extends JSObject { public interface JSFieldSetter extends JSObject {
void set(PlatformObject instance, JSObject value); void set(PlatformObject instance, PlatformObject value);
} }

View File

@ -0,0 +1,30 @@
/*
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.impl.reflection;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
public interface JSMember extends JSObject {
@JSProperty
String getName();
@JSProperty
int getModifiers();
@JSProperty
int getAccessLevel();
}

View File

@ -25,6 +25,7 @@ import org.teavm.classlib.impl.ReflectionDependencyListener;
import org.teavm.model.ClassReader; import org.teavm.model.ClassReader;
import org.teavm.model.ElementModifier; import org.teavm.model.ElementModifier;
import org.teavm.model.FieldReader; import org.teavm.model.FieldReader;
import org.teavm.model.MemberReader;
import org.teavm.model.MethodReference; import org.teavm.model.MethodReference;
import org.teavm.model.ValueType; import org.teavm.model.ValueType;
@ -32,20 +33,23 @@ public class ClassGenerator implements Generator {
@Override @Override
public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException { public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException {
switch (methodRef.getName()) { switch (methodRef.getName()) {
case "createJsFields": case "createMetadata":
generateCreateJsFields(context, writer); generateCreateMetadata(context, writer);
break; break;
} }
} }
private void generateCreateJsFields(GeneratorContext context, SourceWriter writer) throws IOException { private void generateCreateMetadata(GeneratorContext context, SourceWriter writer) throws IOException {
ReflectionDependencyListener reflection = context.getService(ReflectionDependencyListener.class); ReflectionDependencyListener reflection = context.getService(ReflectionDependencyListener.class);
for (String className : reflection.getClassesWithReflectableFields()) { for (String className : reflection.getClassesWithReflectableFields()) {
generateCreateJsFieldsForClass(context, writer, className); generateCreateFieldsForClass(context, writer, className);
}
for (String className : reflection.getClassesWithReflectableMethods()) {
generateCreateMethodsForClass(context, writer, className);
} }
} }
private void generateCreateJsFieldsForClass(GeneratorContext context, SourceWriter writer, String className) private void generateCreateFieldsForClass(GeneratorContext context, SourceWriter writer, String className)
throws IOException { throws IOException {
ReflectionDependencyListener reflection = context.getService(ReflectionDependencyListener.class); ReflectionDependencyListener reflection = context.getService(ReflectionDependencyListener.class);
Set<String> accessibleFields = reflection.getAccessibleFields(className); Set<String> accessibleFields = reflection.getAccessibleFields(className);
@ -53,8 +57,43 @@ public class ClassGenerator implements Generator {
ClassReader cls = context.getClassSource().get(className); ClassReader cls = context.getClassSource().get(className);
writer.appendClass(className).append(".$meta.fields").ws().append('=').ws().append('[').indent(); writer.appendClass(className).append(".$meta.fields").ws().append('=').ws().append('[').indent();
generateCreateMembers(writer, cls.getFields(), field -> {
appendProperty(writer, "type", false, () -> writer.append(context.typeToClassString(field.getType())));
appendProperty(writer, "getter", false, () -> {
if (accessibleFields != null && accessibleFields.contains(field.getName())) {
renderGetter(writer, field);
} else {
writer.append("null");
}
});
appendProperty(writer, "setter", false, () -> {
if (accessibleFields != null && accessibleFields.contains(field.getName())) {
renderSetter(writer, field);
} else {
writer.append("null");
}
});
});
writer.outdent().append("];").softNewLine();
}
private void generateCreateMethodsForClass(GeneratorContext context, SourceWriter writer, String className)
throws IOException {
ReflectionDependencyListener reflection = context.getService(ReflectionDependencyListener.class);
ClassReader cls = context.getClassSource().get(className);
writer.appendClass(className).append(".$meta.methods").ws().append('=').ws().append('[').indent();
writer.outdent().append("];").softNewLine();
}
private <T extends MemberReader> void generateCreateMembers(SourceWriter writer, Iterable<T> members,
MemberRenderer<T> renderer) throws IOException {
boolean first = true; boolean first = true;
for (FieldReader field : cls.getFields()) { for (T member : members) {
if (!first) { if (!first) {
writer.append(",").ws(); writer.append(",").ws();
} else { } else {
@ -63,37 +102,25 @@ public class ClassGenerator implements Generator {
first = false; first = false;
writer.append("{").indent().softNewLine(); writer.append("{").indent().softNewLine();
writer.append("name").ws().append(':').ws().append('"') appendProperty(writer, "name", true, () -> writer.append('"')
.append(RenderingUtil.escapeString(field.getName())) .append(RenderingUtil.escapeString(member.getName())).append('"'));
.append("\",").softNewLine(); appendProperty(writer, "modifiers", false, () -> writer.append(packModifiers(member.readModifiers())));
writer.append("modifiers").ws().append(':').ws().append(packModifiers(field.readModifiers())) appendProperty(writer, "accessLevel", false, () -> writer.append(member.getLevel().ordinal()));
.append(",").softNewLine(); renderer.render(member);
writer.append("accessLevel").ws().append(':').ws().append(field.getLevel().ordinal())
.append(",").softNewLine();
writer.append("type").ws().append(':').ws().append(context.typeToClassString(field.getType()))
.append(",").softNewLine();
writer.append("getter").ws().append(':').ws();
if (accessibleFields != null && accessibleFields.contains(field.getName())) {
renderGetter(writer, field);
} else {
writer.append("null");
}
writer.append(",").softNewLine();
writer.append("setter").ws().append(':').ws();
if (accessibleFields != null && accessibleFields.contains(field.getName())) {
renderSetter(writer, field);
} else {
writer.append("null");
}
writer.outdent().softNewLine().append("}"); writer.outdent().softNewLine().append("}");
} }
writer.outdent().append("];").softNewLine(); writer.outdent().append("];").softNewLine();
} }
private void appendProperty(SourceWriter writer, String name, boolean first, Fragment value) throws IOException {
if (!first) {
writer.append(",").softNewLine();
}
writer.append(name).ws().append(':').ws();
value.render();
}
private void renderGetter(SourceWriter writer, FieldReader field) throws IOException { private void renderGetter(SourceWriter writer, FieldReader field) throws IOException {
writer.append("function(obj)").ws().append("{").indent().softNewLine(); writer.append("function(obj)").ws().append("{").indent().softNewLine();
initClass(writer, field); initClass(writer, field);
@ -209,6 +236,10 @@ public class ClassGenerator implements Generator {
void render() throws IOException; void render() throws IOException;
} }
private interface MemberRenderer<T extends MemberReader> {
void render(T member) throws IOException;
}
private int packModifiers(Set<ElementModifier> elementModifiers) { private int packModifiers(Set<ElementModifier> elementModifiers) {
ElementModifier[] knownModifiers = ElementModifier.values(); ElementModifier[] knownModifiers = ElementModifier.values();
int value = 0; int value = 0;

View File

@ -53,7 +53,7 @@ public class TClass<T> extends TObject implements TAnnotatedElement {
private Map<TClass<?>, TAnnotation> annotationsByType; private Map<TClass<?>, TAnnotation> annotationsByType;
private TField[] declaredFields; private TField[] declaredFields;
private TField[] fields; private TField[] fields;
private static boolean jsFieldsInitialized; private static boolean reflectionInitialized;
private TClass(PlatformClass platformClass) { private TClass(PlatformClass platformClass) {
this.platformClass = platformClass; this.platformClass = platformClass;
@ -142,7 +142,7 @@ public class TClass<T> extends TObject implements TAnnotatedElement {
public TField[] getDeclaredFields() throws TSecurityException { public TField[] getDeclaredFields() throws TSecurityException {
if (declaredFields == null) { if (declaredFields == null) {
initJsFields(); initReflection();
JSClass jsClass = (JSClass) getPlatformClass().getMetadata(); JSClass jsClass = (JSClass) getPlatformClass().getMetadata();
JSArray<JSField> jsFields = jsClass.getFields(); JSArray<JSField> jsFields = jsClass.getFields();
declaredFields = new TField[jsFields.getLength()]; declaredFields = new TField[jsFields.getLength()];
@ -156,15 +156,15 @@ public class TClass<T> extends TObject implements TAnnotatedElement {
return declaredFields; return declaredFields;
} }
private static void initJsFields() { private static void initReflection() {
if (!jsFieldsInitialized) { if (!reflectionInitialized) {
jsFieldsInitialized = true; reflectionInitialized = true;
createJsFields(); createMetadata();
} }
} }
@GeneratedBy(ClassGenerator.class) @GeneratedBy(ClassGenerator.class)
private static native void createJsFields(); private static native void createMetadata();
public TField[] getFields() throws TSecurityException { public TField[] getFields() throws TSecurityException {
if (fields == null) { if (fields == null) {

View File

@ -0,0 +1,122 @@
/*
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.java.lang.reflect;
import org.teavm.classlib.impl.reflection.Converter;
import org.teavm.classlib.impl.reflection.Flags;
import org.teavm.classlib.impl.reflection.JSCallable;
import org.teavm.classlib.java.lang.TClass;
import org.teavm.classlib.java.lang.TIllegalAccessException;
import org.teavm.classlib.java.lang.TIllegalArgumentException;
import org.teavm.classlib.java.lang.TInstantiationException;
import org.teavm.classlib.java.lang.TObject;
import org.teavm.platform.PlatformObject;
import org.teavm.platform.PlatformSequence;
public class TConstructor<T> extends TAccessibleObject implements TMember {
private TClass<T> declaringClass;
private String name;
private int modifiers;
private int accessLevel;
private TClass<?>[] parameterTypes;
private JSCallable callable;
public TConstructor(TClass<T> declaringClass, String name, int modifiers, int accessLevel,
TClass<?>[] parameterTypes, JSCallable callable) {
this.declaringClass = declaringClass;
this.name = name;
this.modifiers = modifiers;
this.accessLevel = accessLevel;
this.parameterTypes = parameterTypes;
this.callable = callable;
}
@Override
public TClass<T> getDeclaringClass() {
return declaringClass;
}
@Override
public String getName() {
return name;
}
@Override
public int getModifiers() {
return Flags.getModifiers(modifiers, accessLevel);
}
@Override
public boolean isSynthetic() {
return (modifiers & Flags.SYNTHETIC) != 0;
}
public TClass<?>[] getParameterTypes() {
return parameterTypes.clone();
}
public int getParameterCount() {
return parameterTypes.length;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(TModifier.toString(getModifiers()));
if (sb.length() > 0) {
sb.append(' ');
}
sb.append(declaringClass.getName().toString()).append('(');
for (int i = 0; i < parameterTypes.length; ++i) {
if (i > 0) {
sb.append(',');
}
sb.append(parameterTypes[i].getName());
}
return sb.append(')').toString();
}
@SuppressWarnings("unchecked")
public T newInstance(Object... initargs) throws TInstantiationException, TIllegalAccessException,
TIllegalArgumentException, TInvocationTargetException {
if ((modifiers & Flags.ABSTRACT) != 0) {
throw new TInstantiationException();
}
if (callable == null) {
throw new TIllegalAccessException();
}
if (initargs.length != parameterTypes.length) {
throw new TIllegalArgumentException();
}
for (int i = 0; i < initargs.length; ++i) {
if (initargs[i] != null && !parameterTypes[i].isInstance((TObject) initargs[i])) {
throw new TIllegalArgumentException();
}
if (parameterTypes[i].isPrimitive() && initargs[i] == null) {
throw new TIllegalArgumentException();
}
}
PlatformSequence<PlatformObject> jsArgs = Converter.arrayFromJava(initargs);
PlatformObject instance = callable.call(null, jsArgs);
return (T) Converter.toJava(instance);
}
public boolean isVarArgs() {
return (modifiers & Flags.VARARGS) != 0;
}
}

View File

@ -23,8 +23,8 @@ import org.teavm.classlib.java.lang.TClass;
import org.teavm.classlib.java.lang.TIllegalAccessException; import org.teavm.classlib.java.lang.TIllegalAccessException;
import org.teavm.classlib.java.lang.TIllegalArgumentException; import org.teavm.classlib.java.lang.TIllegalArgumentException;
import org.teavm.classlib.java.lang.TObject; import org.teavm.classlib.java.lang.TObject;
import org.teavm.jso.JSObject;
import org.teavm.platform.Platform; import org.teavm.platform.Platform;
import org.teavm.platform.PlatformObject;
public class TField extends TAccessibleObject implements TMember { public class TField extends TAccessibleObject implements TMember {
private TClass<?> declaringClass; private TClass<?> declaringClass;
@ -90,7 +90,7 @@ public class TField extends TAccessibleObject implements TMember {
throw new TIllegalAccessException(); throw new TIllegalAccessException();
} }
checkInstance(obj); checkInstance(obj);
JSObject result = getter.get(Platform.getPlatformObject(obj)); PlatformObject result = getter.get(Platform.getPlatformObject(obj));
return Converter.toJava(result); return Converter.toJava(result);
} }

View File

@ -0,0 +1,37 @@
/*
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.java.lang.reflect;
import org.teavm.classlib.java.lang.TReflectiveOperationException;
import org.teavm.classlib.java.lang.TString;
import org.teavm.classlib.java.lang.TThrowable;
public class TInvocationTargetException extends TReflectiveOperationException {
protected TInvocationTargetException() {
}
public TInvocationTargetException(TThrowable target) {
super(null, target);
}
public TInvocationTargetException(TThrowable target, TString s) {
super(s, target);
}
public TThrowable getTargetException() {
return getCause();
}
}

View File

@ -19,11 +19,6 @@ import org.teavm.jso.JSIndexer;
import org.teavm.jso.JSObject; import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty; import org.teavm.jso.JSProperty;
/**
*
* @author Alexey Andreev
* @param <T>
*/
public interface PlatformSequence<T extends JSObject> extends JSObject { public interface PlatformSequence<T extends JSObject> extends JSObject {
@JSProperty @JSProperty
int getLength(); int getLength();

View File

@ -22,6 +22,8 @@ import org.teavm.classlib.ReflectionContext;
import org.teavm.classlib.ReflectionSupplier; import org.teavm.classlib.ReflectionSupplier;
import org.teavm.model.ClassReader; import org.teavm.model.ClassReader;
import org.teavm.model.FieldReader; import org.teavm.model.FieldReader;
import org.teavm.model.MethodDescriptor;
import org.teavm.model.MethodReader;
public class ReflectionSupplierImpl implements ReflectionSupplier { public class ReflectionSupplierImpl implements ReflectionSupplier {
@Override @Override
@ -35,4 +37,16 @@ public class ReflectionSupplierImpl implements ReflectionSupplier {
} }
return fields; return fields;
} }
@Override
public Collection<MethodDescriptor> getAccessibleMethods(ReflectionContext context, String className) {
ClassReader cls = context.getClassSource().get(className);
Set<MethodDescriptor> methods = new HashSet<>();
for (MethodReader method : cls.getMethods()) {
if (method.getAnnotations().get(Reflectable.class.getName()) != null) {
methods.add(method.getDescriptor());
}
}
return methods;
}
} }