Skip to content

Commit fa80028

Browse files
authored
[MCOMPILER-485] Fixes internal string format in generated package-info.class (#95)
* [MCOMPILER-485] Add failing integration test for class file contents The internal package string in the class file should contain a forward slash * [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 * [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 ] ..."
1 parent f605c0f commit fa80028

File tree

6 files changed

+145
-3
lines changed

6 files changed

+145
-3
lines changed
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
invoker.goals = clean compile
19+
invoker.buildResult = success

src/it/MCOMPILER-485/pom.xml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
-->
19+
20+
<project
21+
xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
25+
<modelVersion>4.0.0</modelVersion>
26+
27+
<groupId>blah</groupId>
28+
<artifactId>blah</artifactId>
29+
<version>1.0</version>
30+
<packaging>jar</packaging>
31+
32+
<properties>
33+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34+
</properties>
35+
<build>
36+
<plugins>
37+
<plugin>
38+
<groupId>org.apache.maven.plugins</groupId>
39+
<artifactId>maven-compiler-plugin</artifactId>
40+
<version>@pom.version@</version>
41+
</plugin>
42+
</plugins>
43+
</build>
44+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package dummy;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
23+
public class HelloWorld
24+
{
25+
public static void main(String[] argv) {
26+
System.out.println("Hello World");
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
/**
21+
* This is the package javadoc
22+
*/
23+
package dummy;

src/it/MCOMPILER-485/verify.groovy

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
def packageInfoClassFile = new File( basedir, 'target/classes/dummy/package-info.class' )
21+
def packageInfoBytes = packageInfoClassFile.bytes
22+
def packageInfoHex = packageInfoBytes.encodeHex().toString()
23+
// "dummy/package-info" hex encoded
24+
assert packageInfoHex.contains( '64756d6d792f7061636b6167652d696e666f' )

src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -1363,12 +1363,16 @@ private void createMissingPackageInfoClasses( CompilerConfiguration compilerConf
13631363
private byte[] generatePackage( CompilerConfiguration compilerConfiguration, String javaFile )
13641364
{
13651365
int version = getOpcode( compilerConfiguration );
1366+
String internalPackageName = javaFile.substring( 0, javaFile.length() - ".java".length() );
1367+
if ( File.separatorChar != '/' )
1368+
{
1369+
internalPackageName = internalPackageName.replace( File.separatorChar, '/' );
1370+
}
13661371
ClassWriter cw = new ClassWriter( 0 );
1367-
cw.visitSource( "package-info.java", null );
13681372
cw.visit( version,
13691373
Opcodes.ACC_SYNTHETIC | Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE,
1370-
javaFile.substring( 0, javaFile.length() - ".java".length() ),
1371-
null, "java/lang/Object", null );
1374+
internalPackageName, null, "java/lang/Object", null );
1375+
cw.visitSource( "package-info.java", null );
13721376
return cw.toByteArray();
13731377
}
13741378

0 commit comments

Comments
 (0)