From 8cbfb2ed66158e6c2df2277ff38604eef95d862c Mon Sep 17 00:00:00 2001 From: Daniel Widdis Date: Tue, 15 Feb 2022 08:48:12 -0800 Subject: [PATCH 1/3] [MCOMPILER-485] Add failing integration test for class file contents The internal package string in the class file should contain a forward slash --- src/it/MCOMPILER-485/invoker.properties | 19 ++++++++ src/it/MCOMPILER-485/pom.xml | 44 +++++++++++++++++++ .../src/main/java/dummy/HelloWorld.java | 28 ++++++++++++ .../src/main/java/dummy/package-info.java | 23 ++++++++++ src/it/MCOMPILER-485/verify.groovy | 24 ++++++++++ 5 files changed, 138 insertions(+) create mode 100644 src/it/MCOMPILER-485/invoker.properties create mode 100644 src/it/MCOMPILER-485/pom.xml create mode 100644 src/it/MCOMPILER-485/src/main/java/dummy/HelloWorld.java create mode 100644 src/it/MCOMPILER-485/src/main/java/dummy/package-info.java create mode 100644 src/it/MCOMPILER-485/verify.groovy diff --git a/src/it/MCOMPILER-485/invoker.properties b/src/it/MCOMPILER-485/invoker.properties new file mode 100644 index 00000000..8595721d --- /dev/null +++ b/src/it/MCOMPILER-485/invoker.properties @@ -0,0 +1,19 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +invoker.goals = clean compile +invoker.buildResult = success diff --git a/src/it/MCOMPILER-485/pom.xml b/src/it/MCOMPILER-485/pom.xml new file mode 100644 index 00000000..bc6f4f24 --- /dev/null +++ b/src/it/MCOMPILER-485/pom.xml @@ -0,0 +1,44 @@ + + + + + 4.0.0 + + blah + blah + 1.0 + jar + + + UTF-8 + + + + + org.apache.maven.plugins + maven-compiler-plugin + @pom.version@ + + + + diff --git a/src/it/MCOMPILER-485/src/main/java/dummy/HelloWorld.java b/src/it/MCOMPILER-485/src/main/java/dummy/HelloWorld.java new file mode 100644 index 00000000..cda1006a --- /dev/null +++ b/src/it/MCOMPILER-485/src/main/java/dummy/HelloWorld.java @@ -0,0 +1,28 @@ +package dummy; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +public class HelloWorld +{ + public static void main(String[] argv) { + System.out.println("Hello World"); + } +} diff --git a/src/it/MCOMPILER-485/src/main/java/dummy/package-info.java b/src/it/MCOMPILER-485/src/main/java/dummy/package-info.java new file mode 100644 index 00000000..ed72542b --- /dev/null +++ b/src/it/MCOMPILER-485/src/main/java/dummy/package-info.java @@ -0,0 +1,23 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * This is the package javadoc + */ +package dummy; \ No newline at end of file diff --git a/src/it/MCOMPILER-485/verify.groovy b/src/it/MCOMPILER-485/verify.groovy new file mode 100644 index 00000000..2384fecd --- /dev/null +++ b/src/it/MCOMPILER-485/verify.groovy @@ -0,0 +1,24 @@ + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +def packageInfoClassFile = new File( basedir, 'target/classes/dummy/package-info.class' ) +def packageInfoBytes = packageInfoClassFile.bytes +def packageInfoHex = packageInfoBytes.encodeHex().toString() +// "dummy/package-info" hex encoded +assert packageInfoHex.contains( '64756d6d792f7061636b6167652d696e666f' ) From 7c9f69f5720b40d9e70d87e1203ba87cbd2ddf43 Mon Sep 17 00:00:00 2001 From: Daniel Widdis Date: Tue, 15 Feb 2022 09:01:51 -0800 Subject: [PATCH 2/3] [MCOMPILER-485] Always use forward slash for internal package name Per JLS 4.2.1 if the file separator character is not a forward slash, replace it --- .../maven/plugin/compiler/AbstractCompilerMojo.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java index a027e550..a0c284f1 100644 --- a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java +++ b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java @@ -1363,12 +1363,16 @@ private void createMissingPackageInfoClasses( CompilerConfiguration compilerConf private byte[] generatePackage( CompilerConfiguration compilerConfiguration, String javaFile ) { int version = getOpcode( compilerConfiguration ); + String internalPackageName = javaFile.substring( 0, javaFile.length() - ".java".length() ); + if ( File.separatorChar != '/' ) + { + internalPackageName = internalPackageName.replace( File.separatorChar, '/' ); + } ClassWriter cw = new ClassWriter( 0 ); cw.visitSource( "package-info.java", null ); cw.visit( version, Opcodes.ACC_SYNTHETIC | Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE, - javaFile.substring( 0, javaFile.length() - ".java".length() ), - null, "java/lang/Object", null ); + internalPackageName, null, "java/lang/Object", null ); return cw.toByteArray(); } From d6106cbb1664c0f552cca3c05008b1c2bf514350 Mon Sep 17 00:00:00 2001 From: Daniel Widdis Date: Tue, 15 Feb 2022 09:53:50 -0800 Subject: [PATCH 3/3] [MCOMPILER-485] Call ClassWriter#visit before ClassWriter#visitSource Javadocs for org.objectweb.asm.ClassVisitor specify "The methods of this class must be called in the following order: visit [ visitSource ] ..." --- .../org/apache/maven/plugin/compiler/AbstractCompilerMojo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java index a0c284f1..915d39f5 100644 --- a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java +++ b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java @@ -1369,10 +1369,10 @@ private byte[] generatePackage( CompilerConfiguration compilerConfiguration, Str internalPackageName = internalPackageName.replace( File.separatorChar, '/' ); } ClassWriter cw = new ClassWriter( 0 ); - cw.visitSource( "package-info.java", null ); cw.visit( version, Opcodes.ACC_SYNTHETIC | Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE, internalPackageName, null, "java/lang/Object", null ); + cw.visitSource( "package-info.java", null ); return cw.toByteArray(); }