Package com.puppycrawl.tools.checkstyle.api

Examples of com.puppycrawl.tools.checkstyle.api.DetailAST


        if (ScopeUtils.inInterfaceOrAnnotationBlock(aAST)) {
            return;
        }

        // method is ok if it is private or abstract or final
        final DetailAST modifiers = aAST.findFirstToken(TokenTypes.MODIFIERS);
        if (modifiers.branchContains(TokenTypes.LITERAL_PRIVATE)
            || modifiers.branchContains(TokenTypes.ABSTRACT)
            || modifiers.branchContains(TokenTypes.FINAL)
            || modifiers.branchContains(TokenTypes.LITERAL_STATIC))
        {
            return;
        }

        // method is ok if containing class is not visible in API and
        // cannot be extended by 3rd parties (bug #884035)
        if (!ScopeUtils.getSurroundingScope(aAST).isIn(Scope.PROTECTED)) {
            return;
        }

        // method is ok if it is implementation can verified to be empty
        // Note: native methods don't have impl in java code, so
        // implementation can be null even if method not abstract
        final DetailAST implementation = aAST.findFirstToken(TokenTypes.SLIST);
        if ((implementation != null)
            && (implementation.getFirstChild().getType() == TokenTypes.RCURLY))
        {
            return;
        }

        // check if the containing class can be subclassed
        final DetailAST classDef = findContainingClass(aAST);
        final DetailAST classMods =
            classDef.findFirstToken(TokenTypes.MODIFIERS);
        if ((classDef.getType() == TokenTypes.ENUM_DEF)
            || classMods.branchContains(TokenTypes.FINAL))
        {
            return;
        }

        // check if subclassing is prevented by having only private ctors
        final DetailAST objBlock = classDef.findFirstToken(TokenTypes.OBJBLOCK);

        boolean hasDefaultConstructor = true;
        boolean hasExplNonPrivateCtor = false;

        DetailAST candidate = (DetailAST) objBlock.getFirstChild();

        while (candidate != null) {
            if (candidate.getType() == TokenTypes.CTOR_DEF) {
                hasDefaultConstructor = false;

                final DetailAST ctorMods =
                    candidate.findFirstToken(TokenTypes.MODIFIERS);
                if (!ctorMods.branchContains(TokenTypes.LITERAL_PRIVATE)) {
                    hasExplNonPrivateCtor = true;
                    break;
                }
            }
            candidate = (DetailAST) candidate.getNextSibling();
View Full Code Here


     * @param aParam parameter to check.
     */
    private void checkParam(final DetailAST aParam)
    {
        if (!aParam.branchContains(TokenTypes.FINAL)) {
            final DetailAST paramName = aParam.findFirstToken(TokenTypes.IDENT);
            final DetailAST firstNode = CheckUtils.getFirstNode(aParam);
            log(firstNode.getLineNo(), firstNode.getColumnNo(),
                "final.parameter", paramName.getText());
        }
    }
View Full Code Here

     * @param aAST the start node for searching
     * @return the CLASS_DEF node.
     */
    private DetailAST findContainingClass(DetailAST aAST)
    {
        DetailAST searchAST = aAST;
        while ((searchAST.getType() != TokenTypes.CLASS_DEF)
               && (searchAST.getType() != TokenTypes.ENUM_DEF))
        {
            searchAST = searchAST.getParent();
        }
        return searchAST;
    }
View Full Code Here

    }

    @Override
    public void visitToken(DetailAST aAST)
    {
        final DetailAST typeAST = aAST.getParent();
        if (typeAST.getType() != TokenTypes.TYPE) {
            return;
        }
        final DetailAST declAST = typeAST.getParent();
        if (declAST.getType() == TokenTypes.METHOD_DEF) {
            // Do not check method's return type.
            // We have no alternatives here.
            return;
        }

        final DetailAST variableAST = (DetailAST) typeAST.getNextSibling();
        if (variableAST != null) {
            final boolean isJavaStyle =
                (variableAST.getLineNo() > aAST.getLineNo())
                || (variableAST.getColumnNo() > aAST.getColumnNo());

            if (isJavaStyle != mJavaStyle) {
                log(aAST.getLineNo(), aAST.getColumnNo(), "array.type.style");
            }
        }
View Full Code Here

     * @param aAST class definition for check.
     * @return true if a given class declared as abstract.
     */
    private boolean isAbstract(DetailAST aAST)
    {
        final DetailAST abstractAST = aAST.findFirstToken(TokenTypes.MODIFIERS)
            .findFirstToken(TokenTypes.ABSTRACT);

        return abstractAST != null;
    }
View Full Code Here

    }

    @Override
    protected final boolean mustCheckName(DetailAST aAST)
    {
        final DetailAST modifiersAST =
            aAST.findFirstToken(TokenTypes.MODIFIERS);
        final boolean isFinal = (modifiersAST != null)
            && modifiersAST.branchContains(TokenTypes.FINAL);
        return (!isFinal && ScopeUtils.isLocalVariableDef(aAST));
    }
View Full Code Here

     * @param aAST variable def node for check.
     */
    private void visitVariableDef(DetailAST aAST)
    {
        if (mChecking && (aAST.getParent().getType() == TokenTypes.OBJBLOCK)) {
            final DetailAST modifiersAST =
                aAST.findFirstToken(TokenTypes.MODIFIERS);

            if (!(modifiersAST.findFirstToken(TokenTypes.FINAL) != null)) {
                log(aAST.getLineNo(),  aAST.getColumnNo(), "mutable.exception",
                        aAST.findFirstToken(TokenTypes.IDENT).getText());
            }
        }
    }
View Full Code Here

    }

    @Override
    protected final boolean mustCheckName(DetailAST aAST)
    {
        final DetailAST modifiersAST =
            aAST.findFirstToken(TokenTypes.MODIFIERS);
        final boolean isFinal = (modifiersAST != null)
            && modifiersAST.branchContains(TokenTypes.FINAL);
        return (isFinal && ScopeUtils.isLocalVariableDef(aAST));
    }
View Full Code Here

            || (aAST.getParent().getType() != TokenTypes.OBJBLOCK))
        {
            return;
        }

        final DetailAST varNameAST = getVarNameAST(aAST);
        final String varName = varNameAST.getText();
        final boolean inInterfaceOrAnnotationBlock =
            ScopeUtils.inInterfaceOrAnnotationBlock(aAST);
        final Set<String> mods = getModifiers(aAST);
        final String declaredScope = getVisibilityScope(mods);
        final String variableScope =
             inInterfaceOrAnnotationBlock ? "public" : declaredScope;

        if (!("private".equals(variableScope)
                || inInterfaceOrAnnotationBlock // implicitly static and final
                || (mods.contains("static") && mods.contains("final"))
                || ("package".equals(variableScope) && isPackageAllowed())
                || ("protected".equals(variableScope) && isProtectedAllowed())
                || ("public".equals(variableScope)
                   && getPublicMemberRegexp().matcher(varName).find())))
        {
            log(varNameAST.getLineNo(), varNameAST.getColumnNo(),
                    "variable.notPrivate", varName);
        }
    }
View Full Code Here

    }

    @Override
    public void visitToken(DetailAST aAST)
    {
        final DetailAST modifiers = aAST.findFirstToken(TokenTypes.MODIFIERS);

        if (aAST.getType() == TokenTypes.CLASS_DEF) {
            final boolean isFinal = (modifiers != null)
                    && modifiers.branchContains(TokenTypes.FINAL);
            final boolean isAbstract = (modifiers != null)
                    && modifiers.branchContains(TokenTypes.ABSTRACT);
            mClasses.push(new ClassDesc(isFinal, isAbstract));
        }
        else if (!ScopeUtils.inEnumBlock(aAST)) { //ctors in enums don't matter
            final ClassDesc desc = mClasses.peek();
            if ((modifiers != null)
                && modifiers.branchContains(TokenTypes.LITERAL_PRIVATE))
            {
                desc.reportPrivateCtor();
            }
            else {
                desc.reportNonPrivateCtor();
View Full Code Here

TOP

Related Classes of com.puppycrawl.tools.checkstyle.api.DetailAST

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.