-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MCOMPILER-542] Clean JDK patch version in module-info.class
Signed-off-by: Jorge Solórzano <jorsol@gmail.com>
- Loading branch information
Showing
7 changed files
with
224 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,58 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ 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. | ||
--> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.apache.maven.plugins.compiler.it</groupId> | ||
<artifactId>MCOMPILER-542</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>${java.specification.version}</name> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.build.outputTimestamp>2023-08-14T15:12:12Z</project.build.outputTimestamp> | ||
</properties> | ||
|
||
<build> | ||
<pluginManagement> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>@project.version@</version> | ||
<configuration> | ||
<release>${java.specification.version}</release> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</pluginManagement> | ||
</build> | ||
</project> | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ 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. | ||
--> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.apache.maven.plugins.compiler.it</groupId> | ||
<artifactId>MCOMPILER-542</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>${java.specification.version}</name> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.build.outputTimestamp>2023-08-14T15:12:12Z</project.build.outputTimestamp> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-jdk-platform-logging</artifactId> | ||
<version>2.0.9</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<pluginManagement> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>@project.version@</version> | ||
<configuration> | ||
<release>${java.specification.version}</release> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</pluginManagement> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,58 @@ | ||
/* | ||
* 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 proc = 'javap -v target/classes/module-info.class'.execute(null,basedir) | ||
def sout = new StringBuilder(), serr = new StringBuilder() | ||
proc.consumeProcessOutput(sout, serr) | ||
proc.waitForOrKill(1000) | ||
def out = sout.toString() | ||
println "javap -v target/classes/module-info.class>\n$out\nerr> $serr" | ||
|
||
def module = out.substring(out.indexOf('Module:')) | ||
def javaVersion = System.getProperty('java.version') | ||
assert module.contains('// "java.base" ACC_MANDATED') | ||
assert !module.contains(javaVersion) | ||
/* | ||
* 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. | ||
*/ | ||
|
||
// Check if the javap tool is available | ||
def javapTool = java.util.spi.ToolProvider.findFirst("javap") | ||
assert javapTool.isPresent() : "javap tool not found. Make sure you have the JDK installed." | ||
|
||
def moduleDescriptor = new File(basedir, "target/classes/module-info.class") | ||
// Create a list of arguments to pass to the javap tool | ||
String[] args = ["-v", moduleDescriptor] | ||
|
||
def swout = new StringWriter(), swerr = new StringWriter() | ||
// Execute the javap tool with args | ||
def result = javapTool.get().run(new PrintWriter(swout), new PrintWriter(swerr), args) | ||
println swerr.toString().isEmpty() ? "javap output:\n$swout" : "javap error:\n$swerr" | ||
assert (result == 0) : "javap run failed" | ||
|
||
// Assertions of module content | ||
def out = swout.toString() | ||
def javaVersion = System.getProperty('java.version') | ||
assert out.contains('// "java.base" ACC_MANDATED') : "module not found in module-info.class" | ||
assert out.contains('// "java.logging"') : "module not found in module-info.class" | ||
assert out.contains('// "jdk.zipfs"') : "module not found in module-info.class" | ||
assert out.contains('// "org.slf4j.jdk.platform.logging"') : "module not found in module-info.class" | ||
assert out.contains('// 2.0.9') : "version of org.slf4j.jdk.platform.logging module not found" | ||
// Validation that the module-info should not contain the java version. | ||
assert !out.contains(' ' + javaVersion) : "ERROR: java version found in module descriptor" | ||
|
||
// Additional validation that the checksum is always the same | ||
def javaSpecVersion = System.getProperty('java.specification.version') | ||
|
||
def checksumMap = [ | ||
'21': 'SHA-256 checksum dd3489f88cb2e5afe569a6711c02f27be8ddb01faf48b9704fb1708a8ccdee6a', | ||
'17': 'SHA-256 checksum ab3ba2a20435f1014706e0dbfa13cc11c697374c54a20d664ab6e07d1b0ed2ac', | ||
'11': 'MD5 checksum 157411fcf56061a19e20922876f9903b' | ||
] | ||
|
||
def expectedChecksum = checksumMap[javaSpecVersion] | ||
if (expectedChecksum) { | ||
println "Java version: $javaVersion" | ||
assert out.contains(expectedChecksum) : "checksum doesn't match expected output" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/org/apache/maven/plugin/compiler/ModuleInfoTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package org.apache.maven.plugin.compiler; | ||
|
||
import org.objectweb.asm.ClassReader; | ||
import org.objectweb.asm.ClassVisitor; | ||
import org.objectweb.asm.ClassWriter; | ||
import org.objectweb.asm.ModuleVisitor; | ||
import org.objectweb.asm.Opcodes; | ||
|
||
final class ModuleInfoTransformer { | ||
|
||
private ModuleInfoTransformer() {} | ||
|
||
public static byte[] transformModuleInfo(byte[] originalBytecode) { | ||
ClassReader reader = new ClassReader(originalBytecode); | ||
ClassWriter writer = new ClassWriter(0); | ||
|
||
ClassVisitor classVisitor = new ClassVisitor(Opcodes.ASM9, writer) { | ||
@Override | ||
public ModuleVisitor visitModule(String name, int access, String version) { | ||
ModuleVisitor originalModuleVisitor = super.visitModule(name, access, version); | ||
return new ModuleVisitor(Opcodes.ASM9, originalModuleVisitor) { | ||
@Override | ||
public void visitRequire(String module, int access, String version) { | ||
// Check if the module name matches the java/jdk modules | ||
if (version != null && (module.startsWith("java.") || module.startsWith("jdk."))) { | ||
// Remove the version from the java.* and jdk.* modules | ||
super.visitRequire(module, access, null); | ||
} else { | ||
// Keep the original require statement | ||
super.visitRequire(module, access, version); | ||
} | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
reader.accept(classVisitor, 0); | ||
return writer.toByteArray(); | ||
} | ||
} |