Skip to content
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

UnsupportedOperationException with LexicalPreservingPrinter when removing a sealed modified #4245

Closed
blacelle opened this issue Dec 4, 2023 · 1 comment · Fixed by #4253

Comments

@blacelle
Copy link
Contributor

blacelle commented Dec 4, 2023

In #3935, some issue led to a workaround requesting to drop all modifiers (before applying new ones). This workaround leads to issue with LexicalPreservingPrinter and sealed classes (JDK17).

Here is a reproduction case:

import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ParserConfiguration.LanguageLevel;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter;
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;

// https://github.com/solven-eu/cleanthat/issues/713
public class SealedPermitsParsing {
	static final String testCase =
			"public sealed interface IUpdatePortCommand permits UpdateScheduleCommand, UpdateStateCommand {}";

	public static void main(String[] args) {
		System.out.println(testCase);

		ParserConfiguration parserConfiguration = new ParserConfiguration();
		parserConfiguration.setLanguageLevel(LanguageLevel.JAVA_17);
		StaticJavaParser.setConfiguration(
				parserConfiguration.setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver(true))));
		var unit = StaticJavaParser.parse(testCase);

		unit = LexicalPreservingPrinter.setup(unit);

		ClassOrInterfaceDeclaration classOrInterface = unit.findFirst(ClassOrInterfaceDeclaration.class).get();
		classOrInterface.setModifiers();

		System.out.println(classOrInterface);
	}
}

It fails with:

Exception in thread "main" java.lang.UnsupportedOperationException: Csm token CsmToken(property:{) NodeText TOKEN "permits"   <46>   (line 1,col 44)-(line 1,col 50)
	at com.github.javaparser.printer.lexicalpreservation.Difference.applyKeptDiffElement(Difference.java:655)
	at com.github.javaparser.printer.lexicalpreservation.Difference.apply(Difference.java:363)
	at com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.calculatePropertyChange(LexicalDifferenceCalculator.java:176)
	at com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter$Observer.concretePropertyChange(LexicalPreservingPrinter.java:199)
	at com.github.javaparser.ast.observer.PropagatingAstObserver.propertyChange(PropagatingAstObserver.java:62)
	at com.github.javaparser.ast.Node.lambda$notifyPropertyChange$7(Node.java:629)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
	at com.github.javaparser.ast.Node.notifyPropertyChange(Node.java:629)
	at com.github.javaparser.ast.body.TypeDeclaration.setModifiers(TypeDeclaration.java:133)
	at com.github.javaparser.ast.body.TypeDeclaration.setModifiers(TypeDeclaration.java:51)
	at com.github.javaparser.ast.nodeTypes.NodeWithModifiers.setModifiers(NodeWithModifiers.java:96)
	at eu.solven.cleanthat.engine.java.refactorer.report_javaparser.SealedPermitsParsing.main(SealedPermitsParsing.java:43)
@jlerbsc
Copy link
Collaborator

jlerbsc commented Dec 5, 2023

This is a missing feature in Concrete Syntax Model.

jlerbsc added a commit that referenced this issue Dec 11, 2023
fix: issue #4245 UnsupportedOperationException with LexicalPreservingPrinter when removing a sealed modified
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants