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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:42:18+00:00 2026-06-12T11:42:18+00:00

I have an enum as follows: public enum SomeType { SOME_KEY (some-display-value-as-label); private String

  • 0

I have an enum as follows:

public enum SomeType {
    SOME_KEY (some-display-value-as-label);
    private String label;

    private SomeType(String label){
        this.label=label;
    }
    public String getLabel() {
        return label;
    }
    public void setLabel(String value) {
        this.label = value;
    }
}

Now I am using google reflections library and have come up with a custom Annotation where I mark the enum above with an annotation like @makejson.

The idea is to scan on app startup using reflections for all classes with the @makejson annotation and then generate the json object for each of these enums.

What I’m trying to do is in the startup class:

Reflections reflections = new Reflections("my.package.name");
Set<Class<?>> annotatedClasses = reflections.getTypesAnnotatedWith(MakeJson.class);
    for (Class<?> annotated : annotatedClasses) {
        if  (annotated.isEnum()) {
            MakeJson j = annotated.getAnnotation(MakeJson.class);
            Object[] constants = annotated.getEnumConstants();
            Method[] methods = annotated.getMethods();
            Method getValue = null;
            for (Method m : methods) {
                if ("valueOf".equals(m.getName())) {
                    getValue = m; //get Reference of valueOf method
                    break;
                }    
            }
            //List<Object> labels = Arrays.asList(m.invokem.getReturnType().isEnum()(annotated));
            for (Object constant : constants) {
                System.out.println(constant.toString());
                System.out.println(getValue.invoke(annotated,constant.toString()));
            }
        }
    }

This code breaks with the following exception: Exception in thread “main” java.lang.IllegalArgumentException: wrong number of arguments

Any help would be greatly appreciated. The end objective is to be able to get a json object for SomeType{SOME_KEY:”display-value”}. For this I am unable to get the value of the enum constant using Reflection.

  • 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-12T11:42:19+00:00Added an answer on June 12, 2026 at 11:42 am

    So here’s what I ended up doing:
    I modified the @makejson annotation to include a field called label which is mandatory and which should be set to the method that can return the description associated with SOME_KEY in the enum.

    Here’s what the annotation looks like:

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MakeJson {
        String label();
    }
    

    so the annotation at the enum declaration looks like
    @makejson(label=”getLabel”)
    public enum SomeType {
    … // same as in the question above
    }

    Then in the parsing class, I simply get this method from the annotation’s method and invoke it for the correct constant:

        Reflections reflections = new Reflections("my.package.name");
        Set<Class<?>> annotatedClasses = reflections.getTypesAnnotatedWith(MakeJson.class);
        for (Class<?> annotated : annotatedClasses) {
            if  (annotated.isEnum()) {
                Jsonify j = annotated.getAnnotation(MakeJson.class);
                Object[] constants = annotated.getEnumConstants();
                Method m = annotated.getMethod(j.label()); // get the method name
                for (Object constant : constants) {
                    System.out.println(constant.toString());
                    System.out.println(m.invoke(constant));
                    // construct json object here
                }
            }
        }
    

    So in the end I circumvented the issue of not being able to call the method using reflection by using annotation to plug this info in at runtime!

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

Sidebar

Related Questions

This is a simplified example. I have this enum declaration as follows: public enum
I have the enum structure as follows: public enum MyEnum { One=1, Two=2, Three=3
I have a program with a class that contains a public enum, as follows:
I have a class with a private member ( struct ) as follows: enum
I have enum like this [Flags] public enum Key { None = 0, A
I have enum like this: public enum ObectTypes { TypeOne, TypeTwo, TypeThree, ... TypeTwenty
I have the following property defined. MyLibrary.PumpSignal is an enum as follows: Public Enum
I have created a web service containing an enum with values as follows public
I have a model as follows: namespace MvcApplication1.Models { public enum Sex { Male,
I have an enum defined as follows: public enum CrystalTypeEnum { Red, White, Blue,

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.