Skip to content

Commit

Permalink
[1.8] Improve support for outdated JDK versions
Browse files Browse the repository at this point in the history
The JDT compiler is unable to calculate the argument names of a method
binding when a JDK 8 is used and simply returns an empty array. This
then leads to an out-of-bounds exception, as the number of parameter
types no longer matches the number of parameter names.

Note that this issue only happens with a proper JDK 8, but not an e.g.
JDK 17 with compiler compliance set to Java 8.

The parameter names themselves only seem to be relevant for cosmetic
reason, so they are now simply ignored (together with a warning), rather
than causing the editor to crash.

From a technical perspective, the problem can be isolated to one of the
internal classes of the JDT compiler:

org.eclipse.jdt.internal.compiler.classfmt.MethodInfo
 -> getArgumentNames()
 -> readCodeAttribute()
 -> decodeCodeAttribute()

The offset that is supposed to point to the "LocalVariableTable" is
instead pointing to the "LineNumberTable" and as a result, the compiler
is unable to extract the attribute names.
  • Loading branch information
ptziegler committed Nov 2, 2024
1 parent 7bd0510 commit 402f7e0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2024 Google, Inc.
* Copyright (c) 2011, 2024 Google, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -10,12 +10,17 @@
*******************************************************************************/
package org.eclipse.wb.internal.core.utils.ast.binding;

import org.eclipse.wb.internal.core.DesignerPlugin;
import org.eclipse.wb.internal.core.editor.Messages;

import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.dom.IAnnotationBinding;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.osgi.util.NLS;

import org.apache.commons.lang3.ArrayUtils;

Expand All @@ -36,6 +41,7 @@ public final class DesignerMethodBinding implements IMethodBinding {
private final boolean m_varargs;
private final ITypeBinding m_declaringClass;
private final ITypeBinding m_returnType;
private final String m_key;
private ITypeBinding[] m_parameterTypes;
private String[] m_parameterNames;
private ITypeBinding[] m_exceptionTypes;
Expand All @@ -47,6 +53,7 @@ public final class DesignerMethodBinding implements IMethodBinding {
//
////////////////////////////////////////////////////////////////////////////
DesignerMethodBinding(BindingContext context, IMethodBinding binding) {
m_key = binding.getKey();
m_name = binding.getName();
m_modifiers = binding.getModifiers();
m_constructor = binding.isConstructor();
Expand Down Expand Up @@ -90,7 +97,14 @@ public final class DesignerMethodBinding implements IMethodBinding {
*/
public void removeParameterType(int index) {
m_parameterTypes = ArrayUtils.remove(m_parameterTypes, index);
m_parameterNames = ArrayUtils.remove(m_parameterNames, index);
// When using a JDK 8, JDT is unable to calculate the parameter names, leading
// to a mismatch with the number of parameter types
if (index < m_parameterNames.length) {
m_parameterNames = ArrayUtils.remove(m_parameterNames, index);
} else {
String message = NLS.bind(Messages.DesignerMethodBinding_unknownArgumentNames, m_key);
DesignerPlugin.log(Status.warning(message));
}
if (m_methodDeclaration != this) {
m_methodDeclaration.removeParameterType(index);
}
Expand Down Expand Up @@ -235,7 +249,7 @@ public IJavaElement getJavaElement() {

@Override
public String getKey() {
throw new IllegalArgumentException();
return m_key;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/*******************************************************************************
* Copyright (c) 2011, 2024 Google, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Google, Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.wb.internal.core.editor;

import org.eclipse.osgi.util.NLS;
Expand Down Expand Up @@ -60,6 +70,7 @@ public class Messages extends NLS {
public static String ComponentsTreePage_expandAllAction;
public static String DesignComponentsComposite_componentsTitle;
public static String DesignComponentsComposite_propertiesTitle;
public static String DesignerMethodBinding_unknownArgumentNames;
public static String DesignerPalettePopupActions_addCategoryAction;
public static String DesignerPalettePopupActions_addComponentAction;
public static String DesignerPalettePopupActions_addInstanceFactoryAction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ ComponentsTreePage_collapseAllAction=Collapse All
ComponentsTreePage_expandAllAction=Expand All
DesignComponentsComposite_componentsTitle=Components
DesignComponentsComposite_propertiesTitle=Properties
DesignerMethodBinding_unknownArgumentNames=The argument names for the method "{0}" could not be determined. Possible reasons are an old JDK or a bug within JDT.
DesignerPalettePopupActions_addCategoryAction=Add category...
DesignerPalettePopupActions_addComponentAction=Add component...
DesignerPalettePopupActions_addInstanceFactoryAction=Add instance factory
Expand Down

0 comments on commit 402f7e0

Please sign in to comment.