-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support Class.isEnum() and Class.isAnnotation()
- Loading branch information
1 parent
ea5efd7
commit 8c53d55
Showing
12 changed files
with
149 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package samples.reflection.trivial.isannotationexample; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
public class IsAnnotationExample { | ||
public static void main(String[] args) { | ||
// Trivial Case: Simple annotation | ||
System.out.print("@MyAnnotation is annotation: "); | ||
System.out.println(MyAnnotation.class.isAnnotation()); // true | ||
|
||
// Case 1: Non-annotation class | ||
System.out.print("String.class is annotation: "); | ||
System.out.println(String.class.isAnnotation()); // false | ||
|
||
// Case 2: Annotation subclass (edge case) | ||
System.out.print("@InheritedAnnotation is annotation: "); | ||
System.out.println(InheritedAnnotation.class.isAnnotation()); // true | ||
System.out.print("@MyAnnotation subclass is annotation: "); | ||
System.out.println(SubAnnotation.class.isAnnotation()); // true | ||
|
||
// Case 3: Runtime vs. compile-time retention | ||
System.out.print("@SourceAnnotation is annotation: "); | ||
System.out.println(SourceAnnotation.class.isAnnotation()); // true | ||
|
||
// Case 4: Arrays and primitives | ||
System.out.print("Annotation array is annotation: "); | ||
System.out.println(MyAnnotation[].class.isAnnotation()); // false | ||
System.out.print("int.class is annotation: "); | ||
System.out.println(int.class.isAnnotation()); // false | ||
|
||
// Case 5: Proxy class (edge case) | ||
// Object proxy = java.lang.reflect.Proxy.newProxyInstance( //not yet implemented: INVOKEDYNAMIC | ||
// IsAnnotationExample.class.getClassLoader(), | ||
// new Class<?>[]{MyAnnotation.class}, | ||
// (proxyInstance, method, methodArgs) -> null | ||
// ); | ||
// System.out.print("Proxy of @MyAnnotation is annotation: "); | ||
// System.out.println(proxy.getClass().isAnnotation()); // false | ||
|
||
} | ||
|
||
// Annotation definitions | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
@interface MyAnnotation { | ||
} | ||
|
||
@Retention(RetentionPolicy.CLASS) | ||
@Target(ElementType.TYPE) | ||
@interface SourceAnnotation { | ||
} | ||
|
||
@MyAnnotation | ||
@interface SubAnnotation { | ||
} | ||
|
||
@Inherited | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
@interface InheritedAnnotation { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package samples.reflection.trivial.isenumexample; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
public class IsEnumExample { | ||
public static void main(String[] args) { | ||
// 1. Basic Case: Enum class | ||
System.out.print("Is TimeUnit enum: "); | ||
System.out.println(TimeUnit.class.isEnum()); // true | ||
|
||
// 2. Case: Enum constant's class | ||
System.out.print("Is TimeUnit.MINUTES enum: "); | ||
System.out.println(TimeUnit.MINUTES.getClass().isEnum()); // true | ||
|
||
// 3. Case: Non-enum class | ||
System.out.print("Is String enum: "); | ||
System.out.println(String.class.isEnum()); // false | ||
|
||
// 4. Case: Anonymous subclass of Enum (simulated with reflection) | ||
// Enum<?> anonymousEnum = Enum.valueOf(TimeUnit.class, "MINUTES"); // Native Call Error: Native method jdk/internal/reflect/DirectMethodHandleAccessor$NativeAccessor:invoke0:(Ljava/lang/reflect/Method;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object; not found | ||
// System.out.print("Is Anonymous Enum enum: "); | ||
// System.out.println(anonymousEnum.getClass().isEnum()); // true | ||
|
||
// 5. Case: Array of enums | ||
System.out.print("Is TimeUnit[].class enum: "); | ||
System.out.println(TimeUnit[].class.isEnum()); // false | ||
|
||
// 6. Case: Interface | ||
System.out.print("Is Runnable enum: "); | ||
System.out.println(Runnable.class.isEnum()); // false | ||
|
||
// 7. Case: Primitive type | ||
System.out.print("Is int enum: "); | ||
System.out.println(int.class.isEnum()); // false | ||
|
||
// 8. Case: Void type | ||
System.out.print("Is void enum: "); | ||
System.out.println(void.class.isEnum()); // false | ||
} | ||
} |
Binary file added
BIN
+639 Bytes
...ples/reflection/trivial/isannotationexample/IsAnnotationExample$InheritedAnnotation.class
Binary file not shown.
Binary file added
BIN
+594 Bytes
...samples/reflection/trivial/isannotationexample/IsAnnotationExample$MarkerAnnotation.class
Binary file not shown.
Binary file added
BIN
+586 Bytes
...ata/samples/reflection/trivial/isannotationexample/IsAnnotationExample$MyAnnotation.class
Binary file not shown.
Binary file added
BIN
+596 Bytes
...amples/reflection/trivial/isannotationexample/IsAnnotationExample$RuntimeAnnotation.class
Binary file not shown.
Binary file added
BIN
+592 Bytes
...samples/reflection/trivial/isannotationexample/IsAnnotationExample$SourceAnnotation.class
Binary file not shown.
Binary file added
BIN
+589 Bytes
...ta/samples/reflection/trivial/isannotationexample/IsAnnotationExample$SubAnnotation.class
Binary file not shown.
Binary file added
BIN
+1.63 KB
tests/test_data/samples/reflection/trivial/isannotationexample/IsAnnotationExample.class
Binary file not shown.
Binary file added
BIN
+1.16 KB
tests/test_data/samples/reflection/trivial/isenumexample/IsEnumExample.class
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters