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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:38:16+00:00 2026-05-10T22:38:16+00:00

I have a method fetchObjects(String) that is expected to return an array of Contract

  • 0

I have a method fetchObjects(String) that is expected to return an array of Contract business objects. The className parameter tells me what kind of business objects I should return (of course this doesn’t make sense in this construed case because I already said I will return Contracts, but it’s basically the situation I have in my real scenario). So I get the set of entries from somewhere and load the class of the collection’s entries (the type of which is specified by className).

Now I need to construct the array to return, so I use Set‘s toArray(T[]) method. Using reflection, I build myself an empty Contracts array. But, this gives me a value of static type Object! So next I need to cast it to the appropriate type, which in this case is Contract[] (see ‘asterisk-underlined’ part in the listing below).

My question is: Is there a way, and how, to cast to Contract[] as I do in the listing, but determining the type of the array elements (Contract) only through className (or entriesType)? In other words, what I’d like to do is basically casting like this: (entriesType[]) valueWithStaticTypeObject, where entriesType be replaced by the class specified through the classname parameter, i.e. Contract.

Is this somehow inherently impossible, or can it be done somehow? Maybe using generics?

package xx.testcode;  import java.util.HashSet; import java.util.Set;  class TypedArrayReflection {      public static void main(String[] args) {         try {             Contract[] contracts = fetchObjects('Contract');             System.out.println(contracts.length);         } catch (ClassNotFoundException e) {}     }      static Contract[] fetchObjects(String className) throws ClassNotFoundException {         Class<?> entriesType = Class.forName('xx.testcode.'+className);         Set<?> entries = ObjectManager.getEntrySet(className);          return entries.toArray(                  (Contract[]) java.lang.reflect.Array.newInstance(                 /********/          entriesType, entries.size()) );     } }  class Contract { } // business object  class ObjectManager {     static Set<?> getEntrySet(String className) {         if (className.equals('Contract'))             return new HashSet<Contract>();         return null; // Error     } } 

Thanks.


Update: Using the type-safe method toArray, taken from CodeIdol, I updated my fetchObjects method thus:

static Contract[] fetchObjects(String className) throws ClassNotFoundException {     Class<?> entriesType = Class.forName('xx.testcode.'+className);     Set<?> entries = ObjectManager.getEntrySet(className);     return toArray(entries, entriesType); // compile error     // -> 'method not applicable for (Set<capture#3-of ?>, Class<capture#4-of ?>)' }  public static <T> T[] toArray(Collection<T> c, Class<T> k) {     T[] a = (T[]) java.lang.reflect.Array.newInstance(k, c.size());     int i = 0;     for (T x : c)         a[i++] = x;     return a; } 

What do I need to do to get rid of the compiler error quoted in the comment? Do I absolutely have to specify Set<Contract> in the return type of my getEntrySet method so that this can work? Thanks for any pointers.

  • 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. 2026-05-10T22:38:17+00:00Added an answer on May 10, 2026 at 10:38 pm

    You may use the class as the parameter rather then the class name.

       static <T extends Contract> T[] buildArray(Class<T> clazz){      ArrayList<T> l=new ArrayList<T>();      return l.toArray((T[]) java.lang.reflect.Array.newInstance(clazz, l.size()));    } 

    EDIT: (after read Yang comment)

    No, You cannot use generic type with the value of a variable.

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

Sidebar

Ask A Question

Stats

  • Questions 70k
  • Answers 70k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Stupid, stupid me :( The thing is that I was… May 11, 2026 at 1:03 pm
  • added an answer Here my personal experiences with git vs. svn: Git vs.… May 11, 2026 at 1:03 pm
  • added an answer If you define the relationships properly in SQL, LINQToSQL should… May 11, 2026 at 1:03 pm

Related Questions

I have a method which takes params object[] such as: void Foo(params object[] items)
I have a method that where I want to redirect the user back to
I have a method in my Python code that returns a tuple - a
I have a method that can return either a single object or a collection

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.