Skip to content

Commit

Permalink
Merge pull request #37 from openrewrite/anonymous-subclasses
Browse files Browse the repository at this point in the history
Don't stop iteration on constructor calls, only anonymous subclasses
  • Loading branch information
knutwannheden authored Nov 8, 2023
2 parents 112b76e + baa413b commit 1a92087
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private static Optional<MethodCall> nearestMethodCall(Cursor cursor) {
}

private static boolean asExpression(Cursor cursor, Predicate<Expression> expressionPredicate) {
return cursor.getValue() instanceof Expression && expressionPredicate.test((Expression) cursor.getValue());
return cursor.getValue() instanceof Expression && expressionPredicate.test(cursor.getValue());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public Validation<TraitErrors, Field> viewOf(Cursor cursor) {
Cursor maybeVariableDecl = cursor.getParentTreeCursor();
Cursor maybeBlock = maybeVariableDecl.getParentTreeCursor();
Cursor maybeClassDecl = maybeBlock.getParentTreeCursor();
if (maybeClassDecl.getValue() instanceof J.ClassDeclaration || maybeClassDecl.getValue() instanceof J.NewClass) {
if (maybeClassDecl.getValue() instanceof J.ClassDeclaration ||
maybeClassDecl.getValue() instanceof J.NewClass && ((J.NewClass) maybeClassDecl.getValue()).getBody() != null) {
return Validation.success(new FieldFromCursor(cursor, cursor.getValue(), maybeVariableDecl.getValue(), maybeBlock));
}
return TraitErrors.invalidTraitCreationError("Field must be declared in a class, interface, or anonymous class");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ static Validation<TraitErrors, Callable> findNearestParentCallable(Cursor cursor
while (path.hasNext()) {
Cursor c = path.next();
Tree t = c.getValue();
if (t instanceof J.ClassDeclaration || t instanceof J.NewClass) {
if (t instanceof J.ClassDeclaration || t instanceof J.NewClass && previous != null && ((J.NewClass) t).getBody() == previous.getValue()) {
break;
}
if (t instanceof J.Block && J.Block.isStaticOrInitBlock(c)) {
Expand All @@ -122,6 +122,12 @@ static Validation<TraitErrors, Callable> findNearestParentCallable(Cursor cursor
if (m.getParameters().contains(previous.<Statement>getValue())) {
break;
}
} else if (t instanceof J.Lambda) {
J.Lambda l = (J.Lambda) t;
assert previous != null : "previous should not be null";
if (l.getParameters().getParameters().contains(previous.<Statement>getValue())) {
break;
}
}
Validation<TraitErrors, Method> v = Method.viewOf(c);
if (v.isSuccess()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public J preVisit(J tree, ExecutionContext executionContext) {
}

@Test
@Disabled("https://github.com/openrewrite/rewrite-analysis/issues/31")
void lambdaException() {
//language=java
rewriteRun(
Expand Down

0 comments on commit 1a92087

Please sign in to comment.