49
49
import org .apache .tools .ant .Task ;
50
50
import org .apache .tools .ant .TaskAdapter ;
51
51
import org .apache .tools .ant .UnknownElement ;
52
+ import org .apache .tools .ant .util .JavaEnvUtils ;
52
53
import org .eclipse .ant .core .AntCorePlugin ;
53
54
import org .eclipse .ant .core .AntCorePreferences ;
54
55
import org .eclipse .ant .core .AntSecurityException ;
89
90
@ SuppressWarnings ("removal" ) // SecurityManager
90
91
public class AntModel implements IAntModel {
91
92
93
+ private static final boolean IS_SECURITY_MANAGER_SUPPORTED = isSecurityManagerAllowed ();
94
+
95
+ private static boolean isSecurityManagerAllowed () {
96
+ String sm = System .getProperty ("java.security.manager" ); //$NON-NLS-1$
97
+ if (sm == null ) { // default is 'disallow' since 18 and was 'allow' before
98
+ return !JavaEnvUtils .isAtLeastJavaVersion ("18" ); //$NON-NLS-1$
99
+ }
100
+ // Value is either 'disallow' or 'allow' or specifies the SecurityManager class to set
101
+ return !"disallow" .equals (sm ); //$NON-NLS-1$
102
+ }
103
+
92
104
private static ClassLoader fgClassLoader ;
93
105
private static int fgInstanceCount = 0 ;
94
106
private static Object loaderLock = new Object ();
@@ -363,8 +375,10 @@ private void parseDocument(IDocument input) {
363
375
SecurityManager origSM = System .getSecurityManager ();
364
376
processAntHome (true );
365
377
try {
366
- // set a security manager to disallow system exit and system property setting
367
- System .setSecurityManager (new AntSecurityManager (origSM , Thread .currentThread (), false ));
378
+ if (IS_SECURITY_MANAGER_SUPPORTED ) {
379
+ // set a security manager to disallow system exit and system property setting
380
+ System .setSecurityManager (new AntSecurityManager (origSM , Thread .currentThread (), false ));
381
+ }
368
382
resolveBuildfile ();
369
383
endReporting ();
370
384
// clear the additional property-holder(s) to avoid potential memory leaks
@@ -379,7 +393,9 @@ private void parseDocument(IDocument input) {
379
393
finally {
380
394
Thread .currentThread ().setContextClassLoader (originalClassLoader );
381
395
getClassLoader (null );
382
- System .setSecurityManager (origSM );
396
+ if (System .getSecurityManager () instanceof AntSecurityManager ) {
397
+ System .setSecurityManager (origSM );
398
+ }
383
399
project .fireBuildFinished (null ); // cleanup (IntrospectionHelper)
384
400
}
385
401
}
@@ -550,9 +566,7 @@ private void setGlobalProperties(Project project) {
550
566
551
567
private void resolveBuildfile () {
552
568
Collection <AntTaskNode > nodeCopy = new ArrayList <>(fTaskNodes );
553
- Iterator <AntTaskNode > iter = nodeCopy .iterator ();
554
- while (iter .hasNext ()) {
555
- AntTaskNode node = iter .next ();
569
+ for (AntTaskNode node : nodeCopy ) {
556
570
fNodeBeingResolved = node ;
557
571
fNodeBeingResolvedIndex = -1 ;
558
572
if (node .configure (false )) {
@@ -1439,10 +1453,8 @@ private void reconcileTaskAndTypes() {
1439
1453
if (fCurrentNodeIdentifiers == null || fDefinerNodeIdentifierToDefinedTasks == null ) {
1440
1454
return ;
1441
1455
}
1442
- Iterator <String > iter = fDefinerNodeIdentifierToDefinedTasks .keySet ().iterator ();
1443
1456
ComponentHelper helper = ComponentHelper .getComponentHelper (fProjectNode .getProject ());
1444
- while (iter .hasNext ()) {
1445
- String key = iter .next ();
1457
+ for (String key : fDefinerNodeIdentifierToDefinedTasks .keySet ()) {
1446
1458
if (fCurrentNodeIdentifiers .get (key ) == null ) {
1447
1459
removeDefinerTasks (key , helper .getAntTypeTable ());
1448
1460
}
@@ -1579,9 +1591,7 @@ public AntElementNode getReferenceNode(String text) {
1579
1591
}
1580
1592
1581
1593
Set <Task > nodes = fTaskToNode .keySet ();
1582
- Iterator <Task > iter = nodes .iterator ();
1583
- while (iter .hasNext ()) {
1584
- Task task = iter .next ();
1594
+ for (Task task : nodes ) {
1585
1595
Task tmptask = task ;
1586
1596
if (tmptask instanceof UnknownElement ) {
1587
1597
UnknownElement element = (UnknownElement ) tmptask ;
@@ -1720,9 +1730,7 @@ protected void addDefinedTasks(List<String> newTasks, AntDefiningTaskNode node)
1720
1730
fCurrentNodeIdentifiers .remove (identifier );
1721
1731
}
1722
1732
fDefinerNodeIdentifierToDefinedTasks .put (identifier , newTasks );
1723
- Iterator <String > iter = newTasks .iterator ();
1724
- while (iter .hasNext ()) {
1725
- String name = iter .next ();
1733
+ for (String name : newTasks ) {
1726
1734
fTaskNameToDefiningNode .put (name , node );
1727
1735
}
1728
1736
}
@@ -1800,9 +1808,7 @@ private String getPrefixMapping(String prefix) {
1800
1808
private String getUserPrefixMapping (String prefix ) {
1801
1809
if (fNamespacePrefixMappings != null ) {
1802
1810
Set <Entry <String , String >> entrySet = fNamespacePrefixMappings .entrySet ();
1803
- Iterator <Entry <String , String >> entries = entrySet .iterator ();
1804
- while (entries .hasNext ()) {
1805
- Map .Entry <String , String > entry = entries .next ();
1811
+ for (Entry <String , String > entry : entrySet ) {
1806
1812
if (entry .getValue ().equals (prefix )) {
1807
1813
return entry .getKey ();
1808
1814
}
0 commit comments