Skip to content

Commit

Permalink
fix: Eliminates the use of instanceof in favour of using the API
Browse files Browse the repository at this point in the history
  • Loading branch information
jlerbsc committed Dec 31, 2023
1 parent 6d50915 commit 84a4bac
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ public void visit(final Parameter n, final Void arg) {
printAnnotations(n.getVarArgsAnnotations(), false, arg);
printer.print("...");
}
if (!(n.getType() instanceof UnknownType)) {
if (!(n.getType().isUnknownType())) {
printer.print(" ");
}
n.getName().accept(this, arg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ public void visit(final Parameter n, final Void arg) {
printAnnotations(n.getVarArgsAnnotations(), false, arg);
printer.print("...");
}
if (!(n.getType() instanceof UnknownType)) {
if (!(n.getType().isUnknownType())) {
printer.print(" ");
}
n.getName().accept(this, arg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public ResolvedType visit(VariableDeclarator node, Boolean solveLambdas) {

@Override
public ResolvedType visit(Parameter node, Boolean solveLambdas) {
if (node.getType() instanceof UnknownType) {
if (node.getType().isUnknownType()) {
throw new IllegalStateException("Parameter has unknown type: " + node);
}
return facade.convertToUsage(node.getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public boolean isVariadic() {

@Override
public ResolvedType getType() {
if (wrappedNode.getType() instanceof UnknownType && JavaParserFactory.getContext(wrappedNode, typeSolver) instanceof LambdaExprContext) {
if (wrappedNode.getType().isUnknownType() && JavaParserFactory.getContext(wrappedNode, typeSolver) instanceof LambdaExprContext) {
Optional<Value> value = JavaParserFactory.getContext(wrappedNode, typeSolver).solveSymbolAsValue(wrappedNode.getNameAsString());
if (value.isPresent()) {
return value.get().getType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
public class ExpressionHelper {

public static boolean isExplicitlyTyped(LambdaExpr lambdaExpr) {
return lambdaExpr.getParameters().stream().allMatch(p -> !(p.getType() instanceof UnknownType));
return lambdaExpr.getParameters().stream().allMatch(p -> !(p.getType().isUnknownType()));
}

public static List<Expression> getResultExpressions(BlockStmt blockStmt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ private List<ResolvedType> formalParameterTypes(ResolvedMethodDeclaration method
}

private boolean isImplicitlyTyped(LambdaExpr lambdaExpr) {
return lambdaExpr.getParameters().stream().anyMatch(p -> p.getType() instanceof UnknownType);
return lambdaExpr.getParameters().stream().anyMatch(p -> p.getType().isUnknownType());
}

private boolean isInexact(MethodReferenceExpr methodReferenceExpr) {
Expand All @@ -596,7 +596,7 @@ private boolean isPertinentToApplicability(Expression argument) {
//
// - An implicitly typed lambda expression (§15.27.1).

if (argument instanceof LambdaExpr) {
if (argument.isLambdaExpr()) {
LambdaExpr lambdaExpr = (LambdaExpr)argument;
if (isImplicitlyTyped(lambdaExpr)) {
return false;
Expand All @@ -605,7 +605,7 @@ private boolean isPertinentToApplicability(Expression argument) {

// - An inexact method reference expression (§15.13.1).

if (argument instanceof MethodReferenceExpr) {
if (argument.isMethodReferenceExpr()) {
MethodReferenceExpr methodReferenceExpr = (MethodReferenceExpr)argument;
if (isInexact(methodReferenceExpr)) {
return false;
Expand All @@ -616,37 +616,37 @@ private boolean isPertinentToApplicability(Expression argument) {
// explicitly typed lambda expression or an exact method reference expression for which the
// corresponding target type (as derived from the signature of m) is a type parameter of m.

if (argument instanceof LambdaExpr) {
if (argument.isLambdaExpr()) {
throw new UnsupportedOperationException();
}

if (argument instanceof MethodReferenceExpr) {
if (argument.isMethodReferenceExpr()) {
throw new UnsupportedOperationException();
}

// - An explicitly typed lambda expression whose body is an expression that is not pertinent to applicability.

if (argument instanceof LambdaExpr) {
if (argument.isLambdaExpr()) {
throw new UnsupportedOperationException();
}

// - An explicitly typed lambda expression whose body is a block, where at least one result expression is not
// pertinent to applicability.

if (argument instanceof LambdaExpr) {
if (argument.isLambdaExpr()) {
throw new UnsupportedOperationException();
}

// - A parenthesized expression (§15.8.5) whose contained expression is not pertinent to applicability.

if (argument instanceof EnclosedExpr) {
if (argument.isEnclosedExpr()) {
EnclosedExpr enclosedExpr = (EnclosedExpr)argument;
return isPertinentToApplicability(enclosedExpr.getInner());
}

// - A conditional expression (§15.25) whose second or third operand is not pertinent to applicability.

if (argument instanceof ConditionalExpr) {
if (argument.isConditionalExpr()) {
ConditionalExpr conditionalExpr = (ConditionalExpr)argument;
return isPertinentToApplicability(conditionalExpr.getThenExpr()) &&
isPertinentToApplicability(conditionalExpr.getElseExpr());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public ReductionResult reduce(BoundSet currentBoundSet) {
// - If the lambda parameters have explicitly declared types F1, ..., Fn and the function type
// has parameter types G1, ..., Gn, then i) for all i (1 ≤ i ≤ n), ‹Fi = Gi›, and ii) ‹T' <: T›.

boolean hasExplicitlyDeclaredTypes = lambdaExpr.getParameters().stream().anyMatch(p -> !(p.getType() instanceof UnknownType));
boolean hasExplicitlyDeclaredTypes = lambdaExpr.getParameters().stream().anyMatch(p -> !(p.getType().isUnknownType()));
if (hasExplicitlyDeclaredTypes) {
throw new UnsupportedOperationException();
}
Expand Down

0 comments on commit 84a4bac

Please sign in to comment.