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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:28:33+00:00 2026-06-12T01:28:33+00:00

I am using reflection proxies to perform additional checking on a public API. Essentially

  • 0

I am using reflection proxies to perform additional checking on a public API. Essentially I want to wrap every object that comes back from it so that any object the caller gets their hands on is a proxy to the real object.

Java still has the whole erasure problem, so I am passing the type of the wrapped object around with it. I should know what type everything is because the entry into the API is a single, non-generic interface.

public class ProxyInvocationHandler implements InvocationHandler {
    private final Object delegate;
    private final Type delegateType;

    public ProxyInvocationHandler(Object delegate, Type delegateType) {
        this.delegate = delegate;
        this.delegateType = delegateType;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) {
        // Omitted: additional checks performed here.

        Object result = method.invoke(delegate, args);

        Type returnType = method.getGenericReturnType();

        // e.g. if delegateType is List<Cat> and the method is the get method,
        // returnType would be E but resultType should be Cat.
        Type resultType = ???

        // Utility method I will omit, it just creates another proxy instance
        // using its own invocation handler.
        return ProxyUtils.wrap(result, resultType);
    }
}

I have looked around the Type / ParametrizedType API and can’t seem to find a way to get resultType, even though delegateType and returnType should be enough information to compute this.

What is the “proper” way to do this?

  • 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-12T01:28:34+00:00Added an answer on June 12, 2026 at 1:28 am

    You can use Java ClassMate for that purpose. You’ll have to use com.fasterxml.classmate.GenericType for type tokens:

    GenericType<?> delegateType = new GenericType<List<Cat>>() {};
    

    Note the empty {} that’s called the “Super-type Token” pattern.

    TypeResolver typeResolver = new TypeResolver();
    MemberResolver memberResolver = new MemberResolver(
    
    ResolvedType type = typeResolver.resolve(delegateType);
    ResolvedTypeWithMembers members = memberResolver.resolve(type, null, null);
    ResolvedMethod[] methods = members.getMemberMethods();
    

    Cache the results in a Map:

    Map<Method, ResolvedMethod> resolved = new HashMap<>();
    for (ResolvedMethod method: methods) {
        resolved.put(method.getRawMember(), method);
    }
    

    Now, when you have a method declared by the delegateType, i.e. List, you can get its resolved return type:

    Method method = List.class.getMethod("get", int.class);
    ResolvedType resultType = resolved.get(method).getReturnType();
    System.out.println("resultType = " + resultType);              // prints resultType = Cat
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using reflection I want to retrieve only the properties that have both a get
I'm using reflection to call a method that is outside of the target API
When using reflection it is possible to obtain the call stack (apart from that
Using reflection, is it possible to create an instance of a type that inherits
Using reflection on the type DerivedClassB can we determine: a) that it uses the
When using reflection we typically just was the basic System.Reflection API but I am
Using reflection to obtain a MethodInfo, I want to test if the type returned
Using reflection (i'm guessing?), is it possible to create a method that will return
Using reflection I have an object which I need to cast into an iterable
I am using Reflection.Emit to make a exe. I have come so far that

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.