11/*
2- * Copyright (c) 2013, 2022 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2013, 2023 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2323
2424package jdk .test .lib .process ;
2525
26+ import jdk .test .lib .compiler .CompilerUtils ;
27+
2628import java .io .BufferedReader ;
2729import java .io .File ;
2830import java .io .IOException ;
@@ -118,7 +120,11 @@ public class Proc {
118120 private boolean inheritIO = false ;
119121 private boolean noDump = false ;
120122
121- private List <String > cp ; // user-provided classpath
123+ private boolean addcp ; // user-provided classpath is appended
124+ private List <String > cp ; // user-provided classpaths
125+
126+ private boolean compile ; // compile the program as well
127+
122128 private String clazz ; // Class to launch
123129 private String debug ; // debug flag, controller will show data
124130 // transfer between procs. If debug is set,
@@ -195,15 +201,20 @@ public Proc inheritProp(String k) {
195201 }
196202 return this ;
197203 }
198- // Sets classpath. If not called, Proc will choose a classpath. If called
199- // with no arg, no classpath will be used. Can be called multiple times.
204+ // Sets classpath. Can be called multiple times.
200205 public Proc cp (String ... s ) {
201206 if (cp == null ) {
202207 cp = new ArrayList <>();
203208 }
204209 cp .addAll (Arrays .asList (s ));
205210 return this ;
206211 }
212+ // Adds classpath to defaults. Can be called multiple times.
213+ // Once called, addcp is always true.
214+ public Proc addcp (String ... s ) {
215+ addcp = true ;
216+ return cp (s );
217+ }
207218 // Adds a permission to policy. Can be called multiple times.
208219 // All perm() calls after a series of grant() calls are grouped into
209220 // a single grant block. perm() calls before any grant() call are grouped
@@ -259,6 +270,34 @@ public Proc grant(String v) {
259270 grant .append (v ).append (", " );
260271 return this ;
261272 }
273+ // Compile as well
274+ public Proc compile () {
275+ compile = true ;
276+ return this ;
277+ }
278+
279+ // get full classpath.
280+ // 1. Default classpath used if neither cp() or addcp() is called
281+ // 2. User provided classpath (can be empty) used if only cp() is called
282+ // 3. User provided classpath + default classpath used, otherwise
283+ String fullcp () {
284+ if (cp == null ) {
285+ return System .getProperty ("test.class.path" ) + File .pathSeparator +
286+ System .getProperty ("test.src.path" );
287+ } else {
288+ var newcp = new ArrayList <>(cp );
289+ if (addcp ) {
290+ newcp .add (System .getProperty ("test.class.path" ));
291+ newcp .add (System .getProperty ("test.src.path" ));
292+ }
293+ if (!newcp .isEmpty ()) {
294+ return newcp .stream ().collect (Collectors .joining (File .pathSeparator ));
295+ } else {
296+ return null ;
297+ }
298+ }
299+ }
300+
262301 // Starts the proc
263302 public Proc start () throws IOException {
264303 List <String > cmd = new ArrayList <>();
@@ -282,18 +321,26 @@ public Proc start() throws IOException {
282321 }
283322 }
284323
285- Collections .addAll (cmd , splitProperty ("test.vm.opts" ));
286- Collections .addAll (cmd , splitProperty ("test.java.opts" ));
287-
288- if (cp == null ) {
289- cmd .add ("-cp" );
290- cmd .add (System .getProperty ("test.class.path" ) + File .pathSeparator +
291- System .getProperty ("test.src.path" ));
292- } else if (!cp .isEmpty ()) {
324+ var lcp = fullcp ();
325+ if (lcp != null ) {
293326 cmd .add ("-cp" );
294- cmd .add (cp . stream (). collect ( Collectors . joining ( File . pathSeparator )) );
327+ cmd .add (lcp );
295328 }
296329
330+ if (compile ) {
331+ boolean comp = CompilerUtils .compile (
332+ Path .of (System .getProperty ("test.src" ), clazz + ".java" ),
333+ Path .of (System .getProperty ("test.classes" )),
334+ cmd .subList (1 , cmd .size ()).toArray (new String [0 ]));
335+ // subList(1): all options added without launcher name
336+ if (!comp ) {
337+ throw new RuntimeException ("Compilation error" );
338+ }
339+ }
340+
341+ Collections .addAll (cmd , splitProperty ("test.vm.opts" ));
342+ Collections .addAll (cmd , splitProperty ("test.java.opts" ));
343+
297344 if (!secprop .isEmpty ()) {
298345 Path p = Path .of (getId ("security" ));
299346 try (OutputStream fos = Files .newOutputStream (p );
0 commit comments