Compiler creates broken classfiles although errors are shown in output

Sven Reimers sven.reimers at gmail.com
Wed Feb 17 21:06:35 UTC 2016


Ok so here is an example - copy and paste into a shell script, make javac
available and run...

See the script / output for details.

Hope this helps

Sven

-------- snip here ---------
#!/bin/bash
mkdir interface
cat <<EOF >interface/I.java
public interface I {}
EOF

mkdir abstract
cat <<EOF >abstract/AImpl.java
public abstract class AImpl implements I {}
EOF

mkdir impl
cat <<EOF >impl/Impl.java
public class Impl extends AImpl {}
EOF

#Now create an AnnotationProcessor:

mkdir processor

cat <<EOF >processor/A.java
import javax.annotation.processing.*;
import java.util.Set;
import javax.lang.model.element.TypeElement;
import javax.lang.model.SourceVersion;

@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class A extends AbstractProcessor {

        @Override
        public synchronized void init(ProcessingEnvironment env){
System.out.println("Init");}

        @Override
        public boolean process(Set<? extends TypeElement> annoations,
RoundEnvironment env) { System.out.println("process");return true; }

}
EOF
mkdir processor/META-INF
mkdir processor/META-INF/services

cat <<EOF >processor/META-INF/services/javax.annotation.processing.Processor
A
EOF
#Now compile all of them to different directories:

mkdir build

mkdir build/interface
javac -d build/interface interface/I.java

mkdir build/abstract
javac -cp build/interface -d build/abstract abstract/AImpl.java

mkdir build/processor
javac -d build/processor processor/A.java
cp -r processor/META-INF build/processor

#Now try to compile Impl.java

mkdir build/impl-v1
echo Variant 1:
echo javac -cp build/abstract -d build/impl-v1 impl/Impl.java
javac -cp build/abstract -d build/impli-v1 impl/Impl.java
echo $?

#Fails due to missing interface class... exit code = 1

mkdir build/impl-v2
echo Variant 2
echo javac -cp build/abstract -processorpath build/processor -d
build/impl-v2 impl/Impl.java
javac -cp build/abstract -processorpath build/processor -d build/impl-v2
impl/Impl.java
echo $?
ls -la build/impl-v2/Impl.class
#Does not fail due to missing interface class although output shows the
problem... exit code = 0, class file generated but should not

mkdir build/impl-v3
echo Variant 3
echo javac -cp build/abstract:build/interface -processorpath
build/processor -d build/impl-v3 impl/Impl.java
javac -cp build/abstract:build/interface -processorpath build/processor -d
build/impl-v3 impl/Impl.java
echo $?
ls -la build/impl-v3/Impl.class

#Succeeds and good class file
-------- snip here ---------
---

On Tue, Feb 16, 2016 at 3:08 PM, Sven Reimers <sven.reimers at gmail.com>
wrote:

> Hi Jan,
>
> seems the problem is caused by using an annotation processor..
>
> It seems sufficient to have an annotation processor doing nothing to
> trigger the behaviour.
>
> Will try to post an example, if you need one..
>
> -Sven
> Am 15.02.2016 20:03 schrieb "Jan Lahoda" <jan.lahoda at oracle.com>:
>
>> Hi Sven,
>>
>> I tried the example with:
>> $ javac -fullversion
>> javac full version "1.8.0_40-b25"
>>
>> And it produced:
>> $ javac  Client.java
>> Client.java:7: error: cannot access I
>>        new A().m();
>>               ^
>>   class file for p1.I not found
>> 1 error
>>
>> No classfile was written.
>>
>> With:
>> $ javac -fullversion
>> javac full version "1.7.0_45-b35"
>>
>> $ javac  Client.java
>>
>> produced no errors, and written a (correct as far as I can tell)
>> classfile (equivalent to a classfile written when I.class is available).
>>
>> Do you have a testcase where javac produces an error, but writes an
>> (incorrect) classfile?
>>
>> Thanks,
>>     Jan
>>
>> On 15.2.2016 17:02, Sven Reimers wrote:
>>
>>> There is an example in here
>>>
>>>
>>> http://www.oracle.com/technetwork/java/javase/8-compatibility-guide-2156366.html
>>>
>>> Area: Tools / javac
>>>
>>> Synopsis
>>>
>>> Interfaces need to be present when compiling against their
>>> implementations
>>>
>>> Description
>>>
>>> When compiling a class against another class implementing an interface
>>> which is defined in yet another class file, such class file (where
>>> interface is defined) must be available in the class path used
>>> by javac during compilation. This is a new requirement as of JDK 8 - a
>>> failure to do so will result in a compilation error.
>>>
>>> Example:
>>>
>>> Client.java:
>>>
>>> import p1.A;
>>>
>>> class Client {
>>>
>>>     void test() {
>>>
>>>         new A.m();
>>>
>>>     }
>>>
>>> }
>>>
>>> p1/A.java:
>>>
>>> package p1;
>>>
>>> public class A implements I {
>>>
>>>     public void m() { }
>>>
>>> }
>>>
>>> p1/I.java:
>>>
>>> package p1;
>>>
>>> public interface I {
>>>
>>>     void m();
>>>
>>> }
>>>
>>> If neither p1/I.java nor p1/I.class are available when compiling
>>> Client.java, the following error will be displayed:
>>>
>>> Client.java: error: cannot access I
>>>
>>>         new A().m();
>>>
>>>              ^
>>>
>>>    class file for p1.I not found
>>>
>>>
>>> If this does not reproduce the problem, I will try to reduce our code
>>> sample
>>>
>>> Thanks
>>>
>>> Sven
>>>
>>>
>>> Hi Sven,
>>>
>>> Would you have a testcase on which this can be seen?
>>>
>>> Thanks,
>>>      Jan
>>>
>>> On 15.2.2016 15:34, Sven Reimers wrote:
>>>
>>>      >
>>>      > Hi,
>>>      >
>>>      > I just ran intohttps://bugs.openjdk.java.net/browse/JDK-8145208
>>>     <https://bugs.openjdk.java.net/browse/JDK-8145208>
>>>      >
>>>      > The main problem is not to fix the error,  but our continuous
>>>      > integration is not creating breaking builds,  so the error can get
>>>      > unnoticed into builds and fails at runtime.
>>>      >
>>>      > Any idea if this can be fixed in an upcoming jdk 8u release?
>>>      >
>>>      > Any idea how to make the build break in this case?
>>>      >
>>>      > Thanks for your help
>>>      >
>>>      > -Sven
>>>      >
>>>
>>>


-- 
Sven Reimers

* Senior Expert Software Architect
* Java Champion
* NetBeans Dream Team Member: http://dreamteam.netbeans.org
* Community Leader  NetBeans: http://community.java.net/netbeans
                              Desktop Java:
http://community.java.net/javadesktop
* JUG Leader JUG Bodensee: http://www.jug-bodensee.de
* Duke's Choice Award Winner 2009
* Blog: https://www.java.net//blog/sven

* XING: https://www.xing.com/profile/Sven_Reimers8
* LinkedIn: http://www.linkedin.com/in/svenreimers

Join the NetBeans Groups:
* XING: http://www.xing.com/group-20148.82db20
* NUGM: http://haug-server.dyndns.org/display/NUGM/Home
* LinkedIn: http://www.linkedin.com/groups?gid=1860468
                   http://www.linkedin.com/groups?gid=107402
                   http://www.linkedin.com/groups?gid=1684717
* Oracle: https://mix.oracle.com/groups/18497
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20160217/6d8ea93f/attachment.html>


More information about the compiler-dev mailing list