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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:36:28+00:00 2026-05-26T20:36:28+00:00

I was playing around with Java reflection, and I wanted to create a method

  • 0

I was playing around with Java reflection, and I wanted to create a method caching mechanism from Methods declared in different classes. In order to prevent random behavior, I want to forbid loading methods with the same signature to the Cache (method declared in different classes can have the same signature).

The only way I found of doing this, was to Override the contains() method of the Set where I cache the methods.

Is it dangerous to do so? Do you have any better idea to achieve this?

private final Set<Method> methodsCache;

public MyMethodCachingClass(Set<Class<?>> classes) {
    methodsCache = new HashSet<Method>(){
        private static final long serialVersionUID = -1467698582662452923L;

        /**
        * Overwriting the contains method of this Set so that we don't allow multiple methods with the same signature,
        * even if they are declared in different classes.
        */
        @Override
        public boolean contains(Object o) {
            if (!(o instanceof Method)) {
                return false;
            }

            Method method = (Method) o;
            for (Method m : this) {
                if (method.getName().equals(m.getName()) && method.getParameterTypes().equals(m.getParameterTypes())) {
                    return true;
                }
            }
            return false;
        }

    };

    for (Class<?> c : classes) {
        for (Method m : c.getDeclaredMethods()) {
            if (methodsCache.contains(m)) {
                throw new IllegalArgumentException("The method " + m.getName() + " exists with the same signature in two different classes.");
            }
            methodsCache.add(m);
        }
    }
}

Thanks!

  • 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-26T20:36:29+00:00Added an answer on May 26, 2026 at 8:36 pm

    Simply use a combination of the following for your cache key:

    class name + method name + method parameter types

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

Sidebar

Related Questions

I'm just learning c++ coming from a Java background. Just playing around with simple
I've been playing around with reflection in Java... and I'm a little bit baffled.
I'm playing around with Java's reflection API and trying to handle some fields. Now
I have been playing around with node.js, and coming from a Java background, I
I'm playing around with some reflection calls to call a method that is normally
Java Day 1 from c#. Just playing around with java (just for fun) and
I am currently learning about basic networking in java. I have been playing around
been playing around with ResolveClientUrl(~/Confirmation.aspx) and other methods.. I am tryin go get the
I'm currently playing around with java.nio , which I haven't been using for a
I am just playing around with threads in java. I have a class which

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.