Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone provide an example of how javac's -implicit option works?

At the shell command prompt, if I type in javac -help, it tells me that the -implicit option specifies "... whether or not to generate class files for implicitly referenced files". There are two options that go with -implicit: none or class.

Firstly, what's the difference between using -implicit:none and not using the -implicit option?

Secondly, regarding my question title, I was hoping that an example provided would help me to understand what an implicitly referenced file was. Thanks a lot.

like image 808
user2911290 Avatar asked May 07 '14 14:05

user2911290


1 Answers

You have it described quite well in the Java documentation.

The answer to your first question is quite simple. Not using the -implicit option is almost like using -implicit:class (which is the default value of the option), but by explicitly using the option you suppress certain warning:

The compiler might not discover the need for some type information until after annotation processing completes. When the type information is found in a source file and no -implicit option is specified, the compiler gives a warning that the file is being compiled without being subject to annotation processing. To disable the warning, either specify the file on the command line (so that it will be subject to annotation processing) or use the -implicit option to specify whether or not class files should be generated for such source files.

Now your second question. As the documentation states:

To compile a source file, the compiler often needs information about a type, but the type definition is not in the source files specified on the command line. The compiler needs type information for every class or interface used, extended, or implemented in the source file. This includes classes and interfaces not explicitly mentioned in the source file, but that provide information through inheritance.

For example, when you create a subclass java.applet.Applet, you are also using the ancestor classes of Applet: java.awt.Panel, java.awt.Container, java.awt.Component, and java.lang.Object.

Let's have three classes in three files: Main, ImplicitClass, BaseImplicitClass.

Main.java:

public class Main {
    public static void main(String[] args) {
        ImplicitClass ec = new ImplicitClass();
        System.out.println(ec.getClass());
    }
}

ImplicitClass.java:

public class ImplicitClass extends BaseImplicitClass {
}

BaseImplicitClass.java:

public class BaseImplicitClass {
}

When you compile them from command line like this:

javac -cp . -implicit:class Main.java

or

javac -cp . Main.java

all the three *.class files are created.

When you compile it

javac -cp . -implicit:none Main.java

only Main.class is created.

When you compile it

javac -cp . -implicit:none Main.java ImplicitClass.java

two *.class files are created, Main.class and ImplicitClass.class (the ImplicitClass has now become explicit because it has been explicitly passed to the compiler), but not BaseImplicitClass.class as this class is needed, but is implicit.

like image 54
Honza Zidek Avatar answered Oct 30 '22 02:10

Honza Zidek