java.lang.Class Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jtransc-rt Show documentation
Show all versions of jtransc-rt Show documentation
JVM AOT compiler currently generating JavaScript, C++, Haxe, with initial focus on Kotlin and games.
/*
* Copyright 2016 Carlos Ballesteros Velasco
*
* 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 java.lang;
import com.jtransc.ds.FastStringMap;
import com.jtransc.JTranscSystem;
import com.jtransc.annotation.JTranscKeep;
import com.jtransc.annotation.JTranscMethodBody;
import com.jtransc.annotation.haxe.HaxeAddMembers;
import com.jtransc.annotation.haxe.HaxeMethodBody;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
@HaxeAddMembers({
"public var _hxClass:Class = null;",
"public var _hxProxyClass:Class = null;",
"public var _hxFfiClass:Class = null;",
"public var _internalName = '';",
"public var _parent:String = null;",
"public var _interfaces:Array = [];",
"public var _fields = [];",
"public var _modifiers = 0;",
"public var _methods:Array<{% CLASS java.lang.reflect.Method %}> = [];",
"public var _constructors = [];",
"public var _annotations = [];",
"public var _methodsById = null;",
"" +
"public function populateMethodsById() {\n" +
" if (_methodsById != null) return;\n" +
" _methodsById = new Map();\n" +
" function populate(clazz:{% CLASS java.lang.Class %}) {\n" +
" for (m in clazz._methods) _methodsById.set(m.{% FIELD java.lang.reflect.Method:id %}, m);\n" +
" if (clazz._parent != null) populate(HaxeNatives.resolveClass(clazz._parent));\n" +
" for (i in clazz._interfaces) populate(HaxeNatives.resolveClass(i));\n" +
" }\n" +
" populate(this);\n" +
"}\n",
"public function locateMethodById(id:Int) { populateMethodsById(); return _methodsById.get(id); }",
})
public final class Class implements java.io.Serializable, Type, GenericDeclaration, AnnotatedElement {
@JTranscKeep
private String name;
@JTranscKeep
private boolean primitive = false;
@JTranscKeep
@SuppressWarnings("unused")
private int modifiers;
private static final int ANNOTATION = 0x00002000;
private static final int ENUM = 0x00004000;
private static final int SYNTHETIC = 0x00001000;
// Returns the Class representing the superclass of the entity (class, interface, primitive type or void) represented by this Class. If this Class represents either the Object class, an interface, a primitive type, or void, then null is returned. If this object represents an array class then the Class object representing the Object class is returned.
// Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private fields, but excludes inherited fields. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if the class or interface declares no fields, or if this Class object represents a primitive type, an array class, or void.
// Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private fields, but excludes inherited fields. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if the class or interface declares no fields, or if this Class object represents a primitive type, an array class, or void.
@HaxeMethodBody("return HaxeArrayAny.fromArray(_fields, '[Ljava.lang.reflect.Field;');")
@JTranscMethodBody(target = "js", value = "return JA_L.fromArrayOrEmpty(this._fields, '[Ljava.lang.reflect.Field;');")
native public Field[] getDeclaredFields() throws SecurityException;
@HaxeMethodBody("return HaxeArrayAny.fromArray(_methods, '[Ljava.lang.reflect.Method;');")
@JTranscMethodBody(target = "js", value = "return JA_L.fromArrayOrEmpty(this._methods, '[Ljava.lang.reflect.Method;');")
native public Method[] getDeclaredMethods() throws SecurityException;
public Constructor[] getDeclaredConstructors() throws SecurityException {
Constructor[] constructors = _getDeclaredConstructors();
return (constructors != null) ? constructors : new Constructor[0];
}
@HaxeMethodBody("return HaxeArrayAny.fromArray(_constructors, '[Ljava.lang.reflect.Constructor;');")
@JTranscMethodBody(target = "js", value = "return JA_L.fromArrayOrEmpty(this._constructors, '[Ljava.lang.reflect.Constructor;');")
native private Constructor[] _getDeclaredConstructors() throws SecurityException;
@HaxeMethodBody("return HaxeArrayAny.fromArray(_annotations, '[Ljava.lang.Annotation;');")
@JTranscMethodBody(target = "js", value = "return JA_L.fromArrayOrEmpty(this._annotations, '[Ljava.lang.Annotation;');")
native public Annotation[] getDeclaredAnnotations();
@HaxeMethodBody("return (_parent != null) ? HaxeNatives.resolveClass(_parent) : null;")
public Class getSuperclass() {
return (Class) forName0(getSuperclassName());
}
@JTranscMethodBody(target = "js", value = "return N.str(this._superclass);")
native private String getSuperclassName();
public Class[] getInterfaces() {
String[] names = getInterfaceNames();
Class[] out = new Class[names.length];
for (int n = 0; n < out.length; n++) out[n] = forName0(names[n]);
return out;
}
@HaxeMethodBody("return HaxeNatives.strArray(_interfaces);")
@JTranscMethodBody(target = "js", value = "return N.strArrayOrEmpty(this._interfaces);")
native private String[] getInterfaceNames();
@HaxeMethodBody("return _modifiers;")
public int getModifiers() {
return modifiers;
}
//@HaxeMethodBody("return HaxeNatives.newInstance(this._internalName);")
//native public T newInstance() throws InstantiationException, IllegalAccessException;
public T newInstance() throws InstantiationException, IllegalAccessException {
try {
Constructor constructor = getDeclaredConstructor();
return constructor.newInstance();
} catch (NoSuchMethodException e) {
throw new InstantiationException(e.getMessage());
} catch (InvocationTargetException e) {
throw new InstantiationException(e.getMessage());
}
}
native public Class[] getDeclaredClasses() throws SecurityException;
native public Method getEnclosingMethod();
native public Constructor getEnclosingConstructor() throws SecurityException;
native public java.net.URL getResource(String name);
@HaxeMethodBody("return Std.is(p0, _hxClass);")
@JTranscMethodBody(target = "js", value = "return N.isInstanceOfClass(p0, this);")
public native boolean isInstance(Object obj);
native public InputStream getResourceAsStream(String name);
public ClassLoader getClassLoader() {
return _ClassInternalUtils.getSystemClassLoader();
}
native public TypeVariable>[] getTypeParameters();
native public Type getGenericSuperclass();
native public Package getPackage();
native public Type[] getGenericInterfaces();
public Class getComponentType() {
if (isArray()) {
try {
return forName(getName().substring(1));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
} else {
return null;
}
}
public native Object[] getSigners();
native public Class getDeclaringClass() throws SecurityException;
native public Class getEnclosingClass() throws SecurityException;
native public Class[] getClasses();
//native public java.security.ProtectionDomain getProtectionDomain();
@JTranscKeep
private Class() {
}
@JTranscKeep
Class(String name) throws ClassNotFoundException {
this.name = name;
this.primitive = false;
if (!_check()) throw new ClassNotFoundException("Can't find class " + name);
}
@JTranscKeep
Class(String name, boolean primitive) {
this.name = name;
this.primitive = primitive;
}
@JTranscKeep
@HaxeMethodBody("return R.__initClass(this);")
@JTranscMethodBody(target = "js", value = "return R.__initClass(this);")
native private boolean _check();
@JTranscKeep
public String getName() {
return this.name;
}
@JTranscKeep
static Class getPrimitiveClass(String name) {
return new Class