跳至主要内容

博文

目前显示的是 二月, 2017的博文

JAXB: Diff Results In Java7 and Java8

Diff Result In Intellij & Maven Recently, we came across some mystrious bugs when using JAXB: We tested our code using Intellij idea by running junit test, in which all tests passed. So we push my code to remote repos and where some build and regression test will run again. Soon, we found that the tests on remote failed. we re-tested many times in Intellij but code still works. Later, we realize that the remote testing using maven. So we test it locally, using maven. It failed also. Analysis My first response is the bug of either Intellij or maven, so we try to search on google, but found nothing. Soon, we find that I can get more detailed message from following command: mvn -X test Running it give us more configuration information that maven use. Comparing it with the setting in Intellij, we found that the main difference lay in: maven use Java8, whereas Intellij using Java7 So we change Intellij to use Java8 and the error also be triggered. Debugging using ...

How Is Enum Implemented?

Enum is a very useful tool to represent a set of fixed content, and we can define and use it like a class which is much powerful and convenient. But, when we were implementing some operations using the enum, I confronting some questions about some compiler errors. So today, we will dive into how enum is implemented to understand the complaint issued by compiler. Cannot find symbol public enum EnumImpl { A { public static final int a = 1 ; }; } We confronting this error when we try to access A.a outside enum constant A . At first, it seems a little bit confusing, but after skimming the byte code, we can understand why: public final static enum EnumImpl; A final enum EnumImpl $1 extends EnumImpl {} Translating it to Java code: final enum EnumImpl$ 1 extends EnumImpl {...} public final static EnumImpl A = new EnumImpl$ 1 (); So, it become so obvious that we can’t access child’s element using father’s reference and that is where errors ...

JVM Startup and Inheritance

This blog is the follow up blog to answer this question : Why is the main method in superclass executed by JVM? class A { public static void main (String[] args) { System.out.println( "A.main" ); } } class B extends A{ } If you execute java B you will see the console output: A.main Before going on, we can take a minute to think why it is works. java B is to call main via JNI by JVM. So, yy viewing the process JVM load the class, we can see why it works clearly: When JVM start up, it will load and initiate your class and invoke LauncherHelper#checkAndLoadMain which will validate if B has main memthod: validateMainClass(mainClass); And continue in validateMainClass , it invokes the Class#getMethod : static void validateMainClass(Class<?> mainClass) { Method mainMethod; try { mainMethod = mainClass.getMethod( "main" , String[].class); } catch (NoSuchMethodException nsme) { //...