Skip to content

Commit

Permalink
Remove usages of deprecated (in Java 9 and above) Class#newInstance (
Browse files Browse the repository at this point in the history
…#5483)

* Remove usages of deprecated (in Java 9 and above) `Class#newInstance`

* Improve catching of InvocationTargetException

* Use AssertionError where appropriate
  • Loading branch information
basil authored May 16, 2021
1 parent 10fab8b commit 52acfa9
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 26 deletions.
5 changes: 3 additions & 2 deletions core/src/main/java/hudson/ClassicPluginStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import hudson.util.CyclicGraphDetector.CycleDetectedException;
import hudson.util.IOUtils;
import hudson.util.MaskingClassLoader;
import java.lang.reflect.InvocationTargetException;
import jenkins.ClassLoaderReflectionToolkit;
import jenkins.ExtensionFilter;
import jenkins.plugins.DetachedPluginsUtil;
Expand Down Expand Up @@ -379,14 +380,14 @@ public void load(PluginWrapper wrapper) throws IOException {
} else {
try {
Class<?> clazz = wrapper.classLoader.loadClass(className);
Object o = clazz.newInstance();
Object o = clazz.getDeclaredConstructor().newInstance();
if(!(o instanceof Plugin)) {
throw new IOException(className+" doesn't extend from hudson.Plugin");
}
wrapper.setPlugin((Plugin) o);
} catch (LinkageError | ClassNotFoundException e) {
throw new IOException("Unable to load " + className + " from " + wrapper.getShortName(),e);
} catch (IllegalAccessException | InstantiationException e) {
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new IOException("Unable to create instance of " + className + " from " + wrapper.getShortName(),e);
}
}
Expand Down
7 changes: 4 additions & 3 deletions core/src/main/java/hudson/cli/CLICommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.List;
Expand Down Expand Up @@ -481,9 +482,9 @@ protected String getClientEnvironmentVariable(String name) throws IOException, I
*/
protected CLICommand createClone() {
try {
return getClass().newInstance();
} catch (IllegalAccessException | InstantiationException e) {
throw new AssertionError(e);
return getClass().getDeclaredConstructor().newInstance();
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new LinkageError(e.getMessage(), e);
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/cli/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public PublicKey verifyIdentity(byte[] sharedSecret) throws IOException, General

return spk;
} catch (ClassNotFoundException e) {
throw new Error(e); // impossible
throw new AssertionError(e); // impossible
}
}

Expand Down
21 changes: 20 additions & 1 deletion core/src/main/java/hudson/lifecycle/Lifecycle.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import hudson.ExtensionPoint;
import hudson.Functions;
import java.io.UncheckedIOException;
import java.lang.reflect.InvocationTargetException;
import jenkins.util.SystemProperties;
import hudson.Util;
import jenkins.model.Jenkins;
Expand Down Expand Up @@ -62,7 +64,11 @@ public static synchronized Lifecycle get() {
if(p!=null) {
try {
ClassLoader cl = Jenkins.get().getPluginManager().uberClassLoader;
instance = (Lifecycle)cl.loadClass(p).newInstance();
instance = (Lifecycle)cl.loadClass(p).getDeclaredConstructor().newInstance();
} catch (NoSuchMethodException e) {
NoSuchMethodError x = new NoSuchMethodError(e.getMessage());
x.initCause(e);
throw x;
} catch (InstantiationException e) {
InstantiationError x = new InstantiationError(e.getMessage());
x.initCause(e);
Expand All @@ -75,6 +81,19 @@ public static synchronized Lifecycle get() {
NoClassDefFoundError x = new NoClassDefFoundError(e.getMessage());
x.initCause(e);
throw x;
} catch (InvocationTargetException e) {
Throwable t = e.getCause();
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else if (t instanceof IOException) {
throw new UncheckedIOException((IOException) t);
} else if (t instanceof Exception) {
throw new RuntimeException(t);
} else if (t instanceof Error) {
throw (Error) t;
} else {
throw new Error(e);
}
}
} else {
if(Functions.isWindows()) {
Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/hudson/model/ComputerSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import hudson.util.DescribableList;
import hudson.util.FormApply;
import hudson.util.FormValidation;
import java.lang.reflect.InvocationTargetException;
import jenkins.model.Jenkins;
import jenkins.model.ModelObjectWithChildren;
import jenkins.model.ModelObjectWithContextMenu.ContextMenu;
Expand Down Expand Up @@ -474,10 +475,10 @@ public static List<String> getComputerNames() {

private static NodeMonitor createDefaultInstance(Descriptor<NodeMonitor> d, boolean ignored) {
try {
NodeMonitor nm = d.clazz.newInstance();
NodeMonitor nm = d.clazz.getDeclaredConstructor().newInstance();
nm.setIgnored(ignored);
return nm;
} catch (InstantiationException | IllegalAccessException e) {
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
LOGGER.log(Level.SEVERE, "Failed to instantiate "+d.clazz,e);
}
return null;
Expand Down
9 changes: 4 additions & 5 deletions core/src/main/java/hudson/model/Descriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import hudson.util.ReflectionUtils;
import hudson.util.ReflectionUtils.Parameter;
import hudson.views.ListViewColumn;
import java.lang.reflect.InvocationTargetException;
import jenkins.model.GlobalConfiguration;
import jenkins.model.GlobalConfigurationCategory;
import jenkins.model.Jenkins;
Expand Down Expand Up @@ -589,7 +590,7 @@ public T newInstance(@Nullable StaplerRequest req, @NonNull JSONObject formData)
} else {
if (req==null) {
// yes, req is supposed to be always non-null, but see the note above
return verifyNewInstance(clazz.newInstance());
return verifyNewInstance(clazz.getDeclaredConstructor().newInstance());
}

// new behavior as of 1.206
Expand All @@ -608,10 +609,8 @@ public T newInstance(@Nullable StaplerRequest req, @NonNull JSONObject formData)
req.setBindInterceptor(oldInterceptor);
}
}
} catch (NoSuchMethodException e) {
throw new AssertionError(e); // impossible
} catch (InstantiationException | IllegalAccessException | RuntimeException e) {
throw new Error("Failed to instantiate "+clazz+" from "+RedactSecretJsonInErrorMessageSanitizer.INSTANCE.sanitize(formData),e);
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException | RuntimeException e) {
throw new LinkageError("Failed to instantiate "+clazz+" from "+RedactSecretJsonInErrorMessageSanitizer.INSTANCE.sanitize(formData),e);
}
}

Expand Down
21 changes: 20 additions & 1 deletion core/src/main/java/hudson/util/DescribableList.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.mapper.Mapper;
import hudson.model.AbstractProject;
import java.io.UncheckedIOException;
import java.lang.reflect.InvocationTargetException;
import jenkins.model.DependencyDeclarer;
import hudson.model.DependencyGraph;
import hudson.model.Describable;
Expand Down Expand Up @@ -275,10 +277,14 @@ public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingC
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
try {
DescribableList r = (DescribableList) context.getRequiredType().asSubclass(DescribableList.class).newInstance();
DescribableList r = (DescribableList) context.getRequiredType().asSubclass(DescribableList.class).getDeclaredConstructor().newInstance();
CopyOnWriteList core = copyOnWriteListConverter.unmarshal(reader, context);
r.data.replaceBy(core);
return r;
} catch (NoSuchMethodException e) {
NoSuchMethodError x = new NoSuchMethodError();
x.initCause(e);
throw x;
} catch (InstantiationException e) {
InstantiationError x = new InstantiationError();
x.initCause(e);
Expand All @@ -287,6 +293,19 @@ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext co
IllegalAccessError x = new IllegalAccessError();
x.initCause(e);
throw x;
} catch (InvocationTargetException e) {
Throwable t = e.getCause();
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else if (t instanceof IOException) {
throw new UncheckedIOException((IOException) t);
} else if (t instanceof Exception) {
throw new RuntimeException(t);
} else if (t instanceof Error) {
throw (Error) t;
} else {
throw new Error(e);
}
}
}
}
Expand Down
21 changes: 20 additions & 1 deletion core/src/main/java/hudson/util/PersistedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import hudson.model.Saveable;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.reflect.InvocationTargetException;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -287,9 +289,13 @@ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext co
CopyOnWriteList core = copyOnWriteListConverter.unmarshal(reader, context);

try {
PersistedList r = (PersistedList)context.getRequiredType().newInstance();
PersistedList r = (PersistedList)context.getRequiredType().getDeclaredConstructor().newInstance();
r.data.replaceBy(core);
return r;
} catch (NoSuchMethodException e) {
NoSuchMethodError x = new NoSuchMethodError();
x.initCause(e);
throw x;
} catch (InstantiationException e) {
InstantiationError x = new InstantiationError();
x.initCause(e);
Expand All @@ -298,6 +304,19 @@ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext co
IllegalAccessError x = new IllegalAccessError();
x.initCause(e);
throw x;
} catch (InvocationTargetException e) {
Throwable t = e.getCause();
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else if (t instanceof IOException) {
throw new UncheckedIOException((IOException) t);
} else if (t instanceof Exception) {
throw new RuntimeException(t);
} else if (t instanceof Error) {
throw (Error) t;
} else {
throw new Error(e);
}
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/jenkins/ClassLoaderReflectionToolkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private static <T extends Exception> Object invoke(Method method, Class<T> excep
try {
return method.invoke(obj, args);
} catch (IllegalAccessException x) {
throw new AssertionError(x);
throw new LinkageError(x.getMessage(), x);
} catch (InvocationTargetException x) {
Throwable x2 = x.getCause();
if (x2 instanceof RuntimeException) {
Expand Down Expand Up @@ -193,7 +193,7 @@ public Class findLoadedClass(ClassLoader cl, String name) throws InvocationTarge
try {
return (Class)FindLoadedClass.FIND_LOADED_CLASS.invoke(cl,name);
} catch (IllegalAccessException e) {
throw new Error(e);
throw new LinkageError(e.getMessage(), e);
}
}

Expand All @@ -203,7 +203,7 @@ public Class findClass(ClassLoader cl, String name) throws InvocationTargetExcep
try {
return (Class)FindClass.FIND_CLASS.invoke(cl,name);
} catch (IllegalAccessException e) {
throw new Error(e);
throw new LinkageError(e.getMessage(), e);
}
}

Expand All @@ -213,7 +213,7 @@ public URL findResource(ClassLoader cl, String name) throws InvocationTargetExce
try {
return (URL)FindResource.FIND_RESOURCE.invoke(cl,name);
} catch (IllegalAccessException e) {
throw new Error(e);
throw new LinkageError(e.getMessage(), e);
}
}

Expand All @@ -223,7 +223,7 @@ public Enumeration<URL> findResources(ClassLoader cl, String name) throws Invoca
try {
return (Enumeration<URL>)FindResources.FIND_RESOURCES.invoke(cl,name);
} catch (IllegalAccessException e) {
throw new Error(e);
throw new LinkageError(e.getMessage(), e);
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/jenkins/model/lazy/LazyBuildMixIn.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public RunT loadBuild(File dir) throws IOException {
try {
return getBuildClass().getConstructor(asJob().getClass(), File.class).newInstance(asJob(), dir);
} catch (InstantiationException | NoSuchMethodException | IllegalAccessException e) {
throw new Error(e);
throw new LinkageError(e.getMessage(), e);
} catch (InvocationTargetException e) {
throw handleInvocationTargetException(e);
}
Expand All @@ -187,7 +187,7 @@ public final synchronized RunT newBuild() throws IOException {
throw handleInvocationTargetException(e);
} catch (ReflectiveOperationException | IllegalStateException e) {
LOGGER.log(Level.WARNING, String.format("A new build could not be created in job %s", asJob().getFullName()), e);
throw new Error(e);
throw new LinkageError(e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public AntWithFindResourceClassLoader(ClassLoader parent, boolean parentFirst) {
$pathComponents.setAccessible(true);
pathComponents = (ArrayList<File>)$pathComponents.get(this);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new Error(e);
throw new LinkageError(e.getMessage(), e);
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/hudson/util/SubClassGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void foo() throws Exception {
Class<? extends Foo> c = new SubClassGenerator(getClass().getClassLoader()).generate(Foo.class, "12345");
assertEquals("12345",c.getName());

c.newInstance();
c.getDeclaredConstructor().newInstance();

Foo f = c.getConstructor(String.class).newInstance("aaa");
assertEquals("aaa",f.s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public Object callFunction(HtmlPage page, Function function, Scriptable scope, S
}
}
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
throw new LinkageError(e.getMessage(), e);
}
}
return super.callFunction(page, function, scope, thisObject, args);
Expand Down

0 comments on commit 52acfa9

Please sign in to comment.