Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Comment on lines -1420 to -1428
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a very nasty piece of code. Creating an instance of this class via clazz.newInstance() bypasses the compiler check that allows you to throw an Exception without having to catch it.

By using clazz.getDeclaredConstructor().newInstance() this exception is wrapped inside an InvocationTargetException and thus defeating the entire point of this class.

While it's still a very questionable implementation, the new approach takes advantage of type erasure to make the compiler think the Exception is actually a RuntimeException.


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.<RuntimeException>spit0(new InstantiationException());
}
if (System.getProperty("wbp.ReflectionUtils.propagate().IllegalAccessException") != null) {
ExceptionThrower.<RuntimeException>spit0(new IllegalAccessException());
}
ExceptionThrower.<RuntimeException>spit0(t);
} catch (InstantiationException | IllegalAccessException e) {
// ignore
}
}
}

@SuppressWarnings("unchecked")
private static <T extends Throwable> void spit0(Throwable t) throws T, InstantiationException, IllegalAccessException {
throw (T) t;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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();
}

////////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -119,7 +119,7 @@ protected void initTestSourceState() throws Exception {
protected <T extends TextDisplayPropertyEditor> T createEditor(Class<T> clazz,
Map<String, Object> parameters) throws Exception {
initTestSourceState();
T editor = clazz.newInstance();
T editor = clazz.getDeclaredConstructor().newInstance();
IConfigurablePropertyObject configurableEditor = (IConfigurablePropertyObject) editor;
configurableEditor.configure(m_lastState, parameters);
return editor;
Expand Down
Loading