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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:13:55+00:00 2026-06-05T11:13:55+00:00

Consider the following Java code: public class Test { private Foo< String, String >

  • 0

Consider the following Java code:

public class Test
{
    private Foo< String, String > foo1;
    private Foo< Integer, Integer > foo2;
}

public class Foo< T, V >
{
    private Bar< V > bar;
    private T a;
}

public class Bar< T >
{
    @MyAnnotation
    private List< T > list;
}

First, starting with the class named ‘Test’, I’m searching all fields recursively, which are annotated with ‘MyAnnotation’ and whose type is derived from ‘java.lang.Collection’. For the given example, the results would be:

  • Test.foo1.bar.list
  • Test.foo2.bar.list

It’s obviosly clear, that the first one can only take strings for its elements, whereas the second one can only take integers.

My Question is: What is the best (easiest) way to find all Collection fields annotated with ‘MyAnnotation’ and tell their element type?

Finding annotated fields from type java.lang.Collection is not the problem. But how can we get a result which looks like the following one for the given example?

  • Test.foo1.bar.list< String >
  • Test.foo2.bar.list< Integer >

I’ve tried several approaches, which always gave me ‘Object’ as the collection’s element type instead of ‘String’.

I’m really stuck on this.
Many thanks in advance!

  • 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-05T11:13:58+00:00Added an answer on June 5, 2026 at 11:13 am

    This is not possible due to Java’s type erasure. When compiled, the generic types will be replaced by raw types, resulting in something like this:

    public class Test
    {
        private Foo foo1;
        private Foo foo2;
    }
    
    public class Foo
    {
        private Bar bar;
        private Object a;
    }
    
    public class Bar
    {
        @MyAnnotation
        private List list;
    }
    

    The actual types are then filled in places where you are directly working with foo1 and foo2. For example, the following code:

    Test test = new Test();
    List<String> a = test.foo1.bar;
    String b = a.get(0);
    Integer b = test.foo2.a;
    

    will be translated to something like:

    Test test = new Test();
    // The generic type is erased here...
    List a = test.foo1.bar;
    // ...and a cast is added here
    String b = (String) a.get(0);
    Integer b = (Integer) test.foo2.a;
    

    Long story short: there’s no way you can retrieve that type, since it is discarded at compile time.

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

Sidebar

Related Questions

Consider the following code, public class StartUp { public StartUp(String[] test){} public static void
Consider the following Java class declaration: public class Test { private final int defaultValue
Consider the following Java class: public class Foo { public static void doStuff() {
Please consider the following java source: package com.stackoverflow; public class CondSpeed { private static
Consider the following code public class sqldetails extends JFrame implements ActionListener{ static String username=null;
Consider this code: class test { public static void main(String[] args) { test inst_test
Let's consider the following code in Java. package obj; final class First { public
Let's consider the following code snippet in Java. package escape; final public class Main
Consider the following code: public class ReadingTest { public void readAndPrint(String usingEncoding) throws Exception
Consider the following simple code import java.util.*; public class MainTest<T extends Object1<?,?>> { List<T>

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.