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

  • Home
  • SEARCH
  • 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 7964319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:51:28+00:00 2026-06-04T05:51:28+00:00

How do you get values set inside a annotation? I have the following annotation

  • 0

How do you get values set inside a annotation?

I have the following annotation defined:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonElement {
    int type();
}

And here is the method that uses it in a POJO class

@JsonElement(type=GETTER_METHOD)
public String getUsername{
........................
}

And the util class that uses reflection to check if this method has the JSonElement annotation present and to check what the type value is.

Method methods[] = classObject.getClass().getDeclaredMethods();

        JSONObject jsonObject = new JSONObject();
        try {
            for (int i = 0; i < methods.length; i++) {
                String key = methods[i].getName();
                System.out.println(key);
                if (methods[i].isAnnotationPresent(JsonElement.class) && key.startsWith(GET_CHAR_SEQUENCE)) {
                    methods[i].getDeclaredAnnotations();
                    key = key.replaceFirst(GET_CHAR_SEQUENCE, "");
                    jsonObject.put(key, methods[i].invoke(classObject));
                }

            }
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

How do I find out what type() value is? I can find whether the annotation is present, but I can’t find a method that finds out what value – if any – was set for type().

  • 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-04T05:51:30+00:00Added an answer on June 4, 2026 at 5:51 am
    JsonElement jsonElem = methods[i].getAnnotation(JsonElement.class);
    int itsTypeIs = jsonElem.type();
    

    Note that you must be sure that jsonElem is not null either by your

    isAnnotationPresent(JsonElement.class)
    

    or a simple

    if (jsonElem != null) {
    }
    

    check.


    Additionally, if you changed your annotation to

    public @interface JsonElement {
        int type() default -1;
    }
    

    you wouldn’t have to state the type attribute at every occurence of @JsonElement in your code – it would default to -1.

    You could also consider using an enum for this instead of some integer flags, for example:

    public enum JsonType {
        GETTER, SETTER, OTHER;
    }
    
    public @interface JsonElement {
        JsonType type() default JsonType.OTHER;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get the collection prototype to have a set of default values
I have a combo box set inside a xslt using the following code <select
Is there any way to get available values for SET field in table? Thank
I want to get all values of a set interface in one go as
I am trying to get values from a data set. My function has to
How do you set/get the values of attributes of t given by x ?
How to set up a TextBox to get only certain values. e.g. DateTime input
I m able to get values using JPA when i have only mapped one
I am trying to get values from a table that lives inside an iframe
How can I retrieve GET values inside a PHP Class construct or functions? Similar

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.