diff --git a/core/src/main/java/hudson/ClassicPluginStrategy.java b/core/src/main/java/hudson/ClassicPluginStrategy.java index de4b40a75e..5b584e5de7 100644 --- a/core/src/main/java/hudson/ClassicPluginStrategy.java +++ b/core/src/main/java/hudson/ClassicPluginStrategy.java @@ -31,6 +31,7 @@ import hudson.util.CyclicGraphDetector; 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; @@ -379,14 +380,14 @@ public class ClassicPluginStrategy implements PluginStrategy { } 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); } } diff --git a/core/src/main/java/hudson/cli/CLICommand.java b/core/src/main/java/hudson/cli/CLICommand.java index d0c9825768..22bca98235 100644 --- a/core/src/main/java/hudson/cli/CLICommand.java +++ b/core/src/main/java/hudson/cli/CLICommand.java @@ -40,6 +40,7 @@ import java.io.ByteArrayOutputStream; 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; @@ -481,9 +482,9 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable { */ 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); } } diff --git a/core/src/main/java/hudson/cli/Connection.java b/core/src/main/java/hudson/cli/Connection.java index 13cec3f615..85fb25d58c 100644 --- a/core/src/main/java/hudson/cli/Connection.java +++ b/core/src/main/java/hudson/cli/Connection.java @@ -268,7 +268,7 @@ public class Connection { return spk; } catch (ClassNotFoundException e) { - throw new Error(e); // impossible + throw new AssertionError(e); // impossible } } diff --git a/core/src/main/java/hudson/lifecycle/Lifecycle.java b/core/src/main/java/hudson/lifecycle/Lifecycle.java index 152f4d457b..f55d42bcd3 100644 --- a/core/src/main/java/hudson/lifecycle/Lifecycle.java +++ b/core/src/main/java/hudson/lifecycle/Lifecycle.java @@ -25,6 +25,8 @@ package hudson.lifecycle; 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; @@ -62,7 +64,11 @@ public abstract class Lifecycle implements ExtensionPoint { 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); @@ -75,6 +81,19 @@ public abstract class Lifecycle implements ExtensionPoint { 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()) { diff --git a/core/src/main/java/hudson/model/ComputerSet.java b/core/src/main/java/hudson/model/ComputerSet.java index 9ffcdfc80e..cbeb58bae1 100644 --- a/core/src/main/java/hudson/model/ComputerSet.java +++ b/core/src/main/java/hudson/model/ComputerSet.java @@ -37,6 +37,7 @@ import hudson.triggers.SafeTimerTask; 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; @@ -474,10 +475,10 @@ public final class ComputerSet extends AbstractModelObject implements Describabl private static NodeMonitor createDefaultInstance(Descriptor 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; diff --git a/core/src/main/java/hudson/model/Descriptor.java b/core/src/main/java/hudson/model/Descriptor.java index 2faecefa03..e307c1acd7 100644 --- a/core/src/main/java/hudson/model/Descriptor.java +++ b/core/src/main/java/hudson/model/Descriptor.java @@ -37,6 +37,7 @@ import hudson.util.FormValidation.CheckMethod; 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; @@ -589,7 +590,7 @@ public abstract class Descriptor> implements Saveable, } 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 @@ -608,10 +609,8 @@ public abstract class Descriptor> implements Saveable, 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); } } diff --git a/core/src/main/java/hudson/util/DescribableList.java b/core/src/main/java/hudson/util/DescribableList.java index ce2d08788f..7aa11e8852 100644 --- a/core/src/main/java/hudson/util/DescribableList.java +++ b/core/src/main/java/hudson/util/DescribableList.java @@ -31,6 +31,8 @@ import com.thoughtworks.xstream.io.HierarchicalStreamReader; 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; @@ -275,10 +277,14 @@ public class DescribableList, D extends Descriptor> @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); @@ -287,6 +293,19 @@ public class DescribableList, D extends Descriptor> 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); + } } } } diff --git a/core/src/main/java/hudson/util/PersistedList.java b/core/src/main/java/hudson/util/PersistedList.java index 9268e19502..50fcd1ced3 100644 --- a/core/src/main/java/hudson/util/PersistedList.java +++ b/core/src/main/java/hudson/util/PersistedList.java @@ -35,6 +35,8 @@ import hudson.model.Describable; 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; @@ -287,9 +289,13 @@ public class PersistedList extends AbstractList { 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); @@ -298,6 +304,19 @@ public class PersistedList extends AbstractList { 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); + } } } } diff --git a/core/src/main/java/jenkins/ClassLoaderReflectionToolkit.java b/core/src/main/java/jenkins/ClassLoaderReflectionToolkit.java index 06a61b879b..3992907a09 100644 --- a/core/src/main/java/jenkins/ClassLoaderReflectionToolkit.java +++ b/core/src/main/java/jenkins/ClassLoaderReflectionToolkit.java @@ -31,7 +31,7 @@ public class ClassLoaderReflectionToolkit { 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) { @@ -193,7 +193,7 @@ public class ClassLoaderReflectionToolkit { try { return (Class)FindLoadedClass.FIND_LOADED_CLASS.invoke(cl,name); } catch (IllegalAccessException e) { - throw new Error(e); + throw new LinkageError(e.getMessage(), e); } } @@ -203,7 +203,7 @@ public class ClassLoaderReflectionToolkit { try { return (Class)FindClass.FIND_CLASS.invoke(cl,name); } catch (IllegalAccessException e) { - throw new Error(e); + throw new LinkageError(e.getMessage(), e); } } @@ -213,7 +213,7 @@ public class ClassLoaderReflectionToolkit { try { return (URL)FindResource.FIND_RESOURCE.invoke(cl,name); } catch (IllegalAccessException e) { - throw new Error(e); + throw new LinkageError(e.getMessage(), e); } } @@ -223,7 +223,7 @@ public class ClassLoaderReflectionToolkit { try { return (Enumeration)FindResources.FIND_RESOURCES.invoke(cl,name); } catch (IllegalAccessException e) { - throw new Error(e); + throw new LinkageError(e.getMessage(), e); } } diff --git a/core/src/main/java/jenkins/model/lazy/LazyBuildMixIn.java b/core/src/main/java/jenkins/model/lazy/LazyBuildMixIn.java index 0aec9bc625..7215fbc862 100644 --- a/core/src/main/java/jenkins/model/lazy/LazyBuildMixIn.java +++ b/core/src/main/java/jenkins/model/lazy/LazyBuildMixIn.java @@ -165,7 +165,7 @@ public abstract class LazyBuildMixIn & Queue.Task & 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); } @@ -187,7 +187,7 @@ public abstract class LazyBuildMixIn & Queue.Task & 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); } } diff --git a/core/src/main/java/jenkins/util/AntWithFindResourceClassLoader.java b/core/src/main/java/jenkins/util/AntWithFindResourceClassLoader.java index 58006e09fa..4c03d41242 100644 --- a/core/src/main/java/jenkins/util/AntWithFindResourceClassLoader.java +++ b/core/src/main/java/jenkins/util/AntWithFindResourceClassLoader.java @@ -23,7 +23,7 @@ public class AntWithFindResourceClassLoader extends AntClassLoader implements Cl $pathComponents.setAccessible(true); pathComponents = (ArrayList)$pathComponents.get(this); } catch (NoSuchFieldException | IllegalAccessException e) { - throw new Error(e); + throw new LinkageError(e.getMessage(), e); } } diff --git a/core/src/test/java/hudson/util/SubClassGeneratorTest.java b/core/src/test/java/hudson/util/SubClassGeneratorTest.java index d9ca838c63..1cc590bb6d 100644 --- a/core/src/test/java/hudson/util/SubClassGeneratorTest.java +++ b/core/src/test/java/hudson/util/SubClassGeneratorTest.java @@ -49,7 +49,7 @@ public class SubClassGeneratorTest { Class 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); diff --git a/test/src/test/java/hudson/tools/ZipExtractionInstallerTest.java b/test/src/test/java/hudson/tools/ZipExtractionInstallerTest.java index 3885d1e103..fe3b82c5c0 100644 --- a/test/src/test/java/hudson/tools/ZipExtractionInstallerTest.java +++ b/test/src/test/java/hudson/tools/ZipExtractionInstallerTest.java @@ -160,7 +160,7 @@ public class ZipExtractionInstallerTest { } } } catch (NoSuchFieldException | IllegalAccessException e) { - e.printStackTrace(); + throw new LinkageError(e.getMessage(), e); } } return super.callFunction(page, function, scope, thisObject, args);