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 8266455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:10:03+00:00 2026-06-08T05:10:03+00:00

I’m working on an annotation which aims to it mandatory for a class to

  • 0

I’m working on an annotation which aims to it mandatory for a class to be immutable. Here the code of the processor:

@SupportedAnnotationTypes("archipel.immutability.IsImmutable")
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class IsImmutableProcessor extends AbstractProcessor {

    @Override
    public boolean process(Set<? extends TypeElement> annotations,
            RoundEnvironment roundEnv) {
        for (TypeElement type : annotations) {
            processMustBeImmutable(roundEnv, type);
        }
        return true;
    }

    private void processMustBeImmutable(RoundEnvironment env, TypeElement type) {
        for (Element element : env.getElementsAnnotatedWith(type)) {
            processClass(element);
        }
    }

    private void processClass(Element element) {
        boolean isFinal=false;

        for(Modifier modifier : element.getModifiers()) {
            if (modifier.equals(Modifier.FINAL)) {
                isFinal=true;
                break;
            }
        }

        if (!isFinal) {
            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Class "+element+" is not immutable because it is not final");
        } else {
            for (Element subElement : element.getEnclosedElements()) {
                if (subElement.getKind()==ElementKind.FIELD) {
                    isFinal=false;

                    for(Modifier modifier : subElement.getModifiers()) {
                        if (modifier.equals(Modifier.FINAL)) {
                            isFinal=true;
                            break;
                        }
                    }
                    if (!isFinal) {
                        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Field "+element+" is not immutable because it is not final");
                    } else {
                        Element superElement = subElement.getEnclosingElement();
                        // TODO
                    }
                }
            }
        }
    }

}

The annotation itself is trivial, of course:

@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface IsImmutable {
}

And I compile it with a Ant script:

<project name="immutability" basedir="." default="main">

    <property name="lib.dir" value="lib"/>

    <property name="src.dir" value="src"/>

    <property name="build.dir" value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="meta.dir" value="${build.dir}/META-INF"/>
    <property name="jar.dir" value="${build.dir}/jar"/>

    <property name="processor-package"
        value="archipel.immutability" />

    <property name="processor" value="${processor-package}.IsImmutableProcessor"/>

    <path id="classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar"/>
        <fileset dir="${classes.dir}"/>
    </path>

    <target name="clean">
    <delete dir="${build.dir}"/>
    </target>

    <target name="compile" description="Compiles the code.">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" />
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar">
            <fileset dir="${classes.dir}"/>
            <service type="javax.annotation.processing.Processor" provider="archipel.immutability.IsImmutableProcessor"/>
        </jar>
    </target>

    <target name="main" depends="clean,jar"/>

</project>

Problem is, something must be missing, because when I try to use the resulting annotation, provided by the resulting jar file, like the following, nothing happens:

@IsImmutable
public class Immut {

    private int toto;

    public int getToto() {
        return toto;
    }

    public void setToto(int toto) {
        this.toto = toto;
    }

    public final static void main(String args[]) {
        Immut truc = new Immut();
        truc.setToto(5);
        truc.setToto(6);
    }

}

Obviousy, this class is not final, and the class should signal an error in Eclipse. But it’s not.

Any idea?

Edit: The jar file I built with my build.xml seems correct: It contains the class files, and also a META-INF/services/javax.annotation.processing.Processor file, which contains archipel.immutability.IsImmutableProcessor. I import this jar file in my test project, and when I use the annotation in my Immut class (which is only a rough test), nothing happens.

  • 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-08T05:10:10+00:00Added an answer on June 8, 2026 at 5:10 am

    Annotation processing by default is disabled in eclipse. in order to enable it you need to open project properties and then
    1. Java Compiler -> Annotation processing: enable annotation processing
    2. Java Compiler -> Annotation processing -> factory path: add jar with factories

    if you need some more information take a look at: getting started with AP in eclipse

    Edited:

    take a look at this tutorial. It explains in details how to set up eclipse to use custom annotation processors.

    Important: when printing errors use this method: javax.annotation.processing.Messager.printMessage(Kind, CharSequence, Element)
    instead of:
    javax.annotation.processing.Messager.printMessage(Kind, CharSequence).

    messages from the first one are visible in Problems view and most source-related views while messages from the second one are visible only in ErrorLog view.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

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.