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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:48:09+00:00 2026-06-15T05:48:09+00:00

I wanted to create a GenericBuilder that could be used to retrieve the Builder

  • 0

I wanted to create a GenericBuilder that could be used to retrieve the Builder classes that are defined inside my classes. I created the following interfaces and classes

public interface BuilderType<T> {
}

public class MyObject implements BuilderType<MyObject.MyObjectBuilder> {
private int i;
private MyObject() {}
public int getI() {
    return i;
}
public static MyObjectBuilder buildMyObject() {
    MyObjectBuilder builder = new MyObjectBuilder();
    return builder;
}
public static class MyObjectBuilder {
    private final MyObject obj;
    MyObjectBuilder() {
        obj = new MyObject();
    }
    public MyObjectBuilder withI(int i) {
        obj.i = i;
        return this;
    }
    public MyObject build() {
        return obj;
    }
}
public class GenericBuilder {
 public static <T extends BuilderType<S>, S> S getBuilder(Class<T> t) {
 S s = null;
 try {
     s = (S) t.getDeclaredMethod("build" + t.getSimpleName(), null)
                .invoke(null, null);
 } catch (Exception e) {
     e.printStackTrace();
 }
 return s;
 }
}

the statement s=(S)t.get… in my GenericBuilder gives a cast warning: Unchecked Cast from Object to S, is there a way to eliminate it??

  • 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-15T05:48:10+00:00Added an answer on June 15, 2026 at 5:48 am

    There is no way to eliminate the warning because S is not a real type. By runtime it will get erased to its upper bound, which is Object in your case. So the “downcast” isn’t actually doing anything. You can check the Method‘s return type by calling the appropriate method, and then do a reflective downcast (not very helpful for your scenario). Type safety cannot possibly be achieved when you dynamically fetch a method at runtime.

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

Sidebar

Related Questions

I wanted to create a view that looked something like the following but i
I wanted to create a method in Android that could communicate with a WCF
I wanted to create a very simple method that switches between views in a
I wanted to create some subdirectories inside my blob. But it is not working
I wanted to create an new gesture keyboard which can be used by all
I wanted to create a new WordPress page that is actually a link to
I wanted to create a WCF that employs a singleton pattern but the service
I wanted to create a 3 level dijit.Tree , like that: -root | --level1
I wanted to create a button that had an image and a textblock as
I wanted to create a layout like the CyanogenMod call log in that there

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.