Skip to content

Commit dbfad1b

Browse files
committed
Automatic code improvements for Java 8
1 parent cfcd175 commit dbfad1b

20 files changed

+114
-198
lines changed

src/main/java/org/codehaus/classworlds/ClassWorldAdapter.java

+6-14
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.util.Collection;
2020
import java.util.Vector;
21-
import java.util.Iterator;
2221

2322
/**
2423
* An adapter for ClassWorlds
@@ -32,9 +31,7 @@ public class ClassWorldAdapter
3231

3332
public static ClassWorldAdapter getInstance( org.codehaus.plexus.classworlds.ClassWorld newWorld )
3433
{
35-
ClassWorldAdapter adapter = new ClassWorldAdapter( newWorld );
36-
37-
return adapter;
34+
return new ClassWorldAdapter( newWorld );
3835
}
3936

4037
private org.codehaus.plexus.classworlds.ClassWorld world;
@@ -64,8 +61,7 @@ public ClassRealm newRealm( String id,
6461
{
6562
try
6663
{
67-
return ClassRealmAdapter.getInstance( world.newRealm( id,
68-
classLoader ) );
64+
return ClassRealmAdapter.getInstance( world.newRealm( id, classLoader ) );
6965
}
7066
catch ( org.codehaus.plexus.classworlds.realm.DuplicateRealmException e )
7167
{
@@ -101,15 +97,11 @@ public ClassRealm getRealm( String id )
10197

10298
public Collection getRealms()
10399
{
104-
Collection realms = world.getRealms();
105-
Vector ret = new Vector();
106-
107-
Iterator it = realms.iterator();
108-
while ( it.hasNext() )
100+
Collection<org.codehaus.plexus.classworlds.realm.ClassRealm> realms = world.getRealms();
101+
Vector<ClassRealmAdapter> ret = new Vector<>();
102+
for ( org.codehaus.plexus.classworlds.realm.ClassRealm classRealm : realms )
109103
{
110-
org.codehaus.plexus.classworlds.realm.ClassRealm realm =
111-
(org.codehaus.plexus.classworlds.realm.ClassRealm) it.next();
112-
ret.add( ClassRealmAdapter.getInstance( realm ) );
104+
ret.add( ClassRealmAdapter.getInstance( classRealm ) );
113105
}
114106

115107
return ret;

src/main/java/org/codehaus/classworlds/ClassWorldReverseAdapter.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.HashMap;
2020
import java.util.Collection;
2121
import java.util.Vector;
22-
import java.util.Iterator;
2322

2423
/**
2524
* A reverse adapter for ClassWorlds
@@ -110,10 +109,9 @@ public Collection getRealms()
110109
Collection realms = world.getRealms();
111110
Vector ret = new Vector();
112111

113-
Iterator it = realms.iterator();
114-
while ( it.hasNext() )
112+
for ( Object o : realms )
115113
{
116-
ClassRealm realm = (ClassRealm) it.next();
114+
ClassRealm realm = (ClassRealm) o;
117115
ret.add( ClassRealmReverseAdapter.getInstance( realm ) );
118116
}
119117

src/main/java/org/codehaus/classworlds/DefaultClassRealm.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616
* limitations under the License.
1717
*/
1818

19-
/**
19+
/*
2020
* A compatibility wrapper for org.codehaus.plexus.classworlds.realm.ClassRealm
2121
* provided for legacy code
2222
*
2323
* @author Andrew Williams
24-
* @version $Id$
2524
*/
2625

2726
import java.io.File;

src/main/java/org/codehaus/plexus/classworlds/ClassWorld.java

+12-23
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
* limitations under the License.
1717
*/
1818

19-
import java.io.Closeable;
2019
import java.io.IOException;
2120
import java.util.ArrayList;
2221
import java.util.Collection;
@@ -38,7 +37,7 @@ public class ClassWorld
3837
{
3938
private Map<String, ClassRealm> realms;
4039

41-
private final List<ClassWorldListener> listeners = new ArrayList<ClassWorldListener>();
40+
private final List<ClassWorldListener> listeners = new ArrayList<>();
4241

4342
public ClassWorld( String realmId, ClassLoader classLoader )
4443
{
@@ -56,7 +55,7 @@ public ClassWorld( String realmId, ClassLoader classLoader )
5655

5756
public ClassWorld()
5857
{
59-
this.realms = new LinkedHashMap<String, ClassRealm>();
58+
this.realms = new LinkedHashMap<>();
6059
}
6160

6261
public ClassRealm newRealm( String id )
@@ -90,31 +89,21 @@ public synchronized ClassRealm newRealm( String id, ClassLoader classLoader )
9089
public synchronized void disposeRealm( String id )
9190
throws NoSuchRealmException
9291
{
93-
ClassRealm realm = (ClassRealm) realms.remove( id );
92+
ClassRealm realm = realms.remove( id );
9493

9594
if ( realm != null )
9695
{
97-
closeIfJava7( realm );
98-
for ( ClassWorldListener listener : listeners )
96+
try
9997
{
100-
listener.realmDisposed( realm );
98+
realm.close();
10199
}
102-
}
103-
}
104-
105-
private void closeIfJava7( ClassRealm realm )
106-
{
107-
try
108-
{
109-
//noinspection ConstantConditions
110-
if ( realm instanceof Closeable )
100+
catch ( IOException ignore )
111101
{
112-
//noinspection RedundantCast
113-
( (Closeable) realm ).close();
114102
}
115-
}
116-
catch ( IOException ignore )
117-
{
103+
for ( ClassWorldListener listener : listeners )
104+
{
105+
listener.realmDisposed( realm );
106+
}
118107
}
119108
}
120109

@@ -123,15 +112,15 @@ public synchronized ClassRealm getRealm( String id )
123112
{
124113
if ( realms.containsKey( id ) )
125114
{
126-
return (ClassRealm) realms.get( id );
115+
return realms.get( id );
127116
}
128117

129118
throw new NoSuchRealmException( this, id );
130119
}
131120

132121
public synchronized Collection<ClassRealm> getRealms()
133122
{
134-
return Collections.unmodifiableList( new ArrayList<ClassRealm>( realms.values() ) );
123+
return Collections.unmodifiableList( new ArrayList<>( realms.values() ) );
135124
}
136125

137126
// from exports branch

src/main/java/org/codehaus/plexus/classworlds/ClassWorldListener.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
public interface ClassWorldListener
2222
{
23-
public void realmCreated( ClassRealm realm );
23+
void realmCreated( ClassRealm realm );
2424

25-
public void realmDisposed( ClassRealm realm );
25+
void realmDisposed( ClassRealm realm );
2626
}

src/main/java/org/codehaus/plexus/classworlds/UrlUtils.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.net.URL;
2020
import java.net.URLClassLoader;
21+
import java.util.Arrays;
2122
import java.util.HashSet;
2223
import java.util.Set;
2324

@@ -56,13 +57,6 @@ public static String normalizeUrlPath( String name )
5657

5758
public static Set<URL> getURLs( URLClassLoader loader )
5859
{
59-
Set<URL> ret = new HashSet<URL>();
60-
61-
for ( URL url : loader.getURLs() )
62-
{
63-
ret.add( url );
64-
}
65-
66-
return ret;
60+
return new HashSet<>( Arrays.asList( loader.getURLs() ) );
6761
}
6862
}

src/main/java/org/codehaus/plexus/classworlds/launcher/ConfigurationParser.java

+26-30
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818

1919
import java.io.BufferedReader;
2020
import java.io.File;
21-
import java.io.FileInputStream;
2221
import java.io.FileNotFoundException;
23-
import java.io.FilenameFilter;
2422
import java.io.IOException;
2523
import java.io.InputStream;
2624
import java.io.InputStreamReader;
2725
import java.net.MalformedURLException;
2826
import java.net.URL;
27+
import java.nio.charset.StandardCharsets;
28+
import java.nio.file.Files;
29+
import java.nio.file.Paths;
2930
import java.util.Properties;
3031

3132
import org.codehaus.plexus.classworlds.realm.DuplicateRealmException;
@@ -76,9 +77,9 @@ public ConfigurationParser( ConfigurationHandler handler, Properties systemPrope
7677
public void parse( InputStream is )
7778
throws IOException, ConfigurationException, DuplicateRealmException, NoSuchRealmException
7879
{
79-
BufferedReader reader = new BufferedReader( new InputStreamReader( is, "UTF-8" ) );
80+
BufferedReader reader = new BufferedReader( new InputStreamReader( is, StandardCharsets.UTF_8 ) );
8081

81-
String line = null;
82+
String line;
8283

8384
int lineNo = 0;
8485

@@ -181,7 +182,7 @@ else if ( line.startsWith( SET_PREFIX ) )
181182

182183
try
183184
{
184-
properties.load( new FileInputStream( propertiesFileName ) );
185+
properties.load( Files.newInputStream( Paths.get( propertiesFileName ) ) );
185186

186187
value = properties.getProperty( property );
187188
}
@@ -247,7 +248,7 @@ else if ( line.startsWith( LOAD_PREFIX ) )
247248

248249
constituent = filter( constituent );
249250

250-
if ( constituent.indexOf( "*" ) >= 0 )
251+
if ( constituent.contains( "*" ) )
251252
{
252253
loadGlob( constituent, false /*not optionally*/ );
253254
}
@@ -278,7 +279,7 @@ else if ( line.startsWith( OPTIONALLY_PREFIX ) )
278279

279280
constituent = filter( constituent );
280281

281-
if ( constituent.indexOf( "*" ) >= 0 )
282+
if ( constituent.contains( "*" ) )
282283
{
283284
loadGlob( constituent, true /*optionally*/ );
284285
}
@@ -350,23 +351,18 @@ protected void loadGlob( String line,
350351

351352
final String suffix = localName.substring( starLoc + 1 );
352353

353-
File[] matches = dir.listFiles( new FilenameFilter()
354-
{
355-
public boolean accept( File dir,
356-
String name )
354+
File[] matches = dir.listFiles( ( dir1, name ) -> {
355+
if ( !name.startsWith( prefix ) )
357356
{
358-
if ( !name.startsWith( prefix ) )
359-
{
360-
return false;
361-
}
362-
363-
if ( !name.endsWith( suffix ) )
364-
{
365-
return false;
366-
}
357+
return false;
358+
}
367359

368-
return true;
360+
if ( !name.endsWith( suffix ) )
361+
{
362+
return false;
369363
}
364+
365+
return true;
370366
} );
371367

372368
for ( File match : matches )
@@ -386,16 +382,16 @@ public boolean accept( File dir,
386382
protected String filter( String text )
387383
throws ConfigurationException
388384
{
389-
String result = "";
385+
StringBuilder result = new StringBuilder();
390386

391387
int cur = 0;
392388
int textLen = text.length();
393389

394-
int propStart = -1;
395-
int propStop = -1;
390+
int propStart;
391+
int propStop;
396392

397-
String propName = null;
398-
String propValue = null;
393+
String propName;
394+
String propValue;
399395

400396
while ( cur < textLen )
401397
{
@@ -406,7 +402,7 @@ protected String filter( String text )
406402
break;
407403
}
408404

409-
result += text.substring( cur, propStart );
405+
result.append( text, cur, propStart );
410406

411407
propStop = text.indexOf( "}", propStart );
412408

@@ -430,14 +426,14 @@ protected String filter( String text )
430426
{
431427
throw new ConfigurationException( "No such property: " + propName );
432428
}
433-
result += propValue;
429+
result.append( propValue );
434430

435431
cur = propStop + 1;
436432
}
437433

438-
result += text.substring( cur );
434+
result.append( text.substring( cur ) );
439435

440-
return result;
436+
return result.toString();
441437
}
442438

443439
/**

src/main/java/org/codehaus/plexus/classworlds/launcher/Configurator.java

+4-15
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import java.net.MalformedURLException;
2323
import java.net.URL;
2424
import java.util.ArrayList;
25-
import java.util.Collections;
26-
import java.util.Comparator;
2725
import java.util.HashMap;
2826
import java.util.List;
2927
import java.util.Map;
@@ -69,7 +67,7 @@ public Configurator( Launcher launcher )
6967
{
7068
this.launcher = launcher;
7169

72-
configuredRealms = new HashMap<String, ClassRealm>();
70+
configuredRealms = new HashMap<>();
7371

7472
if ( launcher != null )
7573
{
@@ -97,7 +95,7 @@ public void setClassWorld( ClassWorld world )
9795
{
9896
this.world = world;
9997

100-
configuredRealms = new HashMap<String, ClassRealm>();
98+
configuredRealms = new HashMap<>();
10199
}
102100

103101
/**
@@ -148,19 +146,10 @@ public void configure( InputStream is )
148146
*/
149147
public void associateRealms()
150148
{
151-
List<String> sortRealmNames = new ArrayList<String>( configuredRealms.keySet() );
149+
List<String> sortRealmNames = new ArrayList<>( configuredRealms.keySet() );
152150

153151
// sort by name
154-
Comparator<String> comparator = new Comparator<String>()
155-
{
156-
public int compare( String g1,
157-
String g2 )
158-
{
159-
return g1.compareTo( g2 );
160-
}
161-
};
162-
163-
Collections.sort( sortRealmNames, comparator );
152+
sortRealmNames.sort( String::compareTo );
164153

165154
// So now we have something like the following for defined
166155
// realms:

0 commit comments

Comments
 (0)