jls

Import-on-demand declaration with subpackages only

无人久伴 提交于 2019-12-05 12:17:51
Related : How can I compile "import pack.*" with ant/javac, when there are no such classes? Suppose we have the given package structure parent | ---a ---b where the package parent only contains the two subpackges a and b (no class is under the package parent ). The code import parent.* , situated in a package other than parent , compiled with Maven (i.e. javac ) throws a compile-time error. The error is: package parent does not exist I looked into the Java Language Specification about such a case (import-on-demand declaration where no types are actually imported). Paragraph 7.5.2 of the JLS

Why open a non-existing package from a Java module?

二次信任 提交于 2019-12-05 09:40:13
The JLS 11 "7.7.2. Exported and Opened Packages" says: It is permitted for opens to specify a package which is not declared by a compilation unit associated with the current module. What would be a scenario for this? Why is this needed? Thanks to Alan Bateman and Michael Easter for explanations, and I can think of some realistic scenarios. First, as was explained by Alan and Michael: JLS allows to "open" directories without Java types for situations when we keep resources in them. So, for us these are "resource directories", but JLS names it as package which is not declared by a compilation

Final Fields Semantics in Threads

感情迁移 提交于 2019-12-04 23:28:33
问题 This is from JLS 17.5: The usage model for final fields is a simple one. Set the final fields for an object in that object's constructor. Do not write a reference to the object being constructed in a place where another thread can see it before the object's constructor is finished. If this is followed, then when the object is seen by another thread, that thread will always see the correctly constructed version of that object's final fields. It will also see versions of any object or array

What is the difference between qualified name and a field access expression?

偶尔善良 提交于 2019-12-04 22:48:51
问题 From the JLS details on protected access: Let C be the class in which a protected member is declared. Access is permitted only within the body of a subclass S of C. In addition, if Id denotes an instance field or instance method, then: If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S. If the access is by a field access expression E.Id, where E is a Primary expression, or by

Why Java methods with varargs identified as transient?

柔情痞子 提交于 2019-12-04 10:00:24
问题 I was playing with Java Reflection API and observed that methods with variadic argument list become transient. Why is that and what does transient keyword mean in this context? From Java Glossary, transient : A keyword in the Java programming language that indicates that a field is not part of the serialized form of an object. When an object is serialized, the values of its transient fields are not included in the serial representation, while the values of its non-transient fields are

How does the Java compiler choose the runtime type for a parameterized type with multiple bounds?

点点圈 提交于 2019-12-04 08:03:25
问题 I would like to understand better what happens when the Java compiler encounters a call to a method like the one below. <T extends AutoCloseable & Cloneable> void printType(T... args) { System.out.println(args.getClass().getComponentType().getSimpleName()); } // printType() prints "AutoCloseable" It is clear to me that there is no type <T extends AutoCloseable & Cloneable> at runtime, so the compiler makes the least wrong thing it can do and creates an array with the type of one of the two

Why do interfaces extend Object, according to the class file format?

对着背影说爱祢 提交于 2019-12-04 02:03:27
Why does the JVM specification state that interfaces must have a super_class of java/lang/Object , even though interfaces do not extend java/lang/Object ? I'm specifically referring to §4.1 of the JVM spec, where it says: For an interface, the value of the super_class item must always be a valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Class_info structure representing the class Object. yet in §9.2 of the JLS, it says that interfaces do not extend Object. Instead a implicitly created abstract method is declared which matches each public

Final Fields Semantics in Threads

馋奶兔 提交于 2019-12-03 15:11:07
This is from JLS 17.5: The usage model for final fields is a simple one. Set the final fields for an object in that object's constructor. Do not write a reference to the object being constructed in a place where another thread can see it before the object's constructor is finished. If this is followed, then when the object is seen by another thread, that thread will always see the correctly constructed version of that object's final fields. It will also see versions of any object or array referenced by those final fields that are at least as up-to-date as the final fields are. The discussion

What is the difference between qualified name and a field access expression?

一世执手 提交于 2019-12-03 14:22:08
From the JLS details on protected access : Let C be the class in which a protected member is declared. Access is permitted only within the body of a subclass S of C. In addition, if Id denotes an instance field or instance method, then: If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S. If the access is by a field access expression E.Id, where E is a Primary expression, or by a method invocation expression E.Id(. . .), where E is a Primary expression, then the access is

Annotation attribute must be a class literal? Why? Constants should be fine too

独自空忆成欢 提交于 2019-12-03 12:47:25
Can someone explain why String and Class annotation parameters are expected differently? Why does the compiler require literals for Classes, wherby accepting constants for Strings as well? Working example with Spring's @RequestMapping: public class MyController { public static final String REQUEST_MAPPING = "/index.html"; @RequestMapping(MyController.REQUEST_MAPPING) // ALL OK! ... } WTF example with TestNG's @Test: public class MyControllerTest { public static final Class TEST_EXCEPTION = RuntimeException.class; @Test(expectedExceptions = MyControllerTest.TEST_EXCEPTION) // compilation error,