Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8013193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:34:14+00:00 2026-06-04T19:34:14+00:00

I am trying to create a rule for checkstyle, that will prevent writing inline

  • 0

I am trying to create a rule for checkstyle, that will prevent writing inline annotations usage, like this:

@Entity MyClass someEntity;
@Foo(a="B") public void bar(Baz baz) {
}

but will not prevent thinks like that:

public void bar(@Param Baz baz) {
}

Is there any way to achieve that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:34:18+00:00Added an answer on June 4, 2026 at 7:34 pm

    Most of this answer was inspired by Checkstyle’s “Writing Checks” article. Most of the work is done in AnnotationSameLineCheck.

    AnnotationSameLineCheck.java

    This Java file was inspired by the “Visitor In Action” section of the “Writing Checks” article.

    getDefaultTokens defines which parts (a.k.a. tokens) of a Java file we’re interested in. Out first one might think that we’d be interested in TokenTypes.ANNOTATION, but this is not the case. We’re not interested in TokenTypes.ANNOTATION because we don’t want to inspect all annotations; we actually want to disregard TokenTypes.PARAMETER_DEF.

    What are we interested in then? We’re actually interested in those parts of a Java file that can be annotated (i.e., the class definition TokenTypes.CLASS_DEF, method definitions TokenTypes.METHOD_DEF, etc.). Conveniently, Checkstyle’s API has a method that can tell us if a token is annotated. That method isAnnotationUtility.containsAnnotation, which is used in visitToken.

    The algorithm used to determine if an annotation is on the same line as other parts of a Java file is as follows:

    1. Determine if a particular token contains an annotation. If not, do nothing.
    2. Find the annotation token.
    3. Find the token before the annotation token.
    4. Find the token after the annotation token.
    5. If the annotation token is on the same line as the previous token, log an error.
    6. If the annotation token is on the same line as the next token, log an error.

    This algorithm is found in visitToken.

    package example;
    
    import com.puppycrawl.tools.checkstyle.api.AnnotationUtility;
    import com.puppycrawl.tools.checkstyle.api.Check;
    import com.puppycrawl.tools.checkstyle.api.DetailAST;
    import com.puppycrawl.tools.checkstyle.api.TokenTypes;
    
    public class AnnotationSameLineCheck extends Check {
        @Override
        public int[] getDefaultTokens() {
            // PACKAGE_DEF and PARAMETER_DEF were left out of the list
            return new int[] { TokenTypes.ANNOTATION_DEF, //
                    TokenTypes.ANNOTATION_FIELD_DEF, //
                    TokenTypes.CLASS_DEF, //
                    TokenTypes.CTOR_DEF, //
                    TokenTypes.ENUM_DEF, //
                    TokenTypes.ENUM_CONSTANT_DEF, //
                    TokenTypes.INTERFACE_DEF, //
                    TokenTypes.METHOD_DEF, //
                    TokenTypes.VARIABLE_DEF };
        }
    
        @Override
        public void visitToken(DetailAST ast) {
            if (AnnotationUtility.containsAnnotation(ast)) {
                final DetailAST holder = AnnotationUtility.getAnnotationHolder(ast);
                final DetailAST annotation = getAnnotationAst(holder);
                final DetailAST prev = getPreviousSibling(annotation, holder, ast);
                final DetailAST next = getNextSibling(annotation, holder, ast);
                if (isPreviousSiblingOnSameLine(prev, annotation) || //
                        isNextSiblingOnSameLine(annotation, next)) {
                    log(annotation.getLineNo(), //
                            annotation.getColumnNo(), //
                            "Annotations must exist on their own line");
                }
            }
        }
    
        private static boolean isPreviousSiblingOnSameLine(DetailAST prev, DetailAST annotation) {
            if (prev == null) {
                return false;
            } else if (prev.getLastChild() == null) {
                return prev.getLineNo() == annotation.getLineNo();
            }
            return prev.getLastChild().getLineNo() == annotation.getLineNo();
        }
    
        private static boolean isNextSiblingOnSameLine(DetailAST annotation, DetailAST next) {
            if (next == null) {
                return false;
            }
            return annotation.getLineNo() == next.getLineNo();
        }
    
        private static DetailAST getAnnotationAst(DetailAST aHolderAst) {
            if (aHolderAst.getType() == TokenTypes.ANNOTATIONS) {
                return aHolderAst;
            } else if (aHolderAst.getType() == TokenTypes.MODIFIERS) {
                return aHolderAst.findFirstToken(TokenTypes.ANNOTATION);
            }
            throw new AssertionError("aHolder must be one of TokenTypes.ANNOTATIONS or TokenTypes.MODIFIERS but was " + aHolderAst);
        }
    
        private static DetailAST getPreviousSibling(DetailAST annotation, DetailAST holder, DetailAST ast) {
            if (annotation.getPreviousSibling() != null) {
                return annotation.getPreviousSibling();
            } else if (holder.getPreviousSibling() != null) {
                return holder.getPreviousSibling();
            }
            return ast.getPreviousSibling();
        }
    
        private static DetailAST getNextSibling(DetailAST annotation, DetailAST holder, DetailAST ast) {
            if (annotation.getNextSibling() != null) {
                return annotation.getNextSibling();
            } else if (holder.getNextSibling() != null) {
                return holder.getNextSibling();
            }
            return ast.getNextSibling();
        }
    }
    

    checks.xml

    This XML file was inspired by the “Integrate Your Check” section of the “Writing Checks” article. It is used by Checkstyle to specify which checks to perform on a set of Java files. Note that AnnotationSameLineCheck is fully qualified (i.e., its package is specified in the name). checks.xml is mentioned in the build.xml file.

    <?xml version="1.0"?>
    <!DOCTYPE module PUBLIC
              "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
              "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
    <module name="Checker">
        <module name="TreeWalker">
            <module name="example.AnnotationSameLineCheck"/>
        </module>
    </module>
    

    build.xml

    This Ant build file was inspired by Checkstyle’s “Ant Task” article. Using Ant is only one way to execute Checkstyle. Using the command line is another option.

    <project default="example">
        <taskdef resource="checkstyletask.properties" classpath="target/classes:lib/checkstyle-5.5-all.jar" />
        <target name="example">
            <checkstyle config="checks.xml">
                <fileset dir="src/main/java" includes="**/AnnotatedClass.java" />
                <formatter type="plain" />
            </checkstyle>
        </target>
    </project>
    

    AnnotationClass.java

    One can use the following class to test AnnotationSameLineCheck. It is mentioned in the build.xml file. Note the testing of interface, class, enum, member-variable, method, parameter, and local-variable declarations.

    package example;
        @Deprecated
    class CorrectClassDefA {}
    
    @Deprecated class IncorrectClassDefA {}
    
    abstract
    @Deprecated
    class CorrectClassDefB {}
    
    abstract @SuppressWarnings(value = "unused") class IncorrectClassDefB0 {}
    
    abstract
        @Deprecated class IncorrectClassDefB1 {}
    
    abstract@Deprecated
    class IncorrectClassDefB2 {}
    
    @Deprecated abstract class IncorrectClassDefB3 {}
    
    @Deprecated
    abstract class CorrectClassDefB4 {}
    
    @SuppressWarnings(value = "unused")
    interface CorrectInterfaceDefA {}
    
    @Deprecated interface IncorrectInterfaceDefA {}
    
    abstract
    @Deprecated
    interface CorrectInterfaceDefB {}
    
    abstract @Deprecated interface IncorrectInterfaceDefB0 {}
    
    abstract
    @Deprecated interface IncorrectInterfaceDefB1 {}
    
    abstract @SuppressWarnings(value = "unused")
    interface IncorrectInterfaceDefB2 {}
    
    @SuppressWarnings(value = "unused") abstract interface IncorrectInterfaceDefB3 {}
    
    @SuppressWarnings(value = "unused")
    abstract 
    interface CorrectInterfaceDefB4 {}
    
    @Deprecated
    enum CorrectEnumA {
        @SuppressWarnings(value = "unused")
        CORRECT,
        @Deprecated INCORRECT }
    
    @Deprecated enum 
    IncorrectEnumA {
    @Deprecated
        CORRECT,
        @SuppressWarnings(value = "unused") INCORRECT }
    
    
    public class AnnotatedClass { @Deprecated // incorrect
        public AnnotatedClass() {}
    
        @Deprecated
        AnnotatedClass(int correct) {}
    
        public
        @SuppressWarnings(value = "unused")
        AnnotatedClass(boolean correct, boolean correct0) {}
    
        @SuppressWarnings(value = "unused")
        AnnotatedClass(int correct, int correct0, int correct1) {}
    
        public @SuppressWarnings(value = "unused")
        AnnotatedClass(@Deprecated int bad, int bad0, int bad1, int bad2) {}
    
        @SuppressWarnings(value = "unused") AnnotatedClass(@Deprecated int bad, int bad0, int bad1, int bad2, int bad3) {}
    
        @Deprecated private int incorrectB;
    
        transient @Deprecated 
        private int incorrectC;
    
        transient
        @Deprecated 
        private 
        int correctD;
    
        private
        @SuppressWarnings(value = "unused")
        Object correctA; @SuppressWarnings(value = "dog")
         public void incorrectA(final Object baz) {
        }
    
        public void correctB(@SuppressWarnings(value = "dog") final Object good) {
            @Deprecated
            int correctA;
    
            final @Deprecated int incorrectB;
    
            final
            @Deprecated
            Object
            correctC;
        }
    
        @SuppressWarnings(value = "dog") public 
        void incorrectC(final Object bad) {
        }
    
        public
        @SuppressWarnings(value = "dog") void incorrectD(final Object bad) {
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a rewrite rule that will do the following: http://www.example.com/http://other.example.com →
I am trying to create an apache redirect rule that will redirect half of
I'm trying to create a custom Checkstyle rule which will flag as an error
I'm trying to create a makefile that has rule something like: ~/$ cat >
I'm trying to create a mod_rewrite rule that will drop any .htm that gets
I'm trying to create a custom rule that flags up the use of return
I am trying to create a rewrite rule which will detect numbers only and
I'm trying to create an .htaccess mod_rewrite that will behave differently if the current
im trying to create a sql query, that will detect (possible) duplicate customers in
I am trying to create a rewrite rule that accomplishes two things: Redirect (www.)?domain.com

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.