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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:44:26+00:00 2026-05-27T12:44:26+00:00

Is it somehow possible to annotate a Java Method in that way that i

  • 0

Is it somehow possible to annotate a Java Method in that way that i later can give another Method a Field Identifier or something like that, so that this Method can call the right one?

I know that normally you would do this with interfaces, but in my case this would be a immense count of interfaces… I need to use this in Entity Classes for my Database (and i’m not allowed to use a ORM Mapper)

For example: I have the Entity

public class Account{
    private String username;
    private String password;
    private String name;
    private String mail;


    public void setUserName(String username){
        this.username = username;
    }

    public String getUserName(){
        return username;
    }

    [all other getter/Setter...]
}

Now i want to tell a Method that it need to validate a Field, for example the username field.

The Method that does should look like this:

public void validateField(XXX Field, Entity entity) throws ValidationFailedException, EmptyFieldException;

where XXX is somehow the FieldIdentifier.

Is that in any way possible in Java?

My only guess it that i Use public static final ìnt stuff in there to give every field a Method or so…

  • 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-05-27T12:44:26+00:00Added an answer on May 27, 2026 at 12:44 pm

    What do you use? I don’t see any annotations from which I can guess your framework. If you use Hibernate, you can use something like @NotNull or something else, or even do your custom validation:
    This is how you would go with your example:

    public class Account{
    
        @NotNull(message="This field should not be null")
        private String username;
        @NotBlank(message="This string should not be empty or null")
        private String password;
        private String name;
        private String mail;
    
    
        public void setUserName(String username){
            this.username = username;
        }
    
        public String getUserName(){
            return username;
        }
    
        [all other getter/Setter...]
    }
    

    http://silentwalker.wordpress.com/2009/04/07/custom-validation-in-hibernate/

    You can also create your own annotations without using any framework and use them @prepersist or whatever. Basically the sky is the limit.

    P.S Since you don’t want to use any non internal code, here is how you can approach:

    First, you can define an annotation

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface NotNull{
        public String message() default "";
    }
    

    Then, before persisting the class, you would inspect its fields:

    So, you have something like this:

    Field[] classFields = yourObjectPrePersist.getClass().getDeclaredFields();
    for (int i = 0; i < classFields.length; i++) {
            classFields[i].setAccessible(true);//take notice that if you use a SecurityManager, you should work around it
            if (classFields[i].getAnnotation(NotNull.class) != null) {
                Object value = classFields[i].get(yourObjectPrePersist);
                //here check if value is null or not and then return the message
                if (value == null) {
                    throw new SomeException(((NotNull) classFields[i].getAnnotation(NotNull.class)).message());
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible somehow to close StreamReader after calling ReadToEnd method in construction like
Is it somehow possible on Swing to set a TitledBorder transparent so that a
Is it somehow possible that instead of: Button btnNextWord = (Button) this.findViewById(R.id.btnNextWord); Eclipse automatically
Is it somehow possible to extend a type, wich is defined in another assembly,
(How) is it possible to represent monads in Scala in a generic way (like
Is it somehow possible to send a function to another function for it to
Is that somehow possible to access one datastore? Or access one app from different
Is it somehow possible to write active-x component on Java? Any library, tutorial and
Is it somehow possible to unmask a PasswordTextBox ? I would like to have
Given a Class<?> that describes class A , is it somehow possible to get

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.