What is the problem with the extra classes?
Recently we have a project. Our task is to help analyze the causes of several strange bugs and correct them. The development tool uses eclipse3.0. the deliverables that need to be submitted to the customer are:Source codeAnd the compiled jar files.
The entire development work was relatively smooth, and deliverables were submitted on schedule. But a few days later, the customer raised a question to us, asking us to explain that the jar we submitted contains 269 classes, while the customer compiled the sourceCodeThere are 271 classes in the result. What is the difference between the two classes?
After communication, we learned that the customer compiled with the javac command under the command line. In order to find out the truth of the problem, we also used the same method as the customer for compilation, it is found that the compilation result is indeed 271 classes. What are the two more classes?
We use beyond compare to compare the compilation results of the two methods and find that the two more classes are gameclient $1. Class and gameboard $1. Class. Decompile gameclient $1. Class to obtain the following result (the other is the same except for the package name ):
// Decompiled by DJ v 2.8.8. 54 Copyright 2000 Atanas neshkov Date: 17:22:29
// Home page: http://members.fortunecity.com/neshkov/dj.html-check often for new version!
// Decompiler options: packimports (3)
// Source file name: gameclient. Java
Package XXX. XXX; (the related information is hidden here)
Static class
{
}
From this code,ProgramBut the source code of the program cannot be found. So how are these two classes actually generated? What happened during javac compilation?
After some investigation, I finally located how the Anonymous class was generated. Let's take a look at the following code:
Public class outerclass {
Private innerclass test = new innerclass ();
Private class innerclass {
}
}
The expected compilation result may be the following two class files:
Outerclass. Class
Outerclass $ innerclass. Class
The actual javac compilation result class file is:
Outerclass. Class
Outerclass $ innerclass. Class
Outerclass $1. Class
Outerclass $1. Class is added because:
When internal classes are private and public constructors are not explicitly written, the default constructor is private, javac (Sun JDK 1.4) in this case, you can create an accessible constructor with a parameter. The parameter type is an anonymous static class, therefore, an additional class file will be generated during compilation.
You may use eclipse for compilation, so yes, the result is consistent with your expectation, there is no outerclass $1. Class. Why? Is the compiler used by ECLIPSE jdt different from the javac provided by JDK? Yes, eclipse jdt uses its own built-in compiler, which has some enhanced functions, including perfect handling of the above situation.
The root cause of the problem is that there is a private internal class in gameclient. Java and gameboard. Java that does not define constructors.
So far, this issue has brought us the following inspiration:
1. Confirm the Java class compiler used by the project. If possible, use javac to generate the result application or reach an agreement with the customer.
2. Write the default constructor and its visibility as clearly as possible. For example, change the code above:
Public class outerclass {
Private innerclass test = new innerclass ();
Private class innerclass {
Public innerclass (){
}
}
}
The above is a bit of experience we have gained through this project. I hope it will be helpful to you.