Skip to content

Commit fecacf1

Browse files
committed
Refactor reflection support
1 parent adab852 commit fecacf1

11 files changed

Lines changed: 237 additions & 257 deletions

File tree

classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java

Lines changed: 94 additions & 245 deletions
Large diffs are not rendered by default.

classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,7 @@ private void init() {
231231
}
232232

233233
@Rename("getClass")
234-
public final TClass<?> getClass0() {
235-
return TClass.getClass(Platform.getPlatformObject(this).getPlatformClass());
236-
}
234+
public final native TClass<?> getClass0();
237235

238236
@Override
239237
public int hashCode() {

classlib/src/main/java/org/teavm/classlib/java/lang/reflect/TArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static TObject newInstance(Class<?> componentType, int length) throws TNe
6363
if (PlatformDetector.isWebAssemblyGC()) {
6464
return newInstanceImpl(componentType, length);
6565
} else {
66-
return newInstanceImpl(((TClass<?>) (Object) componentType).getPlatformClass(), length);
66+
return newInstanceImpl(((TClass<?>) (Object) componentType).getClassInfo(), length);
6767
}
6868
}
6969

classlib/src/main/java/org/teavm/classlib/java/lang/reflect/TMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public Object invoke(Object obj, Object... args) throws TIllegalAccessException,
102102
throw new TIllegalArgumentException();
103103
}
104104
} else if (PlatformDetector.isJavaScript()) {
105-
Platform.initClass(declaringClass.getPlatformClass());
105+
Platform.initClass(declaringClass.getClassInfo());
106106
}
107107

108108
for (int i = 0; i < args.length; ++i) {

classlib/src/main/java/org/teavm/classlib/java/util/TGenericEnumSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static Enum<?>[] getConstants(Class<?> cls) {
4848
if (PlatformDetector.isWebAssemblyGC()) {
4949
return ClassSupport.getEnumConstants(cls);
5050
} else {
51-
PlatformClass platformClass = ((TClass<?>) (Object) cls).getPlatformClass();
51+
PlatformClass platformClass = ((TClass<?>) (Object) cls).getClassInfo();
5252
Platform.initClass(platformClass);
5353
return Platform.getEnumConstants(platformClass);
5454
}

classlib/src/main/java/org/teavm/classlib/java/util/TServiceLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public TIterator<S> iterator() {
4848
public static <S> TServiceLoader<S> load(TClass<S> service) {
4949
return new TServiceLoader<>(PlatformDetector.isWebAssemblyGC()
5050
? doLoadServices(service)
51-
: doLoadServices(service.getPlatformClass()));
51+
: doLoadServices(service.getClassInfo()));
5252
}
5353

5454
public static <S> TServiceLoader<S> load(TClass<S> service, @SuppressWarnings("unused") TClassLoader loader) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2026 Alexey Andreev.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.teavm.runtime;
17+
18+
public class ObjectInfo {
19+
public native Object getObject();
20+
}

core/src/main/java/org/teavm/runtime/RuntimeClass.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public class RuntimeClass extends RuntimeObject {
6565
public int tag;
6666
public int canary;
6767
public RuntimeObjectPtr name;
68-
public RuntimeObject nameCache;
68+
public RuntimeObjectPtr simpleName;
69+
6970
public RuntimeClass itemType;
7071
public RuntimeClass arrayType;
7172
public RuntimeClass declaringClass;
@@ -77,10 +78,7 @@ public class RuntimeClass extends RuntimeObject {
7778
public RuntimeClassPointer superinterfaces;
7879
public Address enumValues;
7980
public Address layout;
80-
public RuntimeObjectPtr simpleName;
81-
public RuntimeObject simpleNameCache;
82-
public RuntimeObject canonicalName;
83-
public RuntimeObject reflectionState;
81+
public RuntimeObject classObject;
8482

8583
@Unmanaged
8684
public static int computeCanary(int size, int tag) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2026 Alexey Andreev.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.teavm.runtime;
17+
18+
public class StringInfo {
19+
public native String getStringObject();
20+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2026 Alexey Andreev.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.teavm.runtime.reflect;
17+
18+
import org.teavm.runtime.StringInfo;
19+
20+
public class ClassInfo {
21+
public static final int ABSTRACT = 1;
22+
public static final int INTERFACE = 1 << 1;
23+
public static final int FINAL = 1 << 2;
24+
public static final int ENUM = 1 << 3;
25+
public static final int ANNOTATION = 1 << 4;
26+
public static final int SYNTHETIC = 1 << 5;
27+
public static final int BRIDGE = 1 << 6;
28+
public static final int DEPRECATED = 1 << 7;
29+
public static final int NATIVE = 1 << 8;
30+
public static final int STATIC = 1 << 9;
31+
public static final int STRICT = 1 << 10;
32+
public static final int SYNCHRONIZED = 1 << 11;
33+
public static final int TRANSIENT = 1 << 12;
34+
public static final int VARARGS = 1 << 13;
35+
public static final int VOLATILE = 1 << 14;
36+
public static final int INHERITED_ANNOTATION = 1 << 15;
37+
38+
public static final int NOT_PRIMITIVE = 0;
39+
public static final int BOOLEAN_PRIMITIVE = 1;
40+
public static final int BYTE_PRIMITIVE = 2;
41+
public static final int SHORT_PRIMITIVE = 3;
42+
public static final int CHAR_PRIMITIVE = 4;
43+
public static final int INT_PRIMITIVE = 5;
44+
public static final int LONG_PRIMITIVE = 6;
45+
public static final int FLOAT_PRIMITIVE = 7;
46+
public static final int DOUBLE_PRIMITIVE = 8;
47+
public static final int VOID_PRIMITIVE = 9;
48+
49+
public native int modifiers();
50+
51+
public native int primitiveKind();
52+
53+
public native StringInfo name();
54+
55+
public native StringInfo simpleName();
56+
57+
public native ClassInfo itemType();
58+
59+
public native ClassInfo arrayType();
60+
61+
public native ClassInfo declaringClass();
62+
63+
public native ClassInfo enclosingClass();
64+
65+
public native boolean isSuperTypeOf(ClassInfo subtype);
66+
67+
public native void init();
68+
69+
public native ClassInfo parent();
70+
71+
public native int superinterfaceCount();
72+
73+
public native ClassInfo superinterface(int index);
74+
75+
public native Class<?> classObject();
76+
}

0 commit comments

Comments
 (0)