Skip to content

Fix #313. Add inner class support to ImportHandler #314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2025
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
32 changes: 25 additions & 7 deletions api/src/main/java/jakarta/el/ImportHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2019 Oracle and/or its affiliates and others.
* Copyright (c) 2012, 2025 Oracle and/or its affiliates and others.
* All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -14,7 +14,6 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package jakarta.el;

import static java.lang.reflect.Modifier.isAbstract;
Expand Down Expand Up @@ -47,7 +46,7 @@ public class ImportHandler {
/**
* Import a static field or method.
*
* @param name The static member name, including the full class name, to be imported
* @param name The static member name, including the full class name (in canonical form), to be imported
* @throws ELException if the name does not include a ".".
*/
public void importStatic(String name) throws ELException {
Expand All @@ -65,7 +64,7 @@ public void importStatic(String name) throws ELException {
/**
* Import a class.
*
* @param name The full class name of the class to be imported
* @param name The full class name (in canonical form) of the class to be imported
* @throws ELException if the name does not include a ".".
*/
public void importClass(String name) throws ELException {
Expand Down Expand Up @@ -144,6 +143,25 @@ private Class<?> resolveClassFor(String className) {
if (c != null) {
checkModifiers(c.getModifiers());
classMap.put(className, c);
return c;
}

// Might be an inner class
StringBuilder sb = new StringBuilder(className);
int replacementPosition = sb.lastIndexOf(".");
while (replacementPosition > -1) {
sb.setCharAt(replacementPosition, '$');
c = getClassFor(sb.toString());
if (c != null) {
checkModifiers(c.getModifiers());
classMap.put(sb.toString(), c);
break;
}
replacementPosition = sb.lastIndexOf(".", replacementPosition);
}

if (c == null) {
notAClass.add(className);
}

return c;
Expand All @@ -155,11 +173,11 @@ private Class<?> getClassFor(String className) {
return Class.forName(className, false, Thread.currentThread().getContextClassLoader());
// Some operating systems have case-insensitive path names. An example is Windows if className is
// attempting to be resolved from a wildcard import a java.lang.NoClassDefFoundError may be thrown as
// the expected case for the type likely doesn't match. See
// https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8024775 and
// the expected case for the type likely doesn't match. See
// https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8024775 and
// https://bugs.openjdk.java.net/browse/JDK-8133522.
} catch (ClassNotFoundException | NoClassDefFoundError ex) {
notAClass.add(className);
// Ignore
}
}

Expand Down
35 changes: 35 additions & 0 deletions api/src/test/java/jakarta/el/TestImportHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2025 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package jakarta.el;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class TestImportHandler {

/*
* https://github.com/jakartaee/expression-language/issues/313
*/
@Test
public void testResolveInnerClass() throws Exception {
ImportHandler importHandler = new ImportHandler();

importHandler.importClass("jakarta.el.TesterUtil.Numbers");
Class<?> clazz = importHandler.resolveClass("Numbers");

Assertions.assertEquals(TesterUtil.Numbers.class, clazz);
}
}
25 changes: 25 additions & 0 deletions api/src/test/java/jakarta/el/TesterUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2025 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package jakarta.el;

public class TesterUtil {

public enum Numbers {
ONE,
TWO,
THREE
}
}
6 changes: 5 additions & 1 deletion spec/src/main/asciidoc/ELSpec.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
:sectnums!:
== Jakarta Expression Language, Version 6.1

Copyright (c) 2013, 2024 Oracle and/or its affiliates and others.
Copyright (c) 2013, 2025 Oracle and/or its affiliates and others.
All rights reserved.

Eclipse is a registered trademark of the Eclipse Foundation. Jakarta
Expand Down Expand Up @@ -3000,6 +3000,10 @@ This appendix is non-normative.

=== Changes between 6.1 and 6.0

* https://github.com/jakartaee/expression-language/issues/313[#313]
Add support for inner classes when using the `ImportHandler` and clarify that
the import handler expects canonical class names where full class names are
required.

=== Changes between 6.0 and 5.0

Expand Down