Skip to content

Commit f7f6b07

Browse files
committed
addition of a few java.lang.Class methods
1 parent c2aefcd commit f7f6b07

File tree

1 file changed

+85
-91
lines changed
  • sources/net.sf.j2s.java.core/src/java/lang

1 file changed

+85
-91
lines changed

sources/net.sf.j2s.java.core/src/java/lang/Class.java

Lines changed: 85 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import java.io.IOException;
3030
import java.io.InputStream;
31+
import java.lang.annotation.Annotation;
3132
import java.lang.reflect.Constructor;
3233
import java.lang.reflect.Field;
3334
import java.lang.reflect.GenericSignatureFormatError;
@@ -41,25 +42,14 @@
4142
import java.util.Enumeration;
4243
import java.util.HashMap;
4344
import java.util.Map;
45+
import java.util.Objects;
4446

4547
import javax.xml.bind.annotation.XmlRootElement;
4648

4749
import sun.misc.CompoundEnumeration;
50+
import sun.reflect.annotation.AnnotationType;
4851
import swingjs.JSUtil;
4952

50-
//import sun.misc.Unsafe;
51-
//import sun.reflect.ConstantPool;
52-
//import sun.reflect.Reflection;
53-
//import sun.reflect.ReflectionFactory;
54-
//import sun.reflect.annotation.AnnotationType;
55-
//import sun.reflect.generics.factory.CoreReflectionFactory;
56-
//import sun.reflect.generics.factory.GenericsFactory;
57-
//import sun.reflect.generics.repository.ClassRepository;
58-
//import sun.reflect.generics.repository.ConstructorRepository;
59-
//import sun.reflect.generics.repository.MethodRepository;
60-
//import sun.reflect.generics.scope.ClassScope;
61-
//import sun.security.util.SecurityConstants;
62-
6353
/**
6454
* Instances of the class {@code Class} represent classes and interfaces in a
6555
* running Java application. An enum is a kind of class and an annotation is a
@@ -3431,7 +3421,6 @@ Map<String, T> enumConstantDirectory() {
34313421
*
34323422
* @since 1.5
34333423
*/
3434-
@SuppressWarnings("unchecked")
34353424
public T cast(Object obj) {
34363425
if (obj != null && !isInstance(obj))
34373426
throw new ClassCastException(cannotCastMsg(obj));
@@ -3442,76 +3431,87 @@ private String cannotCastMsg(Object obj) {
34423431
return "Cannot cast " + obj.getClass().getName() + " to " + getName();
34433432
}
34443433

3445-
// /**
3446-
// * Casts this {@code Class} object to represent a subclass of the class
3447-
// * represented by the specified class object. Checks that that the cast is
3448-
// * valid, and throws a {@code ClassCastException} if it is not. If this
3449-
// * method succeeds, it always returns a reference to this class object.
3450-
// *
3451-
// * <p>
3452-
// * This method is useful when a client needs to "narrow" the type of a
3453-
// * {@code Class} object to pass it to an API that restricts the
3454-
// * {@code Class} objects that it is willing to accept. A cast would generate
3455-
// * a compile-time warning, as the correctness of the cast could not be
3456-
// * checked at runtime (because generic types are implemented by erasure).
3457-
// *
3458-
// * @return this {@code Class} object, cast to represent a subclass of the
3459-
// * specified class object.
3460-
// * @throws ClassCastException
3461-
// * if this {@code Class} object does not represent a subclass of
3462-
// * the specified class (here "subclass" includes the class
3463-
// * itself).
3464-
// * @since 1.5
3465-
// */
3466-
// public <U> Class<? extends U> asSubclass(Class<U> clazz) {
3467-
// if (clazz.isAssignableFrom(this))
3468-
// return (Class<? extends U>) this;
3469-
// else
3470-
// throw new ClassCastException(this.toString());
3471-
// }
3434+
/**
3435+
* Casts this {@code Class} object to represent a subclass of the class
3436+
* represented by the specified class object. Checks that that the cast is
3437+
* valid, and throws a {@code ClassCastException} if it is not. If this
3438+
* method succeeds, it always returns a reference to this class object.
3439+
*
3440+
* <p>
3441+
* This method is useful when a client needs to "narrow" the type of a
3442+
* {@code Class} object to pass it to an API that restricts the
3443+
* {@code Class} objects that it is willing to accept. A cast would generate
3444+
* a compile-time warning, as the correctness of the cast could not be
3445+
* checked at runtime (because generic types are implemented by erasure).
3446+
*
3447+
* @return this {@code Class} object, cast to represent a subclass of the
3448+
* specified class object.
3449+
* @throws ClassCastException
3450+
* if this {@code Class} object does not represent a subclass of
3451+
* the specified class (here "subclass" includes the class
3452+
* itself).
3453+
* @since 1.5
3454+
*/
3455+
public <U> Class<? extends U> asSubclass(Class<U> clazz) {
3456+
if (clazz.isAssignableFrom(this))
3457+
return (Class<? extends U>) this;
3458+
else
3459+
throw new ClassCastException(this.toString());
3460+
}
3461+
3462+
/**
3463+
* @throws NullPointerException {@inheritDoc}
3464+
* @since 1.5
3465+
*/
3466+
public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
3467+
Objects.requireNonNull(annotationClass);
3468+
JSUtil.notImplemented(null);
3469+
return null;
3470+
}
34723471

3473-
// /**
3474-
// * @throws NullPointerException
3475-
// * {@inheritDoc}
3476-
// * @since 1.5
3477-
// */
3478-
// public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
3479-
// if (annotationClass == null)
3480-
// throw new NullPointerException();
3481-
//
3482-
// initAnnotationsIfNecessary();
3483-
// return (A) annotations.get(annotationClass);
3472+
// public XmlRootElement getAnnotation(Class<XmlRootElement> c) {
3473+
//
3474+
// // TODO Auto-generated method stub
3475+
// return null;
34843476
// }
34853477
//
3486-
// /**
3487-
// * @throws NullPointerException
3488-
// * {@inheritDoc}
3489-
// * @since 1.5
3490-
// */
3491-
// public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
3492-
// if (annotationClass == null)
3493-
// throw new NullPointerException();
3494-
//
3478+
/**
3479+
* @throws NullPointerException
3480+
* {@inheritDoc}
3481+
* @since 1.5
3482+
*/
3483+
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
3484+
if (annotationClass == null)
3485+
throw new NullPointerException();
3486+
JSUtil.notImplemented(null);
3487+
return false;
34953488
// return getAnnotation(annotationClass) != null;
3496-
// }
3497-
//
3498-
// private static Annotation[] EMPTY_ANNOTATIONS_ARRAY = new Annotation[0];
3499-
//
3500-
// /**
3501-
// * @since 1.5
3502-
// */
3503-
// public Annotation[] getAnnotations() {
3489+
}
3490+
3491+
private static Annotation[] EMPTY_ANNOTATIONS_ARRAY;
3492+
3493+
private static Annotation[] getEmptyAnnotations() {
3494+
return (EMPTY_ANNOTATIONS_ARRAY == null ? EMPTY_ANNOTATIONS_ARRAY = new Annotation[0] : EMPTY_ANNOTATIONS_ARRAY);
3495+
}
3496+
/**
3497+
* @since 1.5
3498+
*/
3499+
public Annotation[] getAnnotations() {
3500+
JSUtil.notImplemented(null);
3501+
return getEmptyAnnotations();
35043502
// initAnnotationsIfNecessary();
35053503
// return annotations.values().toArray(EMPTY_ANNOTATIONS_ARRAY);
3506-
// }
3507-
//
3508-
// /**
3509-
// * @since 1.5
3510-
// */
3511-
// public Annotation[] getDeclaredAnnotations() {
3504+
}
3505+
3506+
/**
3507+
* @since 1.5
3508+
*/
3509+
public Annotation[] getDeclaredAnnotations() {
3510+
JSUtil.notImplemented(null);
3511+
return getEmptyAnnotations();
35123512
// initAnnotationsIfNecessary();
35133513
// return declaredAnnotations.values().toArray(EMPTY_ANNOTATIONS_ARRAY);
3514-
// }
3514+
}
35153515

35163516
// // Annotations cache
35173517
// private transient Map<Class, Annotation> annotations;
@@ -3539,15 +3539,15 @@ private String cannotCastMsg(Object obj) {
35393539
//
35403540
// // Annotation types cache their internal (AnnotationType) form
35413541
//
3542-
// private AnnotationType annotationType;
3543-
//
3544-
// void setAnnotationType(AnnotationType type) {
3545-
// annotationType = type;
3546-
// }
3547-
//
3548-
// AnnotationType getAnnotationType() {
3549-
// return annotationType;
3550-
// }
3542+
private AnnotationType annotationType;
3543+
3544+
void setAnnotationType(AnnotationType type) {
3545+
annotationType = type;
3546+
}
3547+
3548+
AnnotationType getAnnotationType() {
3549+
return annotationType;
3550+
}
35513551

35523552
@SuppressWarnings("null")
35533553
@Override
@@ -3592,12 +3592,6 @@ public Object getPackage() {
35923592
return null;
35933593
}
35943594

3595-
public XmlRootElement getAnnotation(Class<XmlRootElement> c) {
3596-
3597-
// TODO Auto-generated method stub
3598-
return null;
3599-
}
3600-
36013595
public static Class<?> getJ2SSuperclassFor(Class<?> cl) {
36023596
Class<?> c = null;
36033597
/**

0 commit comments

Comments
 (0)