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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:30:05+00:00 2026-05-19T13:30:05+00:00

I have (another) unchecked cast question. I am 90% sure that it is safe,

  • 0

I have (another) unchecked cast question. I am 90% sure that it is safe, but I want to make sure (I’m justifying the use of @SupressWarnings to another developer who is reviewing the code)

The following pattern has been set up by our framework:

abstract class Writer<T> {
    Class<T> valueType;

    Writer(Class<T> valueType) {
        this.valueType = valueType;
    }
}
class Cat { }

class CatWriter extends Writer<Cat> {
    CatWriter() {
        super(Cat.class);
    }
}

I am also using a subclass of Writer to write a class that makes use of generics:

class Color {}
class Green extends Color {}
class Brown extends Color {}

My writer class looks like this:

abstract class Bird<C extends Color> {}
class Parrot extends Bird<Green>{}
class Ostrich extends Bird<Brown>{}

class BirdWriter<C extends Color> extends Writer<Bird<C>> {
    BirdWriter(Bird<C> bird) {
        super((Class<Bird<C>>)bird.getClass());
    }
}

I could use raw types in the writer but that gives many more warnings. Instead I include the generics in the Writer class. This is fine everywhere but the constructor. I am forced to cast the bird.getClass() (which is a class object which has no generic signature) to a Class object with a generic signature. This produces an unchecked cast warning on the cast, but I believe it is safe to cast the result to Class<Bird<C>> because the bird that is passed into the parameter is guaranteed to be a Bird<C>.

Testing backs up my theory, but I want to make sure that my thinking is correct. Is there any way in which this code is unsafe?


Update

Thanks for your answers. Because of the answers I realized there was a weakness in my structure and have revised it.

Essentially Cat uses a simple Writer that knows it’s always writing a Cat. In my case, I’ve got a type of "SmartAnimal" that can be written by a dynamic Writer, so that I don’t have to create a Writer for every Animal.

class SmartAnimal {}
class Dog extends SmartAnimal {}
class Horse extends SmartAnimal {}
class SuperHorse extends Horse {}

class DynamicWriter<A extends SmartAnimal> extends Writer<A> {
    DynamicWriter(A smartAnimal) {
        super((Class<A>)smartAnimal.getClass());
    }
}

Again, I have the same warning, but this seems to be more safe.

Is this better, and is it safe?

  • 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-19T13:30:06+00:00Added an answer on May 19, 2026 at 1:30 pm

    It is most certainly not correct, for the reason listed by ILMTitan in his answer. But I’ll give you a reason why it might also not be very safe. Imagine you had a method verifyType() that made sure that anything that was passed in for writing was the correct type:

    public void write(T t) {
       if (!verifyType(t)) explode();
       //...do write
    }
    
    public boolean verifyType(T t) {
       return valueType.isInstance(t);
    }
    

    This method you would expect never to fail, right? After all, you’re just ensuring that t is a T (since you have a Class<T>), and we already know it is a T because write() only accepts a T? Right? Wrong! You’re not actually checking if t is a T, but potentially some arbitrary subtype of T.

    Examine what would happen if you declared a Writer<Bird<Green>> and instantiated it with a Parrot<Green>, it would be legal to do this call:

    Writer<Bird<Green>> writer;
    writer.write(new SomeOtherGreenBird());
    

    And again you would not expect it to fail. However your value type is for Parrot only, so the type check would fail. It all depends what you’re doing in your writer class, but be forewarned: you are treading in dangerous water here. Declaring a Class<? extends T> in your writer would prevent you from making a mistake like this.

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

Sidebar

Related Questions

I want to use an image for an unchecked checkbox and another image for
I have another programmer who I'm trying to explain why it is that a
i have another (probably unanswered) question about map views. I have a map view
i have another question on getting my XML serialization neat, which i can't seem
I have another question :(. I'm trying to download multiple files for my application.
I have another question and I can't seem to find anything on Google. What
I have another Richfaces question which may seem rather weird. I am developing a
I recently asked another question pertaining to avoid having to use boilerplate exceptions when
Afternoon all. Another hour, another question! I have the following bit of jquery up
I have a WCF service deployed on another machine and I want to authenticate

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.