Skip to content

Commit

Permalink
Added NullEnumConstantDeclaration node and resolved the issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
subyssurendran666 committed Jan 3, 2025
1 parent f529052 commit 5ba631e
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,13 @@
*******************************************************************************/
package org.eclipse.jdt.core.tests.dom;

import java.util.Hashtable;
import java.util.List;
import junit.framework.Test;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ImplicitTypeDeclaration;
import org.eclipse.jdt.core.dom.ImportDeclaration;
import org.eclipse.jdt.core.dom.Javadoc;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.*;

public class ASTConverter_23Test extends ConverterTestSetup {

Expand Down Expand Up @@ -133,4 +126,33 @@ void main() {
assertEquals("Not a Block", block.getNodeType(), ASTNode.BLOCK);
assertEquals("Block startPosition is not correct", block.getStartPosition(), 21);
}

public void testBug549248_01() throws CoreException {
String contents = """
public enum X {
JAPAN(new java.lang.String[]{"440", "441"}) {
};
public enum LoginType {
public static final com.naver.linewebtoon.common.localization.X.LoginType EMAIL = null;
}
}
""";

ASTParser parser = ASTParser.newParser(AST.getJLSLatest());
parser.setSource(contents.toCharArray());
parser.setEnvironment(null, null, null, true);
parser.setResolveBindings(false);

Hashtable<String, String> options = JavaCore.getDefaultOptions();
options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_23);
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_23);
options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_23);

parser.setCompilerOptions(options);

ASTNode node = parser.createAST(null);

assertNotNull("ASTNode creation failed. Node is null!", node);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2024 IBM Corporation and others.
* Copyright (c) 2000, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -9528,7 +9528,8 @@ public void testNodeTypeConstants() throws Exception {
ASTNode.JAVADOC_TEXT_ELEMENT,
ASTNode.RECORD_PATTERN,
ASTNode.EitherOr_MultiPattern,
ASTNode.UNNAMED_CLASS
ASTNode.UNNAMED_CLASS,
ASTNode.NUll_ENUM_CONSTANT_DECLARATION
};

// assert that nodeType values are correct:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2024 IBM Corporation and others.
* Copyright (c) 2000, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -1831,6 +1831,9 @@ public EnumConstantDeclaration convert(org.eclipse.jdt.internal.compiler.ast.Fie
if (anonymousType != null) {
AnonymousClassDeclaration anonymousClassDeclaration = new AnonymousClassDeclaration(this.ast);
int start = retrieveStartBlockPosition(anonymousType.sourceEnd, anonymousType.bodyEnd);
if(start < 0) {
return createFakeEnumConstantDeclaration(enumConstant);
}
int end = retrieveRightBrace(anonymousType.bodyEnd +1, declarationSourceEnd);
if (end == -1) end = anonymousType.bodyEnd;
anonymousClassDeclaration.setSourceRange(start, end - start + 1);
Expand Down Expand Up @@ -4814,6 +4817,16 @@ protected Pattern createFakeNullPattern(org.eclipse.jdt.internal.compiler.ast.Pa
return nullPattern;
}

protected EnumConstantDeclaration createFakeEnumConstantDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration enumConstant) {
if (enumConstant == null) return null;
NullEnumConstantDeclaration nullEnumConstantDeclaration = new NullEnumConstantDeclaration(this.ast);
nullEnumConstantDeclaration.setFlags(nullEnumConstantDeclaration.getFlags() | ASTNode.MALFORMED);
int start = enumConstant.sourceStart;
int end = enumConstant.sourceEnd;
nullEnumConstantDeclaration.setSourceRange(start, end - start + 1);
return nullEnumConstantDeclaration;
}

/**
* @return a new modifier
*/
Expand Down
11 changes: 10 additions & 1 deletion org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTNode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2024 IBM Corporation and others.
* Copyright (c) 2000, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -1081,6 +1081,13 @@ public abstract class ASTNode {
*/
public static final int UNNAMED_CLASS = 115;

/**
* Node type constant indicating a node of type
* <code>NullPattern</code>.
* @see NullPattern
* @since 3.41
*/
public static final int NUll_ENUM_CONSTANT_DECLARATION = 116;
/**
* Returns the node class for the corresponding node type.
*
Expand Down Expand Up @@ -1215,6 +1222,8 @@ public static Class nodeClassForType(int nodeType) {
return NameQualifiedType.class;
case NORMAL_ANNOTATION :
return NormalAnnotation.class;
case NUll_ENUM_CONSTANT_DECLARATION :
return NullEnumConstantDeclaration.class;
case NULL_LITERAL :
return NullLiteral.class;
case NULL_PATTERN :
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2023 IBM Corporation and others.
* Copyright (c) 2000, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -1235,6 +1235,23 @@ public boolean visit(NullPattern node) {
return true;
}

/**
* Visits the given type-specific AST node.
* <p>
* The default implementation does nothing and return true.
* Subclasses may reimplement.
* </p>
*
* @param node the node to visit
* @return <code>true</code> if the children of this node should be
* visited, and <code>false</code> if the children of this node should
* be skipped
* @since 3.41
*/
public boolean visit(NullEnumConstantDeclaration node) {
return true;
}

/**
* Visits the given type-specific AST node.
* <p>
Expand Down Expand Up @@ -2883,6 +2900,19 @@ public void endVisit(NullPattern node) {
// default implementation: do nothing
}

/**
* End of visit the given type-specific AST node.
* <p>
* The default implementation does nothing. Subclasses may reimplement.
* </p>
*
* @param node the node to visit
* @since 3.41
*/
public void endVisit(NullEnumConstantDeclaration node) {
//default implementation: do nothing
}

/**
* End of visit the given type-specific AST node.
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*******************************************************************************
* Copyright (c) 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/

package org.eclipse.jdt.core.dom;

import java.util.ArrayList;
import java.util.List;

/**
* Null EnumConstantDeclaration node.
*
* @since 3.41
* @noinstantiate This class is not intended to be instantiated by clients.
*/
@SuppressWarnings("rawtypes")
public class NullEnumConstantDeclaration extends EnumConstantDeclaration {

/**
* Null EnumConstantDeclaration Pattern node.
*
* @since 3.27
*/
private static final List PROPERTY_DESCRIPTORS;

static {
List propertyList = new ArrayList(1);
createPropertyList(NullPattern.class, propertyList);
PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
}

/**
* Returns a list of structural property descriptors for this node type.
* Clients must not modify the result.
*
* @param apiLevel the API level; one of the
* <code>AST.JLS*</code> constants
* @return a list of property descriptors (element type:
* {@link StructuralPropertyDescriptor})
* @since 3.38
*/
public static List propertyDescriptors(int apiLevel) {
return PROPERTY_DESCRIPTORS;
}

/**
* Creates a new unparented null literal node owned by the given AST.
* <p>
* N.B. This constructor is package-private.
* </p>
*
* @param ast the AST that is to own this node
*/
NullEnumConstantDeclaration(AST ast) {
super(ast);
supportedOnlyIn21();
}

@Override
ASTNode clone0(AST target) {
NullPattern result = new NullPattern(target);
result.setSourceRange(getStartPosition(), getLength());
return result;
}

@Override
void accept0(ASTVisitor visitor) {
visitor.visit(this);
visitor.endVisit(this);
}

@Override
int memSize() {
return BASE_NODE_SIZE;
}

@Override
int treeSize() {
return memSize();
}

}

0 comments on commit 5ba631e

Please sign in to comment.