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

[incubator-kie-drools-5932] [new-parser] Anonymous classes not supported #5979

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
Expand Up @@ -5304,4 +5304,27 @@ void accumulateWithEmptyActionAndReverse() {
});
});
}

@Test
void functionWithAnonymousClass() {
final String text = "function Function<String, Integer> f() {\n" +
" return new Function<String, Integer>() {\n" +
" public Integer apply(String s) {\n" +
" return s.length();\n" +
" }\n" +
" };\n" +
"}";
PackageDescr packageDescr = parseAndGetPackageDescr(text);
FunctionDescr function = packageDescr.getFunctions().get(0);

assertThat(function.getName()).isEqualTo("f");
assertThat(function.getReturnType()).isEqualToIgnoringWhitespace("Function<String, Integer>");
assertThat(function.getParameterTypes()).isEmpty();
assertThat(function.getParameterNames()).isEmpty();
assertThat(function.getBody()).isEqualToIgnoringWhitespace("return new Function<String, Integer>() {\n" +
" public Integer apply(String s) {\n" +
" return s.length();\n" +
" }\n" +
" };");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ identifierSuffix

creator
: nonWildcardTypeArguments? createdName
(arrayCreatorRest | classCreatorRest)
(arrayCreatorRest | classCreatorRestExpr)
;

createdName
Expand All @@ -821,7 +821,7 @@ createdName

// Old parser cannot parse innerCreator with selector expression (outer.new InnerClass() != null) TODO: Delete this after investigation
innerCreator
: {!(helper.validateIdentifierKey(DroolsSoftKeywords.INSTANCEOF))}? drlIdentifier classCreatorRest
: {!(helper.validateIdentifierKey(DroolsSoftKeywords.INSTANCEOF))}? drlIdentifier classCreatorRestExpr
;

arrayCreatorRest
Expand All @@ -840,7 +840,7 @@ arrayInitializer
: LBRACE (variableInitializer (COMMA variableInitializer)* (COMMA)? )? RBRACE
;

classCreatorRest
classCreatorRestExpr // do not overwrite JavaParser.g4 classCreatorRest
: arguments //classBody? //sotty: restored classBody to allow for inline, anonymous classes
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public FunctionDescr visitFunctiondef(DRLParser.FunctiondefContext ctx) {
.withParserRuleContext(ctx)
.build();
if (ctx.typeTypeOrVoid() != null) {
functionDescr.setReturnType(ctx.typeTypeOrVoid().getText());
functionDescr.setReturnType(getTextPreservingWhitespace(ctx.typeTypeOrVoid()));
} else {
functionDescr.setReturnType("void");
}
Expand Down
Loading