Skip to content

Commit cc3709e

Browse files
authored
Common code for argument file creation (#229)
* Fixes #228 Moves the common code for file generation for Arguments to utilities class * Cannot remove the method. * No Launch Configuration attached to Launch * Config name overridden in tests * Option required for argument file * Use Optional String as input for util method
1 parent 05e5a12 commit cc3709e

3 files changed

Lines changed: 62 additions & 27 deletions

File tree

org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/ClasspathShortener.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2018, 2019 Cedric Chabanois and others.
2+
* Copyright (c) 2018, 2023 Cedric Chabanois and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -14,8 +14,6 @@
1414
*******************************************************************************/
1515
package org.eclipse.jdt.internal.launching;
1616

17-
import static org.eclipse.jdt.internal.launching.LaunchingPlugin.LAUNCH_TEMP_FILE_PREFIX;
18-
1917
import java.io.File;
2018
import java.io.FileOutputStream;
2119
import java.io.IOException;
@@ -293,9 +291,8 @@ private void shortenPathUsingClasspathArgumentFile(int modulePathArgumentIndex,
293291
String path = cmdLine.get(modulePathArgumentIndex);
294292
File file;
295293
try {
296-
String timeStamp = getLaunchTimeStamp();
297-
File argFile = new File(processTempFilesDir, String.format(LAUNCH_TEMP_FILE_PREFIX + "%s" + option //$NON-NLS-1$
298-
+ "-arg-%s.txt", getLaunchConfigurationName(), timeStamp)); //$NON-NLS-1$
294+
File argFile = JavaLaunchingUtils.createFileForArgument(getLaunchTimeStamp(), processTempFilesDir, getLaunchConfigurationName(), "%s" //$NON-NLS-1$
295+
+ option + "-arg-%s.txt"); //$NON-NLS-1$
299296

300297
String arg = option + " " + quoteWindowsPath(path); //$NON-NLS-1$
301298
Charset systemCharset = Platform.getSystemCharset();
@@ -343,9 +340,7 @@ protected boolean handleClasspathTooLongStatus() throws CoreException {
343340

344341
private File createClasspathOnlyJar(String classpath) throws CoreException {
345342
try {
346-
String timeStamp = getLaunchTimeStamp();
347-
File jarFile = new File(processTempFilesDir, String.format(LAUNCH_TEMP_FILE_PREFIX
348-
+ "%s-classpathOnly-%s.jar", getLaunchConfigurationName(), timeStamp)); //$NON-NLS-1$
343+
File jarFile = JavaLaunchingUtils.createFileForArgument(getLaunchTimeStamp(), processTempFilesDir, getLaunchConfigurationName(), "%s-classpathOnly-%s.jar"); //$NON-NLS-1$
349344
URI workingDirUri = processTempFilesDir.toURI();
350345
StringBuilder manifestClasspath = new StringBuilder();
351346
String[] classpathArray = getClasspathAsArray(classpath);
@@ -386,11 +381,7 @@ protected String getLaunchConfigurationName() {
386381
}
387382

388383
protected String getLaunchTimeStamp() {
389-
String timeStamp = launch.getAttribute(DebugPlugin.ATTR_LAUNCH_TIMESTAMP);
390-
if (timeStamp == null) {
391-
timeStamp = Long.toString(System.currentTimeMillis());
392-
}
393-
return timeStamp;
384+
return JavaLaunchingUtils.getLaunchTimeStamp(launch);
394385
}
395386

396387
private String[] getEnvpFromNativeEnvironment() {

org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/CommandLineShortener.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2020 Gunnar Wagenknecht and others.
2+
* Copyright (c) 2020, 2023 Gunnar Wagenknecht and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -13,8 +13,6 @@
1313
*******************************************************************************/
1414
package org.eclipse.jdt.internal.launching;
1515

16-
import static org.eclipse.jdt.internal.launching.LaunchingPlugin.LAUNCH_TEMP_FILE_PREFIX;
17-
1816
import java.io.File;
1917
import java.io.IOException;
2018
import java.nio.charset.CharacterCodingException;
@@ -30,7 +28,6 @@
3028
import org.eclipse.core.runtime.IStatus;
3129
import org.eclipse.core.runtime.Platform;
3230
import org.eclipse.core.runtime.Status;
33-
import org.eclipse.debug.core.DebugPlugin;
3431
import org.eclipse.debug.core.ILaunch;
3532
import org.eclipse.debug.core.ILaunchConfiguration;
3633
import org.eclipse.jdt.core.JavaCore;
@@ -94,10 +91,7 @@ protected void addProcessTempFile(File file) {
9491
protected File createArgumentFile(String[] cmdLine) throws CoreException {
9592
Charset systemCharset = Platform.getSystemCharset();
9693
try {
97-
String timeStamp = getLaunchTimeStamp();
98-
File argumentsFile = new File(processTempFilesDir, String.format(LAUNCH_TEMP_FILE_PREFIX
99-
+ "%s-args-%s.txt", getLaunchConfigurationName(), timeStamp)); //$NON-NLS-1$
100-
94+
File argumentsFile = JavaLaunchingUtils.createFileForArgument(getLaunchTimeStamp(), processTempFilesDir, getLaunchConfigurationName(), "%s-args-%s.txt"); //$NON-NLS-1$
10195
cmdLine = quoteForArgfile(cmdLine);
10296

10397
Files.write(argumentsFile.toPath(), Arrays.asList(cmdLine), systemCharset);
@@ -146,11 +140,7 @@ protected String getLaunchConfigurationName() {
146140
}
147141

148142
protected String getLaunchTimeStamp() {
149-
String timeStamp = launch.getAttribute(DebugPlugin.ATTR_LAUNCH_TIMESTAMP);
150-
if (timeStamp == null) {
151-
timeStamp = Long.toString(System.currentTimeMillis());
152-
}
153-
return timeStamp;
143+
return JavaLaunchingUtils.getLaunchTimeStamp(launch);
154144
}
155145

156146
@Override
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2023 IBM Corporation and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* IBM Corporation - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.jdt.internal.launching;
15+
16+
import java.io.File;
17+
18+
import org.eclipse.debug.core.DebugPlugin;
19+
import org.eclipse.debug.core.ILaunch;
20+
21+
/**
22+
* A Utilities class.
23+
*
24+
*/
25+
public class JavaLaunchingUtils {
26+
27+
28+
/**
29+
* Returns the argument file for a launch within the given temp directory
30+
*
31+
* @param launch
32+
* The launch
33+
* @param processTempFilesDir
34+
* The processing file directory
35+
* @param optionalString
36+
* Optional string to be formatted and used for creating file
37+
* @return Created file
38+
*/
39+
public static File createFileForArgument(String timeStamp, File processTempFilesDir, String launchConfigName, String optionalString) {
40+
String child = String.format(org.eclipse.jdt.internal.launching.LaunchingPlugin.LAUNCH_TEMP_FILE_PREFIX
41+
+ optionalString, launchConfigName, timeStamp);
42+
File argumentsFile = new File(processTempFilesDir, child);
43+
return argumentsFile;
44+
}
45+
46+
public static String getLaunchTimeStamp(ILaunch launch) {
47+
String timeStamp = launch.getAttribute(DebugPlugin.ATTR_LAUNCH_TIMESTAMP);
48+
if (timeStamp == null) {
49+
timeStamp = Long.toString(System.currentTimeMillis());
50+
}
51+
return timeStamp;
52+
}
53+
54+
}

0 commit comments

Comments
 (0)