Skip to content

Commit

Permalink
[incubator-kie-drools-5932] [new-parser] Anonymous classes not suppor…
Browse files Browse the repository at this point in the history
…ted (#5979)
  • Loading branch information
tkobayas committed Oct 11, 2024
1 parent 57c5f39 commit cf9960c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
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

0 comments on commit cf9960c

Please sign in to comment.