Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions plexus-compiler-its/src/main/it/issues-351/invoker.properties
Original file line number Diff line number Diff line change
@@ -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.os.family = windows
invoker.goals = clean compile
89 changes: 89 additions & 0 deletions plexus-compiler-its/src/main/it/issues-351/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.codehaus.plexus.compiler.it</groupId>
<artifactId>issues-351</artifactId>
<version>1.0-SNAPSHOT</version>

<name>Issue 351: preserve multiline compiler arguments on Windows</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<plexus.compiler.version>@project.version@</plexus.compiler.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>@maven.compiler.version@</version>
<configuration>
<fork>true</fork>
<release>11</release>
<compilerArgs>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg>
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
<arg>-XDcompilePolicy=simple</arg>
<arg>-Xplugin:ErrorProne
-Xep:DeadException:WARN</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_core</artifactId>
<version>2.24.1</version>
</path>
</annotationProcessorPaths>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-api</artifactId>
<version>${plexus.compiler.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-manager</artifactId>
<version>${plexus.compiler.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-javac</artifactId>
<version>${plexus.compiler.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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 foo;

public class Example {
public void createException() {
new RuntimeException();
}
}
23 changes: 23 additions & 0 deletions plexus-compiler-its/src/main/it/issues-351/verify.groovy
Original file line number Diff line number Diff line change
@@ -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.
*/

assert new File(basedir, 'target/classes/foo/Example.class').exists()

def buildLog = new File(basedir, 'build.log').getText('UTF-8')
assert buildLog.contains('[DeadException] Exception created but not thrown')
Original file line number Diff line number Diff line change
Expand Up @@ -1068,9 +1068,7 @@ private File createFileWithArguments(String[] args, String outputDirectory) thro

writer = new PrintWriter(new FileWriter(tempFile));
for (String arg : args) {
String argValue = arg.replace(File.separatorChar, '/');
writer.write("\"" + argValue + "\"");
writer.println();
writer.println(quoteArgument(arg));
}
writer.flush();

Expand All @@ -1083,6 +1081,41 @@ private File createFileWithArguments(String[] args, String outputDirectory) thro
}
}

/**
* Quotes an argument according to javac command-line argument file syntax.
*/
static String quoteArgument(String argument) {
StringBuilder quoted = new StringBuilder(argument.length() + 2);
quoted.append('"');
for (int i = 0; i < argument.length(); i++) {
char c = argument.charAt(i);
switch (c) {
case '\\':
quoted.append("\\\\");
break;
case '"':
quoted.append("\\\"");
break;
case '\n':
quoted.append("\\n");
break;
case '\r':
quoted.append("\\r");
break;
case '\t':
quoted.append("\\t");
break;
case '\f':
quoted.append("\\f");
break;
default:
quoted.append(c);
break;
}
}
return quoted.append('"').toString();
}

/**
* Get the path of the javac tool executable to use.
* Either given through explicit configuration or via {@link #getJavacExecutable()}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,29 @@ void testExtractMajorAndMinorVersion() {
assertEquals("21", JavacCompiler.extractMajorAndMinorVersion("javac 21"));
assertEquals("1.8", JavacCompiler.extractMajorAndMinorVersion("javac 1.8.0_432"));
}

@ParameterizedTest(name = "{0}")
@MethodSource("quoteArgumentArguments")
void quoteArgument(String description, String argument, String expected) {
assertEquals(expected, JavacCompiler.quoteArgument(argument));
}

private static Stream<Arguments> quoteArgumentArguments() {
return Stream.of(
Arguments.of("empty", "", "\"\""),
Arguments.of("plain", "-Xlint", "\"-Xlint\""),
Arguments.of("spaces", "two words", "\"two words\""),
Arguments.of("double quote", "say \"hello\"", "\"say \\\"hello\\\"\""),
Arguments.of(
"Windows path",
"C:\\Program Files\\Java\\bin\\javac.exe",
"\"C:\\\\Program Files\\\\Java\\\\bin\\\\javac.exe\""),
Arguments.of("trailing backslash", "C:\\temp\\", "\"C:\\\\temp\\\\\""),
Arguments.of("literal backslash-n", "-Dvalue=\\n", "\"-Dvalue=\\\\n\""),
Arguments.of(
"control whitespace",
"line1\nline2\rline3\tline4\fline5",
"\"line1\\nline2\\rline3\\tline4\\fline5\""),
Arguments.of("backslash and newline", "line1\\\nline2", "\"line1" + "\\\\" + "\\n" + "line2\""));
}
}
Loading