Class.newInstance() is deprecated in Java 9+master
@@ -1122,10 +1122,11 @@ public final class IntrospectionHelper { | |||||
public void set(final Project p, final Object parent, final String value) | public void set(final Project p, final Object parent, final String value) | ||||
throws InvocationTargetException, IllegalAccessException, BuildException { | throws InvocationTargetException, IllegalAccessException, BuildException { | ||||
try { | try { | ||||
final EnumeratedAttribute ea = (EnumeratedAttribute) reflectedArg.newInstance(); | |||||
final EnumeratedAttribute ea = | |||||
(EnumeratedAttribute) reflectedArg.getDeclaredConstructor().newInstance(); | |||||
ea.setValue(value); | ea.setValue(value); | ||||
m.invoke(parent, ea); | m.invoke(parent, ea); | ||||
} catch (final InstantiationException ie) { | |||||
} catch (final InstantiationException | NoSuchMethodException ie) { | |||||
throw new BuildException(ie); | throw new BuildException(ie); | ||||
} | } | ||||
} | } | ||||
@@ -271,7 +271,7 @@ public class Project implements ResourceFactory { | |||||
public Project createSubProject() { | public Project createSubProject() { | ||||
Project subProject = null; | Project subProject = null; | ||||
try { | try { | ||||
subProject = (getClass().newInstance()); | |||||
subProject = getClass().getDeclaredConstructor().newInstance(); | |||||
} catch (final Exception e) { | } catch (final Exception e) { | ||||
subProject = new Project(); | subProject = new Project(); | ||||
} | } | ||||
@@ -1219,11 +1219,11 @@ public class Project implements ResourceFactory { | |||||
} | } | ||||
log("Attempting to create object of type " + classname, MSG_DEBUG); | log("Attempting to create object of type " + classname, MSG_DEBUG); | ||||
try { | try { | ||||
o = Class.forName(classname, true, coreLoader).newInstance(); | |||||
o = Class.forName(classname, true, coreLoader).getDeclaredConstructor().newInstance(); | |||||
} catch (final ClassNotFoundException seaEnEfEx) { | } catch (final ClassNotFoundException seaEnEfEx) { | ||||
//try the current classloader | //try the current classloader | ||||
try { | try { | ||||
o = Class.forName(classname).newInstance(); | |||||
o = Class.forName(classname).getDeclaredConstructor().newInstance(); | |||||
} catch (final Exception ex) { | } catch (final Exception ex) { | ||||
log(ex.toString(), MSG_ERR); | log(ex.toString(), MSG_ERR); | ||||
} | } | ||||
@@ -323,19 +323,17 @@ public final class SortFilter extends BaseParamFilterReader | |||||
String className = param.getValue(); | String className = param.getValue(); | ||||
@SuppressWarnings("unchecked") | @SuppressWarnings("unchecked") | ||||
final Comparator<? super String> comparatorInstance | final Comparator<? super String> comparatorInstance | ||||
= (Comparator<? super String>) (Class.forName(className).newInstance()); | |||||
= (Comparator<? super String>) (Class.forName(className).getDeclaredConstructor().newInstance()); | |||||
setComparator(comparatorInstance); | setComparator(comparatorInstance); | ||||
} catch (InstantiationException | ClassNotFoundException | IllegalAccessException e) { | |||||
/* | |||||
* IAE probably means an inner non-static class, that case is not considered | |||||
*/ | |||||
throw new BuildException(e); | |||||
} catch (ClassCastException e) { | } catch (ClassCastException e) { | ||||
throw new BuildException("Value of comparator attribute" | throw new BuildException("Value of comparator attribute" | ||||
+ " should implement" | + " should implement" | ||||
+ " java.util.Comparator" | + " java.util.Comparator" | ||||
+ " interface"); | + " interface"); | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
/* | |||||
* IAE probably means an inner non-static class, that case is not considered | |||||
*/ | |||||
throw new BuildException(e); | throw new BuildException(e); | ||||
} | } | ||||
} | } | ||||
@@ -280,7 +280,7 @@ public class Launcher { | |||||
Throwable thrown = null; | Throwable thrown = null; | ||||
try { | try { | ||||
mainClass = loader.loadClass(mainClassname).asSubclass(AntMain.class); | mainClass = loader.loadClass(mainClassname).asSubclass(AntMain.class); | ||||
final AntMain main = mainClass.newInstance(); | |||||
final AntMain main = mainClass.getDeclaredConstructor().newInstance(); | |||||
main.startAnt(newArgs, null, null); | main.startAnt(newArgs, null, null); | ||||
} catch (final InstantiationException ex) { | } catch (final InstantiationException ex) { | ||||
System.err.println( | System.err.println( | ||||
@@ -22,6 +22,7 @@ import java.io.File; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.OutputStreamWriter; | import java.io.OutputStreamWriter; | ||||
import java.io.PrintWriter; | import java.io.PrintWriter; | ||||
import java.lang.reflect.InvocationTargetException; | |||||
import java.nio.charset.StandardCharsets; | import java.nio.charset.StandardCharsets; | ||||
import java.nio.file.Files; | import java.nio.file.Files; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
@@ -318,7 +319,7 @@ public class AntStructure extends Task { | |||||
} else if (EnumeratedAttribute.class.isAssignableFrom(type)) { | } else if (EnumeratedAttribute.class.isAssignableFrom(type)) { | ||||
try { | try { | ||||
final EnumeratedAttribute ea = | final EnumeratedAttribute ea = | ||||
type.asSubclass(EnumeratedAttribute.class).newInstance(); | |||||
type.asSubclass(EnumeratedAttribute.class).getDeclaredConstructor().newInstance(); | |||||
final String[] values = ea.getValues(); | final String[] values = ea.getValues(); | ||||
if (values == null || values.length == 0 | if (values == null || values.length == 0 | ||||
|| !areNmtokens(values)) { | || !areNmtokens(values)) { | ||||
@@ -326,7 +327,8 @@ public class AntStructure extends Task { | |||||
} else { | } else { | ||||
sb.append(Stream.of(values).collect(joinAlts)).append(" "); | sb.append(Stream.of(values).collect(joinAlts)).append(" "); | ||||
} | } | ||||
} catch (final InstantiationException | IllegalAccessException ie) { | |||||
} catch (final InstantiationException | IllegalAccessException | |||||
| NoSuchMethodException | InvocationTargetException ie) { | |||||
sb.append("CDATA "); | sb.append("CDATA "); | ||||
} | } | ||||
} else if (Enum.class.isAssignableFrom(type)) { | } else if (Enum.class.isAssignableFrom(type)) { | ||||
@@ -18,6 +18,7 @@ | |||||
package org.apache.tools.ant.taskdefs; | package org.apache.tools.ant.taskdefs; | ||||
import java.lang.reflect.InvocationTargetException; | |||||
import java.sql.Connection; | import java.sql.Connection; | ||||
import java.sql.DatabaseMetaData; | import java.sql.DatabaseMetaData; | ||||
import java.sql.Driver; | import java.sql.Driver; | ||||
@@ -425,7 +426,7 @@ public abstract class JDBCTask extends Task { | |||||
Project.MSG_VERBOSE); | Project.MSG_VERBOSE); | ||||
dc = Class.forName(driver).asSubclass(Driver.class); | dc = Class.forName(driver).asSubclass(Driver.class); | ||||
} | } | ||||
driverInstance = dc.newInstance(); | |||||
driverInstance = dc.getDeclaredConstructor().newInstance(); | |||||
} catch (ClassNotFoundException e) { | } catch (ClassNotFoundException e) { | ||||
throw new BuildException( | throw new BuildException( | ||||
"Class Not Found: JDBC driver " + driver + " could not be loaded", | "Class Not Found: JDBC driver " + driver + " could not be loaded", | ||||
@@ -436,9 +437,9 @@ public abstract class JDBCTask extends Task { | |||||
"Illegal Access: JDBC driver " + driver + " could not be loaded", | "Illegal Access: JDBC driver " + driver + " could not be loaded", | ||||
e, | e, | ||||
getLocation()); | getLocation()); | ||||
} catch (InstantiationException e) { | |||||
} catch (InstantiationException | NoSuchMethodException | InvocationTargetException e) { | |||||
throw new BuildException( | throw new BuildException( | ||||
"Instantiation Exception: JDBC driver " + driver + " could not be loaded", | |||||
e.getClass().getSimpleName() + ": JDBC driver " + driver + " could not be loaded", | |||||
e, | e, | ||||
getLocation()); | getLocation()); | ||||
} | } | ||||
@@ -1014,7 +1014,7 @@ public class Tar extends MatchingTask { | |||||
.asSubclass(OutputStream.class); | .asSubclass(OutputStream.class); | ||||
Constructor<? extends OutputStream> c = | Constructor<? extends OutputStream> c = | ||||
sClazz.getConstructor(OutputStream.class, fClazz); | sClazz.getConstructor(OutputStream.class, fClazz); | ||||
return c.newInstance(ostream, oClazz.newInstance()); | |||||
return c.newInstance(ostream, oClazz.getDeclaredConstructor().newInstance()); | |||||
} catch (ClassNotFoundException ex) { | } catch (ClassNotFoundException ex) { | ||||
throw new BuildException("xz compression requires the XZ for Java library", | throw new BuildException("xz compression requires the XZ for Java library", | ||||
ex); | ex); | ||||
@@ -702,7 +702,7 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { | |||||
} else { | } else { | ||||
//anything else is a classname | //anything else is a classname | ||||
final Class<? extends XSLTLiaison> clazz = loadClass(proc).asSubclass(XSLTLiaison.class); | final Class<? extends XSLTLiaison> clazz = loadClass(proc).asSubclass(XSLTLiaison.class); | ||||
liaison = clazz.newInstance(); | |||||
liaison = clazz.getDeclaredConstructor().newInstance(); | |||||
} | } | ||||
} | } | ||||
@@ -52,7 +52,7 @@ public class Javac13 extends DefaultCompilerAdapter { | |||||
// Use reflection to be able to build on all JDKs >= 1.1: | // Use reflection to be able to build on all JDKs >= 1.1: | ||||
try { | try { | ||||
Class<?> c = Class.forName("com.sun.tools.javac.Main"); | Class<?> c = Class.forName("com.sun.tools.javac.Main"); | ||||
Object compiler = c.newInstance(); | |||||
Object compiler = c.getDeclaredConstructor().newInstance(); | |||||
Method compile = c.getMethod("compile", String[].class); | Method compile = c.getMethod("compile", String[].class); | ||||
int result = (Integer) compile.invoke(compiler, | int result = (Integer) compile.invoke(compiler, | ||||
(Object) cmd.getArguments()); | (Object) cmd.getArguments()); | ||||
@@ -151,7 +151,7 @@ public class MimeMailer extends Mailer { | |||||
try { | try { | ||||
final Provider p = | final Provider p = | ||||
Class.forName("com.sun.net.ssl.internal.ssl.Provider") | Class.forName("com.sun.net.ssl.internal.ssl.Provider") | ||||
.asSubclass(Provider.class).newInstance(); | |||||
.asSubclass(Provider.class).getDeclaredConstructor().newInstance(); | |||||
Security.addProvider(p); | Security.addProvider(p); | ||||
} catch (final Exception e) { | } catch (final Exception e) { | ||||
throw new BuildException( | throw new BuildException( | ||||
@@ -22,6 +22,7 @@ import static org.apache.tools.ant.MagicNames.ANT_VM_LAUNCHER_REF_ID; | |||||
import java.io.File; | import java.io.File; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.lang.reflect.InvocationTargetException; | |||||
import java.util.Optional; | import java.util.Optional; | ||||
import org.apache.tools.ant.Project; | import org.apache.tools.ant.Project; | ||||
@@ -173,10 +174,11 @@ public class CommandLauncher { | |||||
String launcherClass = System.getProperty(launcherRefId); | String launcherClass = System.getProperty(launcherRefId); | ||||
if (launcherClass != null) { | if (launcherClass != null) { | ||||
try { | try { | ||||
return Class.forName(launcherClass) | |||||
.asSubclass(CommandLauncher.class).newInstance(); | |||||
return Class.forName(launcherClass).asSubclass(CommandLauncher.class) | |||||
.getDeclaredConstructor().newInstance(); | |||||
} catch (InstantiationException | IllegalAccessException | } catch (InstantiationException | IllegalAccessException | ||||
| ClassNotFoundException e) { | |||||
| ClassNotFoundException | |||||
| NoSuchMethodException | InvocationTargetException e) { | |||||
System.err.println("Could not instantiate launcher class " | System.err.println("Could not instantiate launcher class " | ||||
+ launcherClass + ": " + e.getMessage()); | + launcherClass + ": " + e.getMessage()); | ||||
} | } | ||||
@@ -332,7 +332,7 @@ public class TraXLiaison implements XSLTLiaison4, ErrorListener, XSLTLoggerAware | |||||
Thread.currentThread() | Thread.currentThread() | ||||
.getContextClassLoader()); | .getContextClassLoader()); | ||||
final XSLTTraceSupport ts = | final XSLTTraceSupport ts = | ||||
(XSLTTraceSupport) traceSupport.newInstance(); | |||||
(XSLTTraceSupport) traceSupport.getDeclaredConstructor().newInstance(); | |||||
ts.configureTrace(transformer, traceConfiguration); | ts.configureTrace(transformer, traceConfiguration); | ||||
} catch (final Exception e) { | } catch (final Exception e) { | ||||
final String msg = "Failed to enable tracing because of " + e; | final String msg = "Failed to enable tracing because of " + e; | ||||
@@ -400,7 +400,7 @@ public class TraXLiaison implements XSLTLiaison4, ErrorListener, XSLTLoggerAware | |||||
if (clazz == null) { | if (clazz == null) { | ||||
clazz = Class.forName(factoryName); | clazz = Class.forName(factoryName); | ||||
} | } | ||||
tfactory = (TransformerFactory) clazz.newInstance(); | |||||
tfactory = (TransformerFactory) clazz.getDeclaredConstructor().newInstance(); | |||||
} catch (final Exception e) { | } catch (final Exception e) { | ||||
throw new BuildException(e); | throw new BuildException(e); | ||||
} | } | ||||
@@ -19,6 +19,7 @@ package org.apache.tools.ant.taskdefs.optional; | |||||
import java.io.File; | import java.io.File; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.lang.reflect.InvocationTargetException; | |||||
import java.nio.file.Files; | import java.nio.file.Files; | ||||
import java.util.Vector; | import java.util.Vector; | ||||
@@ -392,8 +393,9 @@ public class XMLValidateTask extends Task { | |||||
readerClass = Class.forName(readerClassName); | readerClass = Class.forName(readerClassName); | ||||
} | } | ||||
reader = readerClass.newInstance(); | |||||
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) { | |||||
reader = readerClass.getDeclaredConstructor().newInstance(); | |||||
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException | |||||
| NoSuchMethodException | InvocationTargetException e) { | |||||
throw new BuildException(INIT_FAILED_MSG + readerClassName, e); | throw new BuildException(INIT_FAILED_MSG + readerClassName, e); | ||||
} | } | ||||
} | } | ||||
@@ -276,9 +276,8 @@ public class GenericDeploymentTool implements EJBDeploymentTool { | |||||
try { | try { | ||||
Class<? extends DependencyAnalyzer> analyzerClass = | Class<? extends DependencyAnalyzer> analyzerClass = | ||||
Class.forName(analyzerClassName) | |||||
.asSubclass(DependencyAnalyzer.class); | |||||
dependencyAnalyzer = analyzerClass.newInstance(); | |||||
Class.forName(analyzerClassName).asSubclass(DependencyAnalyzer.class); | |||||
dependencyAnalyzer = analyzerClass.getDeclaredConstructor().newInstance(); | |||||
dependencyAnalyzer.addClassPath(new Path(task.getProject(), | dependencyAnalyzer.addClassPath(new Path(task.getProject(), | ||||
config.srcDir.getPath())); | config.srcDir.getPath())); | ||||
dependencyAnalyzer.addClassPath(config.classpath); | dependencyAnalyzer.addClassPath(config.classpath); | ||||
@@ -105,7 +105,7 @@ public final class JspCompilerAdapterFactory { | |||||
throws BuildException { | throws BuildException { | ||||
try { | try { | ||||
Class<? extends JspCompilerAdapter> c = classloader.findClass(className).asSubclass(JspCompilerAdapter.class); | Class<? extends JspCompilerAdapter> c = classloader.findClass(className).asSubclass(JspCompilerAdapter.class); | ||||
return c.newInstance(); | |||||
return c.getDeclaredConstructor().newInstance(); | |||||
} catch (ClassNotFoundException cnfe) { | } catch (ClassNotFoundException cnfe) { | ||||
throw new BuildException(className + " can't be found.", cnfe); | throw new BuildException(className + " can't be found.", cnfe); | ||||
} catch (ClassCastException cce) { | } catch (ClassCastException cce) { | ||||
@@ -302,11 +302,11 @@ public class FormatterElement { | |||||
JUnitResultFormatterMirror r; | JUnitResultFormatterMirror r; | ||||
try { | try { | ||||
r = f.asSubclass(JUnitResultFormatterMirror.class).newInstance(); | |||||
r = f.asSubclass(JUnitResultFormatterMirror.class).getDeclaredConstructor().newInstance(); | |||||
} catch (ClassCastException e) { | } catch (ClassCastException e) { | ||||
throw new BuildException("%s is not a JUnitResultFormatter", | |||||
classname); | |||||
} catch (InstantiationException | IllegalAccessException e) { | |||||
throw new BuildException("%s is not a JUnitResultFormatter", classname); | |||||
} catch (InstantiationException | IllegalAccessException | |||||
| NoSuchMethodException | InvocationTargetException e) { | |||||
throw new BuildException(e); | throw new BuildException(e); | ||||
} | } | ||||
@@ -137,7 +137,7 @@ public class TearDownOnVmCrash implements JUnitResultFormatter { | |||||
try { | try { | ||||
Method td = testClass.getMethod("tearDown"); | Method td = testClass.getMethod("tearDown"); | ||||
if (td.getReturnType() == Void.TYPE) { | if (td.getReturnType() == Void.TYPE) { | ||||
td.invoke(testClass.newInstance()); | |||||
td.invoke(testClass.getDeclaredConstructor().newInstance()); | |||||
} | } | ||||
} catch (NoSuchMethodException nsme) { | } catch (NoSuchMethodException nsme) { | ||||
// no tearDown, fine | // no tearDown, fine | ||||
@@ -293,7 +293,7 @@ public class LauncherSupport { | |||||
throw new BuildException("Listener class " + className + " is not of type " + TestExecutionListener.class.getName()); | throw new BuildException("Listener class " + className + " is not of type " + TestExecutionListener.class.getName()); | ||||
} | } | ||||
try { | try { | ||||
return (TestExecutionListener) klass.newInstance(); | |||||
return (TestExecutionListener) klass.getDeclaredConstructor().newInstance(); | |||||
} catch (Exception e) { | } catch (Exception e) { | ||||
throw new BuildException("Failed to create an instance of listener " + className, e); | throw new BuildException("Failed to create an instance of listener " + className, e); | ||||
} | } | ||||
@@ -55,7 +55,7 @@ public final class SunNative2Ascii extends DefaultNative2Ascii { | |||||
try { | try { | ||||
Class<?> n2aMain = Class.forName(SUN_TOOLS_NATIVE2ASCII_MAIN); | Class<?> n2aMain = Class.forName(SUN_TOOLS_NATIVE2ASCII_MAIN); | ||||
Method convert = n2aMain.getMethod("convert", String[].class); | Method convert = n2aMain.getMethod("convert", String[].class); | ||||
return Boolean.TRUE.equals(convert.invoke(n2aMain.newInstance(), | |||||
return Boolean.TRUE.equals(convert.invoke(n2aMain.getDeclaredConstructor().newInstance(), | |||||
(Object) cmd.getArguments())); | (Object) cmd.getArguments())); | ||||
} catch (BuildException ex) { | } catch (BuildException ex) { | ||||
//rethrow | //rethrow | ||||
@@ -75,7 +75,7 @@ public abstract class EnumeratedAttribute { | |||||
} | } | ||||
EnumeratedAttribute ea; | EnumeratedAttribute ea; | ||||
try { | try { | ||||
ea = clazz.newInstance(); | |||||
ea = clazz.getDeclaredConstructor().newInstance(); | |||||
} catch (Exception e) { | } catch (Exception e) { | ||||
throw new BuildException(e); | throw new BuildException(e); | ||||
} | } | ||||
@@ -231,7 +231,7 @@ public class Mapper extends DataType { | |||||
} | } | ||||
try { | try { | ||||
FileNameMapper m = getImplementationClass().newInstance(); | |||||
FileNameMapper m = getImplementationClass().getDeclaredConstructor().newInstance(); | |||||
final Project p = getProject(); | final Project p = getProject(); | ||||
if (p != null) { | if (p != null) { | ||||
p.setProjectReference(m); | p.setProjectReference(m); | ||||
@@ -522,7 +522,7 @@ public class XMLCatalog extends DataType | |||||
// if it can see it - doesn't use the context loader. | // if it can see it - doesn't use the context loader. | ||||
clazz = Class.forName(APACHE_RESOLVER, true, baseResolverLoader); | clazz = Class.forName(APACHE_RESOLVER, true, baseResolverLoader); | ||||
Object obj = clazz.newInstance(); | |||||
Object obj = clazz.getDeclaredConstructor().newInstance(); | |||||
// | // | ||||
// Success! The xml-commons resolver library is | // Success! The xml-commons resolver library is | ||||
// available, so use it. | // available, so use it. | ||||
@@ -19,6 +19,7 @@ | |||||
package org.apache.tools.ant.types.selectors; | package org.apache.tools.ant.types.selectors; | ||||
import java.io.File; | import java.io.File; | ||||
import java.lang.reflect.InvocationTargetException; | |||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.Collections; | import java.util.Collections; | ||||
import java.util.List; | import java.util.List; | ||||
@@ -67,7 +68,7 @@ public class ExtendSelector extends BaseSelector { | |||||
= getProject().createClassLoader(classpath); | = getProject().createClassLoader(classpath); | ||||
c = Class.forName(classname, true, al); | c = Class.forName(classname, true, al); | ||||
} | } | ||||
dynselector = c.asSubclass(FileSelector.class).newInstance(); | |||||
dynselector = c.asSubclass(FileSelector.class).getDeclaredConstructor().newInstance(); | |||||
final Project p = getProject(); | final Project p = getProject(); | ||||
if (p != null) { | if (p != null) { | ||||
p.setProjectReference(dynselector); | p.setProjectReference(dynselector); | ||||
@@ -75,7 +76,8 @@ public class ExtendSelector extends BaseSelector { | |||||
} catch (ClassNotFoundException cnfexcept) { | } catch (ClassNotFoundException cnfexcept) { | ||||
setError("Selector " + classname | setError("Selector " + classname | ||||
+ " not initialized, no such class"); | + " not initialized, no such class"); | ||||
} catch (InstantiationException iexcept) { | |||||
} catch (InstantiationException | NoSuchMethodException | |||||
| InvocationTargetException iexcept) { | |||||
setError("Selector " + classname | setError("Selector " + classname | ||||
+ " not initialized, could not create class"); | + " not initialized, could not create class"); | ||||
} catch (IllegalAccessException iaexcept) { | } catch (IllegalAccessException iaexcept) { | ||||
@@ -407,7 +407,7 @@ public class ModifiedSelector extends BaseExtendSelector | |||||
clazz = Class.forName(classname); | clazz = Class.forName(classname); | ||||
} | } | ||||
Object rv = clazz.newInstance(); | |||||
Object rv = clazz.getDeclaredConstructor().newInstance(); | |||||
if (!type.isInstance(rv)) { | if (!type.isInstance(rv)) { | ||||
throw new BuildException("Specified class (%s) %s", classname, msg); | throw new BuildException("Specified class (%s) %s", classname, msg); | ||||
@@ -25,6 +25,8 @@ import org.apache.tools.ant.ProjectComponent; | |||||
import org.apache.tools.ant.types.Path; | import org.apache.tools.ant.types.Path; | ||||
import org.apache.tools.ant.types.Reference; | import org.apache.tools.ant.types.Reference; | ||||
import java.lang.reflect.InvocationTargetException; | |||||
// CheckStyle:HideUtilityClassConstructorCheck OFF - bc | // CheckStyle:HideUtilityClassConstructorCheck OFF - bc | ||||
/** | /** | ||||
@@ -249,7 +251,7 @@ public class ClasspathUtils { | |||||
try { | try { | ||||
@SuppressWarnings("unchecked") | @SuppressWarnings("unchecked") | ||||
Class<T> clazz = (Class<T>) Class.forName(className, true, userDefinedLoader); | Class<T> clazz = (Class<T>) Class.forName(className, true, userDefinedLoader); | ||||
T o = clazz.newInstance(); | |||||
T o = clazz.getDeclaredConstructor().newInstance(); | |||||
if (!expectedType.isInstance(o)) { | if (!expectedType.isInstance(o)) { | ||||
throw new BuildException( | throw new BuildException( | ||||
"Class of unexpected Type: %s expected : %s", className, | "Class of unexpected Type: %s expected : %s", className, | ||||
@@ -261,7 +263,7 @@ public class ClasspathUtils { | |||||
} catch (InstantiationException e) { | } catch (InstantiationException e) { | ||||
throw new BuildException("Could not instantiate " + className | throw new BuildException("Could not instantiate " + className | ||||
+ ". Specified class should have a no " + "argument constructor.", e); | + ". Specified class should have a no " + "argument constructor.", e); | ||||
} catch (IllegalAccessException e) { | |||||
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { | |||||
throw new BuildException("Could not instantiate " + className | throw new BuildException("Could not instantiate " + className | ||||
+ ". Specified class should have a " + "public constructor.", e); | + ". Specified class should have a " + "public constructor.", e); | ||||
} catch (LinkageError e) { | } catch (LinkageError e) { | ||||
@@ -127,7 +127,7 @@ public class ScriptRunnerCreator { | |||||
} | } | ||||
try { | try { | ||||
runner = (ScriptRunnerBase) Class.forName( | runner = (ScriptRunnerBase) Class.forName( | ||||
runnerClass, true, scriptLoader).newInstance(); | |||||
runnerClass, true, scriptLoader).getDeclaredConstructor().newInstance(); | |||||
runner.setProject(project); | runner.setProject(project); | ||||
} catch (Exception ex) { | } catch (Exception ex) { | ||||
throw ReflectUtil.toBuildException(ex); | throw ReflectUtil.toBuildException(ex); | ||||
@@ -18,6 +18,7 @@ | |||||
package org.apache.tools.zip; | package org.apache.tools.zip; | ||||
import java.lang.reflect.InvocationTargetException; | |||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.List; | import java.util.List; | ||||
import java.util.Map; | import java.util.Map; | ||||
@@ -60,7 +61,7 @@ public class ExtraFieldUtils { | |||||
*/ | */ | ||||
public static void register(Class<?> c) { | public static void register(Class<?> c) { | ||||
try { | try { | ||||
ZipExtraField ze = (ZipExtraField) c.newInstance(); | |||||
ZipExtraField ze = (ZipExtraField) c.getDeclaredConstructor().newInstance(); | |||||
implementations.put(ze.getHeaderId(), c); | implementations.put(ze.getHeaderId(), c); | ||||
} catch (ClassCastException cc) { | } catch (ClassCastException cc) { | ||||
throw new RuntimeException(c + " doesn't implement ZipExtraField"); //NOSONAR | throw new RuntimeException(c + " doesn't implement ZipExtraField"); //NOSONAR | ||||
@@ -68,6 +69,11 @@ public class ExtraFieldUtils { | |||||
throw new RuntimeException(c + " is not a concrete class"); //NOSONAR | throw new RuntimeException(c + " is not a concrete class"); //NOSONAR | ||||
} catch (IllegalAccessException ie) { | } catch (IllegalAccessException ie) { | ||||
throw new RuntimeException(c + "'s no-arg constructor is not public"); //NOSONAR | throw new RuntimeException(c + "'s no-arg constructor is not public"); //NOSONAR | ||||
} catch (NoSuchMethodException e) { | |||||
throw new RuntimeException(c + "'s no-arg constructor not found"); //NOSONAR | |||||
} catch (InvocationTargetException e) { | |||||
throw new RuntimeException(c + "'s no-arg constructor threw an exception:" | |||||
+ e.getMessage()); //NOSONAR | |||||
} | } | ||||
} | } | ||||
@@ -84,7 +90,16 @@ public class ExtraFieldUtils { | |||||
throws InstantiationException, IllegalAccessException { | throws InstantiationException, IllegalAccessException { | ||||
Class<?> c = implementations.get(headerId); | Class<?> c = implementations.get(headerId); | ||||
if (c != null) { | if (c != null) { | ||||
return (ZipExtraField) c.newInstance(); | |||||
// wrap extra exceptions to preserve method signature | |||||
try { | |||||
return (ZipExtraField) c.getDeclaredConstructor().newInstance(); | |||||
} catch (InvocationTargetException e) { | |||||
throw (InstantiationException) | |||||
new InstantiationException().initCause(e.getTargetException()); | |||||
} catch (NoSuchMethodException e) { | |||||
throw (InstantiationException) | |||||
new InstantiationException().initCause(e); | |||||
} | |||||
} | } | ||||
UnrecognizedExtraField u = new UnrecognizedExtraField(); | UnrecognizedExtraField u = new UnrecognizedExtraField(); | ||||
u.setHeaderId(headerId); | u.setHeaderId(headerId); | ||||
@@ -71,7 +71,7 @@ public class PosixGroupSelectorTest { | |||||
@Test | @Test | ||||
public void posixGroupIsTrueForSelf() throws Exception { | public void posixGroupIsTrueForSelf() throws Exception { | ||||
long gid = (long) jaasProviderClass.getMethod(GROUP_GETTER) | long gid = (long) jaasProviderClass.getMethod(GROUP_GETTER) | ||||
.invoke(jaasProviderClass.newInstance()); | |||||
.invoke(jaasProviderClass.getDeclaredConstructor().newInstance()); | |||||
File file = folder.newFile("f.txt"); | File file = folder.newFile("f.txt"); | ||||
Map<String, Object> fileAttributes = Files.readAttributes(file.toPath(), | Map<String, Object> fileAttributes = Files.readAttributes(file.toPath(), | ||||
@@ -87,7 +87,7 @@ public class PosixGroupSelectorTest { | |||||
@Test | @Test | ||||
public void posixGroupFollowSymlinks() throws Exception { | public void posixGroupFollowSymlinks() throws Exception { | ||||
long gid = (long) jaasProviderClass.getMethod(GROUP_GETTER) | long gid = (long) jaasProviderClass.getMethod(GROUP_GETTER) | ||||
.invoke(jaasProviderClass.newInstance()); | |||||
.invoke(jaasProviderClass.getDeclaredConstructor().newInstance()); | |||||
File target = new File(folder.getRoot(), "link"); | File target = new File(folder.getRoot(), "link"); | ||||
Path symbolicLink = Files.createSymbolicLink(target.toPath(), TEST_FILE.toPath()); | Path symbolicLink = Files.createSymbolicLink(target.toPath(), TEST_FILE.toPath()); | ||||