Inter Type Declaration on Compiled Class File
Date : March 29 2020, 07:55 AM
wish helps you Update: Oh my goodness, you do not need reflection to access members or execute methods. Eclipse shows errors in the editor, but you may just ignore them, the code compiles and runs fine anyway. So the aspect is really much more strightforward and simple: public aspect LTWAspect {
public static String Application.staticField = "value of static field";
public String Application.normalField = "value of normal field";
public void Application.myMethod() {
System.out.println(normalField);
}
void around() : execution(void Application.main(..)) {
System.out.println("around before");
proceed();
System.out.println("around after");
System.out.println(Application.staticField);
new Application().myMethod();
}
}
public class Application {
public static void main(String[] args) {
System.out.println("main");
}
}
import org.aspectj.lang.SoftException;
public aspect LTWAspect {
public static String Application.staticField = "value of static field";
public String Application.normalField = "value of normal field";
public void Application.myMethod() {
try {
System.out.println(Application.class.getDeclaredField("normalField").get(this));
} catch (Exception e) {
throw new SoftException(e);
}
}
void around() : execution(void Application.main(..)) {
System.out.println("around before");
proceed();
System.out.println("around after");
try {
System.out.println(Application.class.getDeclaredField("staticField").get(null));
Application.class.getDeclaredMethod("myMethod", null).invoke(new Application());
} catch (Exception e) {
throw new SoftException(e);
}
}
}
around before
main
around after
value of static field
value of normal field
|
Type Erasure in Java contradictory: how does the compiled class file ensures type checking?
Date : March 29 2020, 07:55 AM
I wish this helpful for you There are no type checks at runtime except for the erased upper bounds. Java Generics is all about compiler checking. On the other hand, maybe your question is only about how can the compiler do its checks if the type parameter information is gone from the bytecode. The answer to that is that it is not gone from the classfile as a whole: it is attached as meta-data, available to the compiler (as well as the Reflection API), but irrelevant to the executing code.
|
TYPE_USE annotations on generic type not being compiled into class file
Date : March 29 2020, 07:55 AM
I wish this help you The annotations are compiled into the class file. I suspect that whatever tool you used to inspect the class file is faulty. Here is a MWE: import java.util.List;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target(ElementType.TYPE_USE)
@interface Valid{}
public class GroupPayload<T> {
List<@Valid T> data;
}
javac GroupPayload.java
javap -v GroupPayload.class
...
#9 = Utf8 LValid;
...
java.util.List<T> data;
...
RuntimeInvisibleTypeAnnotations:
0: #9(): FIELD, location=[TYPE_ARGUMENT(0)]
Classfile /home/mernst/GroupPayload.class
Last modified Jan 16, 2020; size 515 bytes
MD5 checksum 3db07417a8da20b35032650b64e9ffce
Compiled from "GroupPayload.java"
public class GroupPayload<T extends java.lang.Object> extends java.lang.Object
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #3.#22 // java/lang/Object."<init>":()V
#2 = Class #23 // GroupPayload
#3 = Class #24 // java/lang/Object
#4 = Utf8 data
#5 = Utf8 Ljava/util/List;
#6 = Utf8 Signature
#7 = Utf8 Ljava/util/List<TT;>;
#8 = Utf8 RuntimeInvisibleTypeAnnotations
#9 = Utf8 LValid;
#10 = Utf8 <init>
#11 = Utf8 ()V
#12 = Utf8 Code
#13 = Utf8 LineNumberTable
#14 = Utf8 LocalVariableTable
#15 = Utf8 this
#16 = Utf8 LGroupPayload;
#17 = Utf8 LocalVariableTypeTable
#18 = Utf8 LGroupPayload<TT;>;
#19 = Utf8 <T:Ljava/lang/Object;>Ljava/lang/Object;
#20 = Utf8 SourceFile
#21 = Utf8 GroupPayload.java
#22 = NameAndType #10:#11 // "<init>":()V
#23 = Utf8 GroupPayload
#24 = Utf8 java/lang/Object
{
java.util.List<T> data;
descriptor: Ljava/util/List;
flags:
Signature: #7 // Ljava/util/List<TT;>;
RuntimeInvisibleTypeAnnotations:
0: #9(): FIELD, location=[TYPE_ARGUMENT(0)]
public GroupPayload();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 8: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this LGroupPayload;
LocalVariableTypeTable:
Start Length Slot Name Signature
0 5 0 this LGroupPayload<TT;>;
}
Signature: #19 // <T:Ljava/lang/Object;>Ljava/lang/Object;
SourceFile: "GroupPayload.java"
|
Type provider and big XML file
Date : March 29 2020, 07:55 AM
|
Understanding Type in Compiled Scala File
Tag : scala , By : rhyhann
Date : March 29 2020, 07:55 AM
|