| Method from java.lang.Class Detail: |
public Class<? extends U> asSubclass(Class<U> clazz) throws ClassCastException {
if (!VMClassRegistry.isAssignableFrom(clazz, this)) {
throw new ClassCastException(toString());
}
return (Class< ? extends U >)this;
}
|
public T cast(Object obj) throws ClassCastException {
if (obj != null && !VMClassRegistry.isInstance(this, obj)) {
throw new ClassCastException(obj.getClass().toString());
}
return (T) obj;
}
|
public boolean desiredAssertionStatus() {
if (disableAssertions) {
return false;
}
ClassLoader loader = getClassLoaderImpl();
if (loader == null) {
// system class, status is controlled via cmdline only
return VMExecutionEngine.getAssertionStatus(this, true, 0) > 0;
}
// First check exact class name
String name = null;
Map< String, Boolean > m = loader.classAssertionStatus;
if (m != null && m.size() != 0)
{
name = getTopLevelClassName();
Boolean status = m.get(name);
if (status != null) {
return status.booleanValue();
}
}
if (!loader.clearAssertionStatus) {
int systemStatus = VMExecutionEngine.getAssertionStatus(this, false, 0);
if (systemStatus != 0) {
return systemStatus > 0;
}
}
// Next try (super)packages name(s) recursively
m = loader.packageAssertionStatus;
if (m != null && m.size() != 0) {
if (name == null) {
name = getName();
}
name = getParentName(name);
// if this class is in the default package,
// it is checked in the 1st iteration
do {
Boolean status = m.get(name);
if (status != null) {
return status.booleanValue();
}
} while ( (name = getParentName(name)).length() > 0);
}
if (!loader.clearAssertionStatus) {
int systemStatus = VMExecutionEngine.getAssertionStatus(this, true,
loader.defaultAssertionStatus);
if (systemStatus != 0) {
return systemStatus > 0;
}
}
// Finally check the default status
return loader.defaultAssertionStatus > 0;
}
|
public static Class<?> forName(String name) throws ClassNotFoundException {
return forName(name, true, VMClassRegistry.getClassLoader(VMStack
.getCallerClass(0)));
}
|
public static Class<?> forName(String name,
boolean initialize,
ClassLoader classLoader) throws ClassNotFoundException {
if (name == null) {
throw new NullPointerException();
}
if(name.indexOf("/") != -1) {
throw new ClassNotFoundException(name);
}
Class< ? > clazz = null;
if (classLoader == null) {
SecurityManager sc = System.getSecurityManager();
if (sc != null &&
VMClassRegistry.getClassLoader(VMStack.getCallerClass(0)) != null) {
sc.checkPermission(RuntimePermissionCollection.GET_CLASS_LOADER_PERMISSION);
}
clazz = VMClassRegistry.loadBootstrapClass(name);
} else {
int dims = 0;
int len = name.length();
while (dims < len && name.charAt(dims) == '[') dims++;
if (dims > 0 && len > dims + 1
&& name.charAt(dims) == 'L' && name.endsWith(";")) {
/*
* an array of a reference type is requested.
* do not care of arrays of primitives as
* they are perfectly loaded by bootstrap classloader.
*/
try {
clazz = classLoader.loadClass(name.substring(dims + 1, len - 1));
} catch (ClassNotFoundException ignore) {}
if (clazz != null ) {
clazz = VMClassRegistry.loadArray(clazz, dims);
}
} else {
clazz = classLoader.loadClass(name);
}
}
if(clazz == null) {
throw new ClassNotFoundException(name);
}
if(classLoader != null) {
/*
* Although class loader may have had a chance to register itself as
* initiating for requested class, there may occur a classloader
* which overloads loadClass method (though it is not recommended by
* J2SE specification). Try to register initiating loader for clazz
* from here again
*/
classLoader.registerInitiatedClass(clazz);
}
if (initialize) {
VMClassRegistry.initializeClass(clazz);
} else {
VMClassRegistry.linkClass(clazz);
}
return clazz;
}
|
public A getAnnotation(Class<A> annotationClass) {
if(annotationClass == null) {
throw new NullPointerException();
}
for (Annotation aa : getCache().getAllAnnotations()) {
if(annotationClass == aa.annotationType()) {
return (A)aa;
}
}
return null;
}
|
public Annotation[] getAnnotations() {
Annotation[] all = getCache().getAllAnnotations();
Annotation aa[] = new Annotation[all.length];
System.arraycopy(all, 0, aa, 0, all.length);
return aa;
}
|
public String getCanonicalName() {
if (isLocalClass() || isAnonymousClass()) {
return null;
}
if (isArray()) {
String res = getComponentType().getCanonicalName();
return res != null ? res + "[]" : null;
}
StringBuffer sb = new StringBuffer(getPackageName());
ArrayList< String > sympleNames = new ArrayList< String >();
Class< ? > clss = this;
while ((clss = clss.getDeclaringClass()) != null) {
if (clss.isLocalClass() || clss.isAnonymousClass()) {
return null;
}
sympleNames.add(clss.getSimpleName());
}
if (sb.length() > 0) {
sb.append(".");
}
for (int i = sympleNames.size() - 1; i > -1 ; i--) {
sb.append(sympleNames.get(i)).append(".");
}
sb.append(getSimpleName());
return sb.toString();
}
|
public ClassLoader getClassLoader() {
ClassLoader loader = getClassLoaderImpl();
SecurityManager sc = System.getSecurityManager();
if (sc != null) {
ClassLoader callerLoader = VMClassRegistry.getClassLoader(VMStack
.getCallerClass(0));
if (callerLoader != null && !callerLoader.isSameOrAncestor(loader)) {
sc.checkPermission(RuntimePermissionCollection.GET_CLASS_LOADER_PERMISSION);
}
}
return loader;
}
|
final ClassLoader getClassLoaderImpl() {
assert(VMClassRegistry.getClassLoader0(this) == definingLoader);
return definingLoader;
}
|
public Class[] getClasses() {
checkMemberAccess(Member.PUBLIC);
Class< ? > clss = this;
ArrayList< Class< ? > > classes = null;
while (clss != null) {
Class< ? >[] declared = VMClassRegistry.getDeclaredClasses(clss);
if (declared.length != 0) {
if (classes == null) {
classes = new ArrayList< Class< ? > >();
}
for (Class< ? > c : declared) {
if (Modifier.isPublic(c.getModifiers())) {
classes.add(c);
}
}
}
clss = clss.getSuperclass();
}
if (classes == null) {
return new Class[0];
} else {
return classes.toArray(new Class[classes.size()]);
}
}
Note: We don't check member access permission for each super class.
Java 1.5 API specification doesn't require this check. |
public Class<?> getComponentType() {
if (!isArray()) {
return null;
}
return VMClassRegistry.getComponentType(this);
}
|
public Constructor<T> getConstructor(Class argumentTypes) throws NoSuchMethodException {
checkMemberAccess(Member.PUBLIC);
Constructor< T > ctors[] = getReflectionData().getPublicConstructors();
for (int i = 0; i < ctors.length; i++) {
Constructor< T > c = ctors[i];
try {
if (isTypeMatches(argumentTypes, c.getParameterTypes())) {
return Reflection.copyConstructor(c);
}
} catch (LinkageError ignore) {}
}
throw new NoSuchMethodException(getName()
+ printMethodSignature(argumentTypes));
}
|
public Constructor[] getConstructors() {
checkMemberAccess(Member.PUBLIC);
return Reflection.copyConstructors(getReflectionData().getPublicConstructors());
}
|
public Annotation[] getDeclaredAnnotations() {
Annotation[] declared = getCache().getDeclaredAnnotations();
Annotation aa[] = new Annotation[declared.length];
System.arraycopy(declared, 0, aa, 0, declared.length);
return aa;
}
|
public Class[] getDeclaredClasses() {
checkMemberAccess(Member.DECLARED);
return VMClassRegistry.getDeclaredClasses(this);
}
|
public Constructor<T> getDeclaredConstructor(Class argumentTypes) throws NoSuchMethodException {
checkMemberAccess(Member.DECLARED);
return Reflection
.copyConstructor(getDeclaredConstructorInternal(argumentTypes));
}
|
public Constructor[] getDeclaredConstructors() {
checkMemberAccess(Member.DECLARED);
return Reflection.copyConstructors(getReflectionData().getDeclaredConstructors());
}
|
public Field getDeclaredField(String fieldName) throws NoSuchFieldException {
checkMemberAccess(Member.DECLARED);
final Field[] declaredFields = getReflectionData().getDeclaredFields();
for (int i = 0; i < declaredFields.length; i++) {
Field f = declaredFields[i];
if (fieldName.equals(f.getName())) {
return Reflection.copyField(f);
}
}
throw new NoSuchFieldException(fieldName.toString());
}
|
public Field[] getDeclaredFields() {
checkMemberAccess(Member.DECLARED);
return Reflection.copyFields(getReflectionData().getDeclaredFields());
}
|
public Method getDeclaredMethod(String methodName,
Class argumentTypes) throws NoSuchMethodException {
checkMemberAccess(Member.DECLARED);
return Reflection
.copyMethod(findMatchingMethod(getReflectionData().getDeclaredMethods(),
methodName, argumentTypes));
}
|
public Method[] getDeclaredMethods() {
checkMemberAccess(Member.DECLARED);
return Reflection.copyMethods(getReflectionData().getDeclaredMethods());
}
|
public Class<?> getDeclaringClass() {
return VMClassRegistry.getDeclaringClass(this);
}
|
public Class<?> getEnclosingClass() {
return VMClassRegistry.getEnclosingClass(this); // see VMClassRegistry.getEnclosingClass() spec
}
|
public Constructor<?> getEnclosingConstructor() {
Member m = VMClassRegistry.getEnclosingMember(this); // see VMClassRegistry.getEnclosingMember() spec
return m instanceof Constructor ? (Constructor< ? >)m : null;
}
|
public Method getEnclosingMethod() {
Member m = VMClassRegistry.getEnclosingMember(this); // see VMClassRegistry.getEnclosingMember() spec
return m instanceof Method? (Method)m : null;
}
|
public T[] getEnumConstants() {
if (isEnum()) {
try {
final Method values = getMethod("values");
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
values.setAccessible(true);
return null;
}
});
return (T[]) values.invoke(null);
} catch (Exception ignore) {}
}
return null;
}
|
public Field getField(String fieldName) throws NoSuchFieldException {
checkMemberAccess(Member.PUBLIC);
final Field[] fields = getReflectionData().getPublicFields();
for (Field f : fields) {
if (fieldName.equals(f.getName())) {
return Reflection.copyField(f);
}
}
throw new NoSuchFieldException(fieldName.toString());
}
|
public Field[] getFields() {
checkMemberAccess(Member.PUBLIC);
return Reflection.copyFields(getReflectionData().getPublicFields());
}
|
public Type[] getGenericInterfaces() throws GenericSignatureFormatError, TypeNotPresentException, MalformedParameterizedTypeException {
if (isArray()) {
return new Type[]{CLONEABLE_CLASS, SERIALIZABLE_CLASS};
}
if (isPrimitive()) {
return new Type[0];
}
return (Type[])getCache().getGenericInterfaces().clone();
}
|
public Type getGenericSuperclass() throws GenericSignatureFormatError, TypeNotPresentException, MalformedParameterizedTypeException {
String tmp;
if (isInterface() || ((tmp = getCanonicalName()) != null && tmp.equals("java.lang.Object")) || isPrimitive()) {
return null;
}
if (isArray()) {
return (Type) OBJECT_CLASS;
}
Class< ? > clazz = getSuperclass();
if (clazz.getTypeParameters().length == 0) {
return (Type) clazz;
}
return getCache().getGenericSuperclass();
}
|
public Class[] getInterfaces() {
return VMClassRegistry.getInterfaces(this);
}
|
public Method getMethod(String methodName,
Class argumentTypes) throws NoSuchMethodException {
checkMemberAccess(Member.PUBLIC);
return Reflection
.copyMethod(findMatchingMethod(getReflectionData().getPublicMethods(),
methodName, argumentTypes));
}
|
public Method[] getMethods() {
checkMemberAccess(Member.PUBLIC);
return Reflection.copyMethods(getReflectionData().getPublicMethods());
}
|
public int getModifiers() {
return getReflectionData().getModifiers();
}
|
public String getName() {
return getReflectionData().name;
}
|
public Package getPackage() {
ClassLoader classLoader = getClassLoaderImpl();
return classLoader == null
? ClassLoader.BootstrapLoader.getPackage(getPackageName())
: classLoader.getPackage(getPackageName());
}
|
String getPackageName() {
return getReflectionData().packageName;
}
|
public ProtectionDomain getProtectionDomain() {
SecurityManager sc = System.getSecurityManager();
if (sc != null) {
sc.checkPermission(
RuntimePermissionCollection.GET_PROTECTION_DOMAIN_PERMISSION);
}
if (domain == null) {
if (systemDomain == null) {
Permissions allPermissions = new Permissions();
allPermissions.add(new AllPermission());
systemDomain = new ProtectionDomain(null, allPermissions);
}
return systemDomain;
}
return domain;
}
|
public URL getResource(String resource) {
resource = getAbsoluteResource(resource);
ClassLoader classLoader = getClassLoaderImpl();
return classLoader == null
? ClassLoader.getSystemResource(resource)
: classLoader.getResource(resource);
}
|
public InputStream getResourceAsStream(String resource) {
resource = getAbsoluteResource(resource);
ClassLoader classLoader = getClassLoaderImpl();
return classLoader == null
? ClassLoader.getSystemResourceAsStream(resource)
: classLoader.getResourceAsStream(resource);
}
|
public Object[] getSigners() {
try {
Object[] signers = (Object[])getClassLoaderImpl().classSigners.get(getName());
return (Object[])signers.clone();
} catch (NullPointerException e) {
}
try {
return (Object[])domain.getCodeSource().getCertificates().clone();
} catch (NullPointerException e) {
}
return null;
}
|
public String getSimpleName() {
return VMClassRegistry.getSimpleName(this);
}
|
static final Class<?>[] getStackClasses(int maxDepth,
boolean stopAtPrivileged) {
return VMStack.getClasses(maxDepth, stopAtPrivileged);
}
|
public Class<? super T> getSuperclass() {
return VMClassRegistry.getSuperclass(this);
}
|
public TypeVariable<T>[] getTypeParameters() throws GenericSignatureFormatError {
return (TypeVariable< Class< T > >[])getCache().getTypeParameters().clone();
}
|
public boolean isAnnotation() {
return (getModifiers() & ACC_ANNOTATION) != 0;
}
|
public boolean isAnnotationPresent(Class<Annotation> annotationClass) {
if(annotationClass == null) {
throw new NullPointerException();
}
for (Annotation aa : getCache().getAllAnnotations()) {
if(annotationClass == aa.annotationType()) {
return true;
}
}
return false;
}
|
public boolean isAnonymousClass() {
return getSimpleName().length() == 0;
}
|
public boolean isArray() {
return getReflectionData().isArray;
}
|
public boolean isAssignableFrom(Class<?> clazz) {
if (SERIALIZABLE_CLASS.equals(this)) {
return clazz.getReflectionData().isSerializable();
}
if (EXTERNALIZABLE_CLASS.equals(this)) {
return clazz.getReflectionData().isExternalizable();
}
return VMClassRegistry.isAssignableFrom(this, clazz);
}
|
public boolean isEnum() {
// check for superclass is needed for compatibility
// otherwise there are false positives on anonymous element classes
return ((getModifiers() & ACC_ENUM) != 0 && getSuperclass() == ENUM_CLASS);
}
|
public boolean isInstance(Object obj) {
return VMClassRegistry.isInstance(this, obj);
}
|
public boolean isInterface() {
return (getModifiers() & ACC_INTERFACE) != 0;
}
|
public boolean isLocalClass() {
return VMClassRegistry.getEnclosingMember(this) != null && !isAnonymousClass(); // see CFF spec, #4.8.6, first paragraph and VMClassRegistry.getEnclosingMember() spec
}
|
public boolean isMemberClass() {
return getDeclaringClass() != null; // see Class.getDeclaringClass() spec
}
|
public boolean isPrimitive() {
return getReflectionData().isPrimitive;
}
|
public boolean isSynthetic() {
return (getModifiers() & ACC_SYNTHETIC) != 0;
}
|
static boolean isTypeMatches(Class<?>[] t1,
Class<?>[] t2) {
if (t1 == null) {
return t2 == null || t2.length == 0;
}
if (t2 == null) {
return t1 == null || t1.length == 0;
}
if (t1.length != t2.length) {
return false;
}
for (int i = 0; i < t2.length; i++) {
if (t1[i] != t2[i]) {
return false;
}
}
return true;
}
Answers whether the arrays are equal |
public T newInstance() throws InstantiationException, IllegalAccessException {
T newInstance = null;
final ReflectionData localReflectionData = getReflectionData();
SecurityManager sc = System.getSecurityManager();
if (sc != null) {
sc.checkMemberAccess(this, Member.PUBLIC);
sc.checkPackageAccess(localReflectionData.packageName);
}
/*
* HARMONY-1930: The synchronization issue is possible here.
*
* The issues is caused by fact that:
* - first thread starts defaultConstructor initialization, including
* setting "isAccessible" flag to "true" for Constrcutor object
* - another thread bypasses initialization and calls "newInstance"
* for defaultConstructor (while isAccessible is "false" yet)
* - so, for this "another" thread the Constructor.newInstance checks
* the access rights by mistake and IllegalAccessException happens
*/
while (!isDefaultConstructorInitialized) {
synchronized (localReflectionData) {
if (isDefaultConstructorInitialized) {
break; // non-first threads can be here - nothing to do
}
// only first thread can reach this point & do initialization
final Constructor< T > c;
try {
c = localReflectionData.getDefaultConstructor();
} catch (NoSuchMethodException e) {
throw new InstantiationException(e.getMessage()
+ " method not found");
}
try {
AccessController.doPrivileged(new PrivilegedAction< Object >() {
public Object run() {
c.setAccessible(true);
return null;
}
});
} catch (SecurityException e) {
// can't change accessibility of the default constructor
IllegalAccessException ex = new IllegalAccessException();
ex.initCause(e);
throw ex;
}
// default constructor is initialized, access flag is set
isDefaultConstructorInitialized = true;
break;
}
}
// initialization is done, threads may work from here in any order
final Constructor< T > defaultConstructor;
try {
defaultConstructor = localReflectionData.getDefaultConstructor();
} catch (NoSuchMethodException e){
throw new AssertionError(e);
}
Reflection.checkMemberAccess(
VMStack.getCallerClass(0),
defaultConstructor.getDeclaringClass(),
defaultConstructor.getDeclaringClass(),
defaultConstructor.getModifiers()
);
try {
newInstance = defaultConstructor.newInstance();
} catch (InvocationTargetException e) {
System.rethrow(e.getCause());
}
return newInstance;
}
|
void setProtectionDomain(ProtectionDomain protectionDomain) {
domain = protectionDomain;
}
|
public String toString() {
return isPrimitive() ? getName()
: (isInterface() ? "interface " : "class ") + getName();
}
|