lombok.ast.ConstructorDeclaration Java Examples

The following examples show how to use lombok.ast.ConstructorDeclaration. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SharedPrefsDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Nullable
private static VariableDefinition getLhs(@NonNull Node node) {
    while (node != null) {
        Class<? extends Node> type = node.getClass();
        // The Lombok AST uses a flat hierarchy of node type implementation classes
        // so no need to do instanceof stuff here.
        if (type == MethodDeclaration.class || type == ConstructorDeclaration.class) {
            return null;
        }
        if (type == VariableDefinition.class) {
            return (VariableDefinition) node;
        }

        node = node.getParent();
    }

    return null;
}
 
Example #2
Source File: JavaContext.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
public static Node findSurroundingMethod(Node scope) {
    while (scope != null) {
        Class<? extends Node> type = scope.getClass();
        // The Lombok AST uses a flat hierarchy of node type implementation classes
        // so no need to do instanceof stuff here.
        if (type == MethodDeclaration.class || type == ConstructorDeclaration.class) {
            return scope;
        }

        scope = scope.getParent();
    }

    return null;
}
 
Example #3
Source File: JavaVisitor.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean visitConstructorDeclaration(ConstructorDeclaration node) {
    List<VisitingDetector> list = mNodeTypeDetectors.get(ConstructorDeclaration.class);
    if (list != null) {
        for (VisitingDetector v : list) {
            v.getVisitor().visitConstructorDeclaration(node);
        }
    }
    return false;
}
 
Example #4
Source File: ApiDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
@Override
public List<Class<? extends lombok.ast.Node>> getApplicableNodeTypes() {
    List<Class<? extends lombok.ast.Node>> types =
            new ArrayList<Class<? extends lombok.ast.Node>>(2);
    types.add(ImportDeclaration.class);
    types.add(Select.class);
    types.add(MethodDeclaration.class);
    types.add(ConstructorDeclaration.class);
    types.add(VariableDefinitionEntry.class);
    types.add(VariableReference.class);
    types.add(Try.class);
    return types;
}
 
Example #5
Source File: StringFormatDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/** Returns the parent method of the given AST node */
@Nullable
public static lombok.ast.Node getParentMethod(@NonNull lombok.ast.Node node) {
    lombok.ast.Node current = node.getParent();
    while (current != null
            && !(current instanceof MethodDeclaration)
            && !(current instanceof ConstructorDeclaration)) {
        current = current.getParent();
    }

    return current;
}
 
Example #6
Source File: ToastDetector.java    From MeituanLintDemo with Apache License 2.0 5 votes vote down vote up
public static Node findSurroundingMethod(Node scope) {
    while(true) {
        if(scope != null) {
            Class type = scope.getClass();
            if(type != MethodDeclaration.class && type != ConstructorDeclaration.class && !isLambdaExpression(type)) {
                scope = scope.getParent();
                continue;
            }

            return scope;
        }

        return null;
    }
}
 
Example #7
Source File: JavaVariablesDetector.java    From lewis with Apache License 2.0 5 votes vote down vote up
/**
 * Check if a variable declaration if inside a class scope (skip other local variables).
 *
 * @param variableDeclaration represents the variable.
 * @return true if it is inside a class, false if not.
 */
private boolean hasClassParent(VariableDeclaration variableDeclaration) {
    MethodDeclaration methodDeclaration = variableDeclaration.astDefinition().upIfParameterToMethodDeclaration();
    ConstructorDeclaration constructorDeclaration = variableDeclaration.astDefinition().upIfParameterToConstructorDeclaration();
    Block block = variableDeclaration.astDefinition().upUpIfLocalVariableToBlock();
    return methodDeclaration == null && constructorDeclaration == null && block == null;
}
 
Example #8
Source File: ApiDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean visitConstructorDeclaration(ConstructorDeclaration node) {
    mLocalVars = null;
    mCurrentMethod = node;
    return super.visitConstructorDeclaration(node);
}
 
Example #9
Source File: SharedPrefsDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean visitMethodInvocation(MethodInvocation node) {
    if (node == mTarget) {
        mSeenTarget = true;
    } else if (mAllowCommitBeforeTarget || mSeenTarget || node.astOperand() == mTarget) {
        String name = node.astName().astValue();
        boolean isCommit = "commit".equals(name);
        if (isCommit || "apply".equals(name)) { //$NON-NLS-1$ //$NON-NLS-2$
            // TODO: Do more flow analysis to see whether we're really calling commit/apply
            // on the right type of object?
            mFound = true;

            ResolvedNode resolved = mContext.resolve(node);
            if (resolved instanceof JavaParser.ResolvedMethod) {
                ResolvedMethod method = (ResolvedMethod) resolved;
                JavaParser.ResolvedClass clz = method.getContainingClass();
                if (clz.isSubclassOf("android.content.SharedPreferences.Editor", false)
                        && mContext.getProject().getMinSdkVersion().getApiLevel() >= 9) {
                    // See if the return value is read: can only replace commit with
                    // apply if the return value is not considered
                    Node parent = node.getParent();
                    boolean returnValueIgnored = false;
                    if (parent instanceof MethodDeclaration ||
                            parent instanceof ConstructorDeclaration ||
                            parent instanceof ClassDeclaration ||
                            parent instanceof Block ||
                            parent instanceof ExpressionStatement) {
                        returnValueIgnored = true;
                    } else if (parent instanceof Statement) {
                        if (parent instanceof If) {
                            returnValueIgnored = ((If) parent).astCondition() != node;
                        } else if (parent instanceof Return) {
                            returnValueIgnored = false;
                        } else if (parent instanceof VariableDeclaration) {
                            returnValueIgnored = false;
                        } else if (parent instanceof For) {
                            returnValueIgnored = ((For) parent).astCondition() != node;
                        } else if (parent instanceof While) {
                            returnValueIgnored = ((While) parent).astCondition() != node;
                        } else if (parent instanceof DoWhile) {
                            returnValueIgnored = ((DoWhile) parent).astCondition() != node;
                        } else if (parent instanceof Case) {
                            returnValueIgnored = ((Case) parent).astCondition() != node;
                        } else if (parent instanceof Assert) {
                            returnValueIgnored = ((Assert) parent).astAssertion() != node;
                        } else {
                            returnValueIgnored = true;
                        }
                    }
                    if (returnValueIgnored && isCommit) {
                        String message = "Consider using `apply()` instead; `commit` writes "
                                + "its data to persistent storage immediately, whereas "
                                + "`apply` will handle it in the background";
                        mContext.report(ISSUE, node, mContext.getLocation(node), message);
                    }
                }
            }
        }
    }

    return super.visitMethodInvocation(node);
}
 
Example #10
Source File: AnnotationDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
private boolean checkId(Annotation node, String id) {
    IssueRegistry registry = mContext.getDriver().getRegistry();
    Issue issue = registry.getIssue(id);
    // Special-case the ApiDetector issue, since it does both source file analysis
    // only on field references, and class file analysis on the rest, so we allow
    // annotations outside of methods only on fields
    if (issue != null && !issue.getImplementation().getScope().contains(Scope.JAVA_FILE)
            || issue == ApiDetector.UNSUPPORTED) {
        // Ensure that this isn't a field
        Node parent = node.getParent();
        while (parent != null) {
            if (parent instanceof MethodDeclaration
                    || parent instanceof ConstructorDeclaration
                    || parent instanceof Block) {
                break;
            } else if (parent instanceof TypeBody) { // It's a field
                return true;
            } else if (issue == ApiDetector.UNSUPPORTED
                    && parent instanceof VariableDefinition) {
                VariableDefinition definition = (VariableDefinition) parent;
                for (VariableDefinitionEntry entry : definition.astVariables()) {
                    Expression initializer = entry.astInitializer();
                    if (initializer instanceof Select) {
                        return true;
                    }
                }
            }
            parent = parent.getParent();
            if (parent == null) {
                return true;
            }
        }

        // This issue doesn't have AST access: annotations are not
        // available for local variables or parameters
        Node scope = getAnnotationScope(node);
        mContext.report(INSIDE_METHOD, scope, mContext.getLocation(node), String.format(
            "The `@SuppressLint` annotation cannot be used on a local " +
            "variable with the lint check '%1$s': move out to the " +
            "surrounding method", id));
        return false;
    }

    return true;
}