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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:40:18+00:00 2026-06-14T21:40:18+00:00

I have the following class public class DBField<T> { protected String fieldName; protected FieldConverter

  • 0

I have the following class

public class DBField<T>
{
  protected String fieldName;
  protected FieldConverter c;
  protected T value;
  protected DataObject dataObject;

  public T getValue()
  {
    return value;
  }

  public void setValue(T value)
  {
    this.value = value;
  }

  public DBField(DataObject dataObject, String fieldName, FieldConverter c)
  {
    this.fieldName = fieldName;
    this.c = c;
    this.dataObject = dataObject;
  }
}

T is supposed to be Boolean, Float, String etc.

protected void ValuesToFields(List<Object> values, List<DBField<?>> fields) throws Exception
  {
    if (values.size() != fields.size())
      throw new Exception("Length does not match.");
    for (int i = 0; i < values.size(); i++)
    {
      Class valueClass = values.get(i).getClass();
      Class fieldClass = fields.get(i).getValue().getClass();
      if (valueClass.equals(fieldClass))
      {
        fields.get(i).setValue(values.get(i));
      }
      else
        throw new Exception("type mismatch");
    }
  }

Object is also supposed to contain Boolean, Float, String etc.

The problem with this code is

fields.get(i).setValue(values.get(i));

The syntax checker tells me I need to cast values.get(i) (to ? i suspect). How do I do this? I already tried valueClass.cast(values.get(i)) but no luck.

  • 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-14T21:40:19+00:00Added an answer on June 14, 2026 at 9:40 pm

    In order for your code to be safe, for each i, the i’th element of values must be an instance of the type parameter of the DBField that is the i’th element of fields. Your code does not guarantee that this holds, and in fact there is no way to declare them in Java to ensure that this relationship between corresponding elements is true. And due to type erasure, you can’t even check at runtime that the elements are right, because given a field, you don’t know its type parameter. So there must be some unchecked casts, and we must take on faith that the arguments are correct.

    The simplest thing to do would be to cast each field to DBField<Object>:

    ((DBField<Object>)fields.get(i)).setValue(values.get(i));
    

    This is kind of saying “trust us, we know that this field can take any Object“, and thus it can take a value of any type. It is kind of lying, because we know there are supposed to be fields whose type parameter is not Object, but since we must make some kind of unchecked cast anyway, this “unsafe cast” is no worse than the other solutions.

    Alternately, if you don’t want to do this arguably dubious cast, a more “legitimate” way would be to write a private helper method — a “wrapper helper” — which explicitly names the type parameter of the field, allowing us to simply cast to the value to this type:

    private <T> static void ValueToField(Object value, DBField<T> field) {
        field.setValue((T)value);
    }
    
    //...
    ValueToField(values.get(i), fields.get(i));
    

    Note that the cast here is also an unchecked cast. The disadvantage of this method is that it requires the overhead of writing an extra method.

    P.S. Your checks with valueClass and fieldClass are not very good. First of all, if the value of a field is currently null, it will cause a null pointer exception. Also, the value of a DBField<T> is any instance of T, whose actual class may be a subclass of T; so if you use this to check, it might lead to bad results. It’s probably best if DBField contains the class object of the class of T, so it can be used to check. Also, you shouldn’t compare equality with the value’s actual class, since a subclass of T would also work, so you should check fieldClass.isInstance(values.get(i)) instead.

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

Sidebar

Related Questions

I have the following class: public class Go { public static void main(String args[])
I have the following class: public abstract class TableServiceEntity { protected TableServiceEntity(); protected TableServiceEntity(string
I have the following class: public class getURLData extends AsyncTask<String, Integer, String>{ @Override protected
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
I have the following class public class MenuVeiculo { public string Nome { get;
I have the following class public class UIControl { public string FName{ get; set;
I have the following class public class CountrySpecificPIIEntity { public string Country { get;
I have the following class public class CountrySpecificPIIEntity { public string Country { get;
I have the following class: public class DocumentCompare { public string Customer; public string
I have this following class: public class TV { public string GroupId { get;

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.