Skip to content

Commit ab72ced

Browse files
ahornaceVladimir Kotal
authored andcommitted
Replace Java 9 deprecated calls with appropriate substitutes (#1840)
1 parent a6dc5de commit ab72ced

File tree

10 files changed

+57
-38
lines changed

10 files changed

+57
-38
lines changed

src/org/opensolaris/opengrok/analysis/AnalyzerGuru.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.Reader;
3232
import java.io.StringReader;
3333
import java.io.Writer;
34+
import java.lang.reflect.InvocationTargetException;
3435
import java.util.ArrayList;
3536
import java.util.Arrays;
3637
import java.util.Comparator;
@@ -512,16 +513,18 @@ public static Genre getGenre(FileAnalyzerFactory factory) {
512513
* FileAnalyzerFactory}
513514
* @throws IllegalAccessException if the constructor cannot be accessed
514515
* @throws InstantiationException if the class cannot be instantiated
516+
* @throws NoSuchMethodException if no-argument constructor could not be found
517+
* @throws InvocationTargetException if the underlying constructor throws an exception
515518
*/
516519
public static FileAnalyzerFactory findFactory(String factoryClassName)
517-
throws ClassNotFoundException, IllegalAccessException,
518-
InstantiationException {
519-
Class fcn = null;
520+
throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException,
521+
InvocationTargetException {
522+
Class<?> fcn = null;
520523
try {
521524
fcn = Class.forName(factoryClassName);
522525

523526
} catch (ClassNotFoundException e) {
524-
fcn = getFactoryClass(factoryClassName);
527+
fcn = getFactoryClass(factoryClassName);
525528

526529
if (fcn == null) {
527530
throw new ClassNotFoundException("Unable to locate class " + factoryClassName);
@@ -540,9 +543,8 @@ public static FileAnalyzerFactory findFactory(String factoryClassName)
540543
*
541544
* @return the analyzer factory class, or null when not found.
542545
*/
543-
public static Class getFactoryClass(String simpleName) {
544-
//return new AnalyzerGuru().getClass().getPackage().getName();
545-
Class factoryClass = null;
546+
public static Class<?> getFactoryClass(String simpleName) {
547+
Class<?> factoryClass = null;
546548

547549
// Build analysis package name list first time only
548550
if (analysisPkgNames.isEmpty()) {
@@ -593,16 +595,17 @@ public static Class getFactoryClass(String simpleName) {
593595
* FileAnalyzerFactory}
594596
* @throws IllegalAccessException if the constructor cannot be accessed
595597
* @throws InstantiationException if the class cannot be instantiated
598+
* @throws NoSuchMethodException if no-argument constructor could not be found
599+
* @throws InvocationTargetException if the underlying constructor throws an exception
596600
*/
597601
private static FileAnalyzerFactory findFactory(Class<?> factoryClass)
598-
throws InstantiationException, IllegalAccessException {
602+
throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
599603
for (FileAnalyzerFactory f : factories) {
600604
if (f.getClass() == factoryClass) {
601605
return f;
602606
}
603607
}
604-
FileAnalyzerFactory f
605-
= (FileAnalyzerFactory) factoryClass.newInstance();
608+
FileAnalyzerFactory f = (FileAnalyzerFactory) factoryClass.getDeclaredConstructor().newInstance();
606609
registerAnalyzer(f);
607610
return f;
608611
}

src/org/opensolaris/opengrok/authorization/AuthorizationFramework.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.io.File;
2626
import java.io.IOException;
27+
import java.lang.reflect.InvocationTargetException;
2728
import java.lang.reflect.Modifier;
2829
import java.security.AccessController;
2930
import java.security.PrivilegedAction;
@@ -395,20 +396,24 @@ public IAuthorizationPlugin handleLoadClass(String classname) {
395396
* instance of that class
396397
* @throws IllegalAccessException when the constructor of the class is not
397398
* accessible
399+
* @throws NoSuchMethodException when the class does not have no-argument constructor
400+
* @throws InvocationTargetException if the underlying constructor of the class throws an exception
398401
*/
399402
private IAuthorizationPlugin loadClass(String classname) throws ClassNotFoundException,
400403
SecurityException,
401404
InstantiationException,
402-
IllegalAccessException {
405+
IllegalAccessException,
406+
NoSuchMethodException,
407+
InvocationTargetException {
403408

404-
Class c = loader.loadClass(classname);
409+
Class<?> c = loader.loadClass(classname);
405410

406411
// check for implemented interfaces
407412
for (Class intf1 : getInterfaces(c)) {
408413
if (intf1.getCanonicalName().equals(IAuthorizationPlugin.class.getCanonicalName())
409414
&& !Modifier.isAbstract(c.getModifiers())) {
410415
// call to non-parametric constructor
411-
return (IAuthorizationPlugin) c.newInstance();
416+
return (IAuthorizationPlugin) c.getDeclaredConstructor().newInstance();
412417
}
413418
}
414419
LOGGER.log(Level.FINEST, "Plugin class \"{0}\" does not implement IAuthorizationPlugin interface.", classname);

src/org/opensolaris/opengrok/authorization/AuthorizationPlugin.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ public boolean setPlugin(IAuthorizationPlugin plugin) {
215215
* the whole stack walk through and prevent the other authorization
216216
* entities to work properly.
217217
*/
218-
return (this.plugin = plugin.getClass().newInstance()) != null;
218+
this.plugin = plugin.getClass().getDeclaredConstructor().newInstance();
219+
return true;
219220
} catch (InstantiationException ex) {
220221
LOGGER.log(Level.INFO, "Class could not be instantiated: ", ex);
221222
} catch (IllegalAccessException ex) {

src/org/opensolaris/opengrok/authorization/AuthorizationPluginClassLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ private void checkPackage(String name) throws SecurityException {
192192
* @throws SecurityException if the loader cannot access the class
193193
*/
194194
@Override
195-
public Class loadClass(String name) throws ClassNotFoundException, SecurityException {
195+
public Class<?> loadClass(String name) throws ClassNotFoundException, SecurityException {
196196
return loadClass(name, true);
197197
}
198198

src/org/opensolaris/opengrok/configuration/messages/Message.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ public static Message createMessage(String type) {
119119
classname += type.substring(1) + "Message";
120120

121121
try {
122-
Class concreteClass = Class.forName(classname);
123-
return (Message) concreteClass.newInstance();
122+
Class<?> concreteClass = Class.forName(classname);
123+
return (Message) concreteClass.getDeclaredConstructor().newInstance();
124124
} catch (Throwable ex) {
125125
LOGGER.log(Level.WARNING, "Couldn't create message object of type \"{0}\".", type);
126126
}

src/org/opensolaris/opengrok/history/HistoryGuru.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.File;
2626
import java.io.IOException;
2727
import java.io.InputStream;
28+
import java.lang.reflect.InvocationTargetException;
2829
import java.nio.file.Path;
2930
import java.util.ArrayList;
3031
import java.util.Collection;
@@ -369,9 +370,9 @@ private Collection<RepositoryInfo> addRepositories(File[] files,
369370
Repository repository = null;
370371
try {
371372
repository = RepositoryFactory.getRepository(file);
372-
} catch (InstantiationException ie) {
373+
} catch (InstantiationException | NoSuchMethodException | InvocationTargetException e) {
373374
LOGGER.log(Level.WARNING, "Could not create repository for '"
374-
+ file + "', could not instantiate the repository.", ie);
375+
+ file + "', could not instantiate the repository.", e);
375376
} catch (IllegalAccessException iae) {
376377
LOGGER.log(Level.WARNING, "Could not create repository for '"
377378
+ file + "', missing access rights.", iae);

src/org/opensolaris/opengrok/history/RepositoryFactory.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.io.File;
2626
import java.io.IOException;
27+
import java.lang.reflect.InvocationTargetException;
2728
import java.util.ArrayList;
2829
import java.util.List;
2930
import java.util.logging.Level;
@@ -88,18 +89,19 @@ public static List<Class<? extends Repository>> getRepositoryClasses() {
8889
*
8990
* @param file File that might contain a repository
9091
* @return Correct repository for the given file
91-
* @throws java.lang.InstantiationException in case we cannot create the
92-
* repository object
93-
* @throws java.lang.IllegalAccessException in case no permissions
94-
* to repository file
92+
* @throws InstantiationException in case we cannot create the repository object
93+
* @throws IllegalAccessException in case no permissions to repository file
94+
* @throws NoSuchMethodException in case we cannot create the repository object
95+
* @throws InvocationTargetException in case we cannot create the repository object
9596
*/
96-
public static Repository getRepository(File file) throws InstantiationException, IllegalAccessException {
97+
public static Repository getRepository(File file) throws InstantiationException, IllegalAccessException,
98+
NoSuchMethodException, InvocationTargetException {
9799
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
98100
Repository repo = null;
99101

100102
for (Repository rep : repositories) {
101103
if (rep.isRepositoryFor(file)) {
102-
repo = rep.getClass().newInstance();
104+
repo = rep.getClass().getDeclaredConstructor().newInstance();
103105
try {
104106
repo.setDirectoryName(file.getCanonicalPath());
105107
} catch (IOException e) {
@@ -170,11 +172,13 @@ public static Repository getRepository(File file) throws InstantiationException,
170172
*
171173
* @param info Information about the repository
172174
* @return Correct repository for the given file
173-
* @throws java.lang.InstantiationException in case we cannot create the
174-
* repository object
175-
* @throws java.lang.IllegalAccessException in case no permissions to repository
175+
* @throws InstantiationException in case we cannot create the repository object
176+
* @throws IllegalAccessException in case no permissions to repository
177+
* @throws NoSuchMethodException in case we cannot create the repository object
178+
* @throws InvocationTargetException in case we cannot create the repository object
176179
*/
177-
public static Repository getRepository(RepositoryInfo info) throws InstantiationException, IllegalAccessException {
180+
public static Repository getRepository(RepositoryInfo info) throws InstantiationException, IllegalAccessException,
181+
NoSuchMethodException, InvocationTargetException {
178182
return getRepository(new File(info.getDirectoryName()));
179183
}
180184

src/org/opensolaris/opengrok/index/Indexer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.File;
2828
import java.io.IOException;
2929
import java.lang.reflect.Field;
30+
import java.lang.reflect.InvocationTargetException;
3031
import java.nio.file.Paths;
3132
import java.text.ParseException;
3233
import java.util.ArrayList;
@@ -754,7 +755,8 @@ private static void configureFileAnalyzer(String fileSpec, String analyzer) {
754755
AnalyzerGuru.findFactory(analyzer));
755756
}
756757

757-
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
758+
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException | NoSuchMethodException
759+
| InvocationTargetException e) {
758760
LOGGER.log(Level.SEVERE, "Unable to locate FileAnalyzerFactory for {0}", analyzer);
759761
LOGGER.log(Level.SEVERE, "Stack: ", e.fillInStackTrace());
760762
System.exit(1);

src/org/opensolaris/opengrok/logger/LoggerUtil.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.io.File;
2929
import java.io.IOException;
30+
import java.lang.reflect.InvocationTargetException;
3031
import java.util.logging.ConsoleHandler;
3132
import java.util.logging.FileHandler;
3233
import java.util.logging.Formatter;
@@ -117,8 +118,9 @@ public static void setFileHandlerLogPath(String path) throws IOException {
117118
String formatter = LogManager.getLogManager().getProperty("java.util.logging.FileHandler.formatter");
118119
newFileHandler.setLevel(fileHandler.getLevel());
119120
try {
120-
newFileHandler.setFormatter((Formatter) Class.forName(formatter).newInstance());
121-
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
121+
newFileHandler.setFormatter((Formatter) Class.forName(formatter).getDeclaredConstructor().newInstance());
122+
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | NoSuchMethodException
123+
| InvocationTargetException e) {
122124
newFileHandler.setFormatter(new FileLogFormatter());
123125
}
124126
getBaseLogger().addHandler(newFileHandler);
@@ -169,8 +171,9 @@ public static String initLogger(String logpath, Level filelevel, Level consolele
169171
fh.setLevel(filelevel);
170172
String formatter = LogManager.getLogManager().getProperty("java.util.logging.FileHandler.formatter");
171173
try {
172-
fh.setFormatter((Formatter) Class.forName(formatter).newInstance());
173-
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
174+
fh.setFormatter((Formatter) Class.forName(formatter).getDeclaredConstructor().newInstance());
175+
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | NoSuchMethodException
176+
| InvocationTargetException e) {
174177
fh.setFormatter(new FileLogFormatter());
175178
}
176179

src/org/opensolaris/opengrok/web/Statistics.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ synchronized protected void maybeRefresh() {
103103
synchronized public void addRequest(String category) {
104104
Long val = requestCategories.get(category);
105105
if (val == null) {
106-
val = new Long(0);
106+
val = 0L;
107107
}
108108
val += 1;
109109
requestCategories.put(category, val);
@@ -122,13 +122,13 @@ synchronized public void addRequestTime(String category, long v) {
122122
Long max = timingMax.get(category);
123123

124124
if (val == null) {
125-
val = new Long(0);
125+
val = 0L;
126126
}
127127
if (max == null || v > max) {
128-
max = new Long(v);
128+
max = v;
129129
}
130130
if (min == null || v < min) {
131-
min = new Long(v);
131+
min = v;
132132
}
133133

134134
val += v;

0 commit comments

Comments
 (0)