javac

Does javac removes methods that are not referenced in the code?

自古美人都是妖i 提交于 2019-12-05 03:42:48
I have a code base and some methods are never used. Does javac remove the unused methods from the class file? Q: I want to know if I have a code base and some methods are never used. Does javac remove the unused methods from the class file? A: No. What goes into the class, stays in the class file. ... however ... The JVM loads only what's needed into memory. RAM isn't "wasted" on unused classes. No, it doesn't. To verify this, you can run javap -c foo.bar.MyClass and see all the code there. You can also access it via reflection (assuming you're running with appropriate permissions). No it

Covariant Return Type in Interface not compiling via Javac

白昼怎懂夜的黑 提交于 2019-12-05 02:52:45
I have the following structure: public interface BarReturn {} public interface FooReturn {} public interface FooBarReturn extends FooReturn, BarReturn {} public interface Foo { FooReturn fooBar( ); } public interface Bar { BarReturn fooBar(); } public interface FooBar extends Foo, Bar { FooBarReturn fooBar(); } Javac fails with the following message: FooBar.java:2: types Bar and Foo are incompatible; both define fooBar(), but with unrelated return types public interface FooBar extends Foo, Bar { ^ 1 error However, Eclipse can compile it fine, and as far as I can see it should compile - FooBar

How are anonymous classes compiled in Java?

青春壹個敷衍的年華 提交于 2019-12-05 00:37:26
I've heard that Java bytecode actually doesn't support any kind of unnamed classes. How does javac translate unnamned classes to named ones? It synthesizes a name of the form EnclosingClass$n , where "n" is a counter for anonymous classes in EnclosingClass . Because using $ in identifiers is discouraged, these names should not collide with any user-specified names. 来源: https://stackoverflow.com/questions/5808492/how-are-anonymous-classes-compiled-in-java

Set ant bootclasspath: JDK 1.7 has a new javac warning for setting an older source without bootclasspath

浪尽此生 提交于 2019-12-04 23:53:27
How do I set the ant bootclasspath in conjunction with -source 1.5 -target 1.5? How can this not be a hardcoded path to the 1.5 JDK? Can I set an environment variable to bootclasspath similar to how JAVA_HOME can be used from ant? Ideally I would like to do something like set an environment variable or pass an argument to ant. Here's an illustration of how you might fetch the Java 5 boot classes location from an environment variable, then use it. First, set the environment variable - say JAVA5_BOOTCLASSES . The property task gives you access to the environment, then the bootclasspath argument

Why this code compiles with jdk8u45 and above but not with jdk8u25?

时光总嘲笑我的痴心妄想 提交于 2019-12-04 22:36:26
Please, could someone help me to figure out why the following code compiles with jdk8u45 and above but fails with jdk8u25? I looked through the JDK release notes but didn't find anything related to the issue or maybe missed it. The code public class Main { static class Param { final int id; Param(int id) { this.id = id; } } static class Subtask { final Param param; Subtask(Param param) { this.param = param; } } public static void main(String[] args) { List<? extends Param> params = IntStream.range(1, 100).mapToObj(Param::new).collect(Collectors.toList()); NavigableMap<String, Subtask> map =

JavaCompiler not compiling files properly

断了今生、忘了曾经 提交于 2019-12-04 17:16:48
I am trying to get used to the JavaCompiler and trying to compile programs with it, I can successfully compile programs that comprises of single file but when im trying to compile projects with multiple files. I get errors on compiling files that implement other classes and where ever the class uses a method from the implemented class. Here is the code that I am using to compile the java code private final String directoryForBin = "C:/TempBINfolder/bin"; public List doCompilation(String sourceCode, String locationOfFile) { List<String> compile = new ArrayList<>(); SimpleJavaFileObject

javac complains: cannot find symbol on enum implementing interface

我怕爱的太早我们不能终老 提交于 2019-12-04 16:29:46
问题 I have three java types as defined below: Main.java: import java.util.Arrays; import java.util.List; public class Main { private Object callFunction() { OperationDefinitions func = OperationDefinitions.CONCATENATE; List<Object> values = Arrays.asList(new Object[] {"ABC", "-", "DEF"}); return func.call (values); } public static void main (String[] args) { Main main = new Main(); System.out.println (main.callFunction()); } } Operation.java import java.util.List; public interface Operation {

Should I use javac -O option to optimize?

怎甘沉沦 提交于 2019-12-04 15:15:13
问题 javac has an interesting -O option: Optimizes compiled code by inlining static, final and private methods. Note that your classes may get larger in size. This option seems to not be popular (hidden?), I've just discovered it today, on CodeCup 2014 page. -O is not mentioned in the official documentation nor in man javac... Strange. In accepted answer to similar question, we can read that: Optimization in Java is mostly done by the JIT compiler at runtime. Hence there is no point trying to

Runtime code generation and compilation

 ̄綄美尐妖づ 提交于 2019-12-04 14:50:53
Say I have this code that uses some input (e.g. a URL path) to determine which method to run, via reflection: // init map.put("/users/*", "viewUser"); map.put("/users", "userIndex"); // later String methodName = map.get(path); Method m = Handler.class.getMethod(methodName, ...); m.invoke(handler, ...); This uses reflection so the performance can be improved. It could be done like this: // init map.put("/users/*", new Runnable() { public void run() { handler.viewUser(); } }); map.put("/users", new Runnable() { public void run() { handler.userIndex(); } }); // later Runnable action = map.get