From 1060ca269ab1eb44670beb55e4ab1aab22767ee3 Mon Sep 17 00:00:00 2001 From: Patrick Ziegler Date: Thu, 17 Jul 2025 19:08:23 +0200 Subject: [PATCH] Avoid usage of deprecated newInstance() method As per the documentation, one can replace the call to clazz.newInstance() with clazz.getDeclaredConstructor().newInstance(). --- .../autobindings/DescriptorContainer.java | 4 +-- .../core/utils/reflect/ReflectionUtils.java | 35 +++++++++---------- .../forms/layout/column/ColumnLayoutInfo.java | 4 +-- .../layout/table/TableWrapLayoutSupport.java | 4 +-- .../rcp/model/rcp/ActionBarAdvisorInfo.java | 4 +-- .../rcp/model/rcp/ViewPartLikeInfo.java | 6 ++-- .../swing/customize/CustomizerAction.java | 2 +- .../swing/laf/model/PluginLafInfo.java | 4 +-- .../swing/laf/model/UserDefinedLafInfo.java | 4 +-- .../editor/beans/JavaBeanEditorProvider.java | 4 +-- .../editor/beans/PropertyEditorWrapper.java | 4 +-- .../swt/support/GridLayoutSupport.java | 4 +-- .../core/eval/MethodInvocationTest.java | 6 ++-- .../AbstractTextPropertyEditorTest.java | 4 +-- 14 files changed, 43 insertions(+), 46 deletions(-) diff --git a/org.eclipse.wb.core.databinding/src/org/eclipse/wb/internal/core/databinding/wizards/autobindings/DescriptorContainer.java b/org.eclipse.wb.core.databinding/src/org/eclipse/wb/internal/core/databinding/wizards/autobindings/DescriptorContainer.java index f31c30d7d..819c4d782 100644 --- a/org.eclipse.wb.core.databinding/src/org/eclipse/wb/internal/core/databinding/wizards/autobindings/DescriptorContainer.java +++ b/org.eclipse.wb.core.databinding/src/org/eclipse/wb/internal/core/databinding/wizards/autobindings/DescriptorContainer.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011 Google, Inc. + * Copyright (c) 2011, 2025 Google, Inc. and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -117,7 +117,7 @@ public void startElement(String uri, String localName, String name, Attributes a m_descriptorClass = classLoader.loadClass(attributes.getValue("class")); } else if ("descriptor".equals(name)) { // create descriptor - m_descriptor = (AbstractDescriptor) m_descriptorClass.newInstance(); + m_descriptor = (AbstractDescriptor) m_descriptorClass.getDeclaredConstructor().newInstance(); } else if (m_descriptor != null) { // fill attributes if (attributes.getLength() == 0) { diff --git a/org.eclipse.wb.core/src/org/eclipse/wb/internal/core/utils/reflect/ReflectionUtils.java b/org.eclipse.wb.core/src/org/eclipse/wb/internal/core/utils/reflect/ReflectionUtils.java index 95d63da0d..6080dff64 100644 --- a/org.eclipse.wb.core/src/org/eclipse/wb/internal/core/utils/reflect/ReflectionUtils.java +++ b/org.eclipse.wb.core/src/org/eclipse/wb/internal/core/utils/reflect/ReflectionUtils.java @@ -1415,30 +1415,27 @@ private static Object getRefObject(Object object) { * Helper class used in {@link #propagate(Throwable)}. */ private static class ExceptionThrower { - private static Throwable throwable; - - private ExceptionThrower() throws Throwable { - if (System.getProperty("wbp.ReflectionUtils.propagate().InstantiationException") != null) { - throw new InstantiationException(); - } - if (System.getProperty("wbp.ReflectionUtils.propagate().IllegalAccessException") != null) { - throw new IllegalAccessException(); - } - throw throwable; - } - - public static synchronized void spit(Throwable t) { + // Based on https://www.mail-archive.com/javaposse@googlegroups.com/msg05984.html + private static synchronized void spit(Throwable t) { if (System.getProperty("wbp.ReflectionUtils.propagate().dontThrow") == null) { - ExceptionThrower.throwable = t; try { - ExceptionThrower.class.newInstance(); - } catch (InstantiationException e) { - } catch (IllegalAccessException e) { - } finally { - ExceptionThrower.throwable = null; + if (System.getProperty("wbp.ReflectionUtils.propagate().InstantiationException") != null) { + ExceptionThrower.spit0(new InstantiationException()); + } + if (System.getProperty("wbp.ReflectionUtils.propagate().IllegalAccessException") != null) { + ExceptionThrower.spit0(new IllegalAccessException()); + } + ExceptionThrower.spit0(t); + } catch (InstantiationException | IllegalAccessException e) { + // ignore } } } + + @SuppressWarnings("unchecked") + private static void spit0(Throwable t) throws T, InstantiationException, IllegalAccessException { + throw (T) t; + } } /** diff --git a/org.eclipse.wb.rcp/src/org/eclipse/wb/internal/rcp/model/forms/layout/column/ColumnLayoutInfo.java b/org.eclipse.wb.rcp/src/org/eclipse/wb/internal/rcp/model/forms/layout/column/ColumnLayoutInfo.java index 47e37f6f8..01c64708e 100644 --- a/org.eclipse.wb.rcp/src/org/eclipse/wb/internal/rcp/model/forms/layout/column/ColumnLayoutInfo.java +++ b/org.eclipse.wb.rcp/src/org/eclipse/wb/internal/rcp/model/forms/layout/column/ColumnLayoutInfo.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2024 Google, Inc. + * Copyright (c) 2011, 2025 Google, Inc. and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -88,7 +88,7 @@ public void onSet() throws Exception { @Override protected Object getDefaultVirtualDataObject() throws Exception { ClassLoader editorLoader = GlobalState.getClassLoader(); - return editorLoader.loadClass("org.eclipse.ui.forms.widgets.ColumnLayoutData").newInstance(); + return editorLoader.loadClass("org.eclipse.ui.forms.widgets.ColumnLayoutData").getDeclaredConstructor().newInstance(); } @Override diff --git a/org.eclipse.wb.rcp/src/org/eclipse/wb/internal/rcp/model/forms/layout/table/TableWrapLayoutSupport.java b/org.eclipse.wb.rcp/src/org/eclipse/wb/internal/rcp/model/forms/layout/table/TableWrapLayoutSupport.java index f27893d84..2f6a7c340 100644 --- a/org.eclipse.wb.rcp/src/org/eclipse/wb/internal/rcp/model/forms/layout/table/TableWrapLayoutSupport.java +++ b/org.eclipse.wb.rcp/src/org/eclipse/wb/internal/rcp/model/forms/layout/table/TableWrapLayoutSupport.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2024 Google, Inc. and others. + * Copyright (c) 2011, 2025 Google, Inc. and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -82,6 +82,6 @@ public static int[] getRowHeights(Object layout) throws Exception { * Create new {@link TableWrapData}. */ public static Object createTableWrapData() throws Exception { - return loadClass("org.eclipse.ui.forms.widgets.TableWrapData").newInstance(); + return loadClass("org.eclipse.ui.forms.widgets.TableWrapData").getDeclaredConstructor().newInstance(); } } \ No newline at end of file diff --git a/org.eclipse.wb.rcp/src/org/eclipse/wb/internal/rcp/model/rcp/ActionBarAdvisorInfo.java b/org.eclipse.wb.rcp/src/org/eclipse/wb/internal/rcp/model/rcp/ActionBarAdvisorInfo.java index f42c9fcbc..3c61e9023 100644 --- a/org.eclipse.wb.rcp/src/org/eclipse/wb/internal/rcp/model/rcp/ActionBarAdvisorInfo.java +++ b/org.eclipse.wb.rcp/src/org/eclipse/wb/internal/rcp/model/rcp/ActionBarAdvisorInfo.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011 Google, Inc. + * Copyright (c) 2011, 2025 Google, Inc. and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -279,7 +279,7 @@ public void render() throws Exception { Class coolBarManagerClass = editorLoader.loadClass("org.eclipse.jface.action.CoolBarManager"); // create managers - m_menuManager = menuManagerClass.newInstance(); + m_menuManager = menuManagerClass.getDeclaredConstructor().newInstance(); m_coolBarManager = coolBarManagerClass.getConstructor(CoolBar.class).newInstance(m_coolBar); // OK, we prepared everything, now add actions/items/managers { diff --git a/org.eclipse.wb.rcp/src/org/eclipse/wb/internal/rcp/model/rcp/ViewPartLikeInfo.java b/org.eclipse.wb.rcp/src/org/eclipse/wb/internal/rcp/model/rcp/ViewPartLikeInfo.java index 40cbfdb9d..a19e7e62d 100644 --- a/org.eclipse.wb.rcp/src/org/eclipse/wb/internal/rcp/model/rcp/ViewPartLikeInfo.java +++ b/org.eclipse.wb.rcp/src/org/eclipse/wb/internal/rcp/model/rcp/ViewPartLikeInfo.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2024 Google, Inc. and others. + * Copyright (c) 2011, 2025 Google, Inc. and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -94,8 +94,8 @@ private void prepareActionBars() throws Exception { Class menuManagerClass = editorLoader.loadClass("org.eclipse.jface.action.MenuManager"); Class actionBarsClass = editorLoader.loadClass("org.eclipse.ui.IActionBars"); // create managers - m_toolBarManager = toolBarManagerClass.newInstance(); - m_menuManager = menuManagerClass.newInstance(); + m_toolBarManager = toolBarManagerClass.getDeclaredConstructor().newInstance(); + m_menuManager = menuManagerClass.getDeclaredConstructor().newInstance(); // create IActionBars m_actionBars = Proxy.newProxyInstance( diff --git a/org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/customize/CustomizerAction.java b/org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/customize/CustomizerAction.java index 21b0527c4..e1e1b25b6 100644 --- a/org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/customize/CustomizerAction.java +++ b/org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/customize/CustomizerAction.java @@ -84,7 +84,7 @@ public void run() throws Exception { } private void performCustomize0() throws Exception { - Customizer customizer = m_customizerClass.newInstance(); + Customizer customizer = m_customizerClass.getDeclaredConstructor().newInstance(); // prepare properties information final JavaInfoState javaInfoState = JavaInfoState.getState(m_javaInfo); boolean explicit = isExplicitPropertyChange(m_javaInfo); diff --git a/org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/laf/model/PluginLafInfo.java b/org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/laf/model/PluginLafInfo.java index 6992cc069..d2c82b917 100644 --- a/org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/laf/model/PluginLafInfo.java +++ b/org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/laf/model/PluginLafInfo.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011 Google, Inc. + * Copyright (c) 2011, 2025 Google, Inc. and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -75,7 +75,7 @@ public LookAndFeel getLookAndFeelInstance() throws Exception { if (m_instanceReference == null || m_instanceReference.get() == null) { m_initializer.initialize(); Class lafClass = m_extensionBundle.loadClass(getClassName()); - m_instanceReference = new SoftReference<>((LookAndFeel) lafClass.newInstance()); + m_instanceReference = new SoftReference<>((LookAndFeel) lafClass.getDeclaredConstructor().newInstance()); } return m_instanceReference.get(); } diff --git a/org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/laf/model/UserDefinedLafInfo.java b/org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/laf/model/UserDefinedLafInfo.java index df45fe8b8..5b9b84bcf 100644 --- a/org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/laf/model/UserDefinedLafInfo.java +++ b/org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/laf/model/UserDefinedLafInfo.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011 Google, Inc. + * Copyright (c) 2011, 2025 Google, Inc. and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -51,7 +51,7 @@ public LookAndFeel getLookAndFeelInstance() throws Exception { ClassLoader classLoader = getClassLoader(); m_lafClass = classLoader.loadClass(getClassName()); } - return (LookAndFeel) m_lafClass.newInstance(); + return (LookAndFeel) m_lafClass.getDeclaredConstructor().newInstance(); } private ClassLoader getClassLoader() throws Exception { diff --git a/org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/model/property/editor/beans/JavaBeanEditorProvider.java b/org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/model/property/editor/beans/JavaBeanEditorProvider.java index e7b6d55d6..2ebf379d1 100644 --- a/org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/model/property/editor/beans/JavaBeanEditorProvider.java +++ b/org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/model/property/editor/beans/JavaBeanEditorProvider.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2024 Google, Inc. + * Copyright (c) 2011, 2025 Google, Inc. and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -36,7 +36,7 @@ public final class JavaBeanEditorProvider extends PropertyEditorProvider { public PropertyEditor getEditorForEditorType(Class editorType) throws Exception { if (java.beans.PropertyEditor.class.isAssignableFrom(editorType)) { try { - return createEditor((java.beans.PropertyEditor) editorType.newInstance()); + return createEditor((java.beans.PropertyEditor) editorType.getDeclaredConstructor().newInstance()); } catch (Throwable e) { // silently ignore any errors, as Introspector does } diff --git a/org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/model/property/editor/beans/PropertyEditorWrapper.java b/org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/model/property/editor/beans/PropertyEditorWrapper.java index 46547d7d4..339b2fdba 100644 --- a/org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/model/property/editor/beans/PropertyEditorWrapper.java +++ b/org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/model/property/editor/beans/PropertyEditorWrapper.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2024 Google, Inc. + * Copyright (c) 2011, 2025 Google, Inc. and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -197,7 +197,7 @@ private static class DialogPresentation extends ButtonPropertyEditorPresentation // //////////////////////////////////////////////////////////////////////////// public DialogPresentation(java.beans.PropertyEditor propertyEditor) throws Exception { - m_dialogPropertyEditor = propertyEditor.getClass().newInstance(); + m_dialogPropertyEditor = propertyEditor.getClass().getDeclaredConstructor().newInstance(); } //////////////////////////////////////////////////////////////////////////// diff --git a/org.eclipse.wb.swt/src/org/eclipse/wb/internal/swt/support/GridLayoutSupport.java b/org.eclipse.wb.swt/src/org/eclipse/wb/internal/swt/support/GridLayoutSupport.java index 8384e519a..c9ae0969e 100644 --- a/org.eclipse.wb.swt/src/org/eclipse/wb/internal/swt/support/GridLayoutSupport.java +++ b/org.eclipse.wb.swt/src/org/eclipse/wb/internal/swt/support/GridLayoutSupport.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2024 Google, Inc. and others. + * Copyright (c) 2011, 2025 Google, Inc. and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -104,6 +104,6 @@ public static int[] getRowHeights(Object layout) { * Create new {@link org.eclipse.swt.layout.GridData}. */ public static Object createGridData() throws Exception { - return loadClass("org.eclipse.swt.layout.GridData").newInstance(); + return loadClass("org.eclipse.swt.layout.GridData").getDeclaredConstructor().newInstance(); } } \ No newline at end of file diff --git a/org.eclipse.wb.tests/src/org/eclipse/wb/tests/designer/core/eval/MethodInvocationTest.java b/org.eclipse.wb.tests/src/org/eclipse/wb/tests/designer/core/eval/MethodInvocationTest.java index a37a02963..a401c711e 100644 --- a/org.eclipse.wb.tests/src/org/eclipse/wb/tests/designer/core/eval/MethodInvocationTest.java +++ b/org.eclipse.wb.tests/src/org/eclipse/wb/tests/designer/core/eval/MethodInvocationTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2024 Google, Inc. and others. + * Copyright (c) 2011, 2025 Google, Inc. and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -974,7 +974,7 @@ public Object evaluate(EvaluationContext context, Constructor actualConstructor, Object[] arguments) throws Exception { if (AstNodeUtils.isSuccessorOf(typeBinding, "test.MyObject")) { - return clazz.newInstance(); + return clazz.getDeclaredConstructor().newInstance(); } return AstEvaluationEngine.UNKNOWN; } @@ -1221,7 +1221,7 @@ public int getSize() { { ClassLoader projectClassLoader = CodeUtils.getProjectClassLoader(m_lastEditor.getModelUnit().getJavaProject()); - final Object baseInstance = projectClassLoader.loadClass("test.Base").newInstance(); + final Object baseInstance = projectClassLoader.loadClass("test.Base").getDeclaredConstructor().newInstance(); ExecutionFlowDescription flowDescription = new ExecutionFlowDescription(methodDeclaration); context = new EvaluationContext(projectClassLoader, flowDescription) { @Override diff --git a/org.eclipse.wb.tests/src/org/eclipse/wb/tests/designer/core/model/property/editor/AbstractTextPropertyEditorTest.java b/org.eclipse.wb.tests/src/org/eclipse/wb/tests/designer/core/model/property/editor/AbstractTextPropertyEditorTest.java index 22838d8d0..0e7a6b4c2 100644 --- a/org.eclipse.wb.tests/src/org/eclipse/wb/tests/designer/core/model/property/editor/AbstractTextPropertyEditorTest.java +++ b/org.eclipse.wb.tests/src/org/eclipse/wb/tests/designer/core/model/property/editor/AbstractTextPropertyEditorTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011 Google, Inc. + * Copyright (c) 2011, 2025 Google, Inc. and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -119,7 +119,7 @@ protected void initTestSourceState() throws Exception { protected T createEditor(Class clazz, Map parameters) throws Exception { initTestSourceState(); - T editor = clazz.newInstance(); + T editor = clazz.getDeclaredConstructor().newInstance(); IConfigurablePropertyObject configurableEditor = (IConfigurablePropertyObject) editor; configurableEditor.configure(m_lastState, parameters); return editor;