• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Create a new instance of a generic type

 
Marshal
Posts: 4017
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have AbstractRow and AbstractTable classes which are used as based classes for tables containing specific types of row data.

When a table is first created, it has zero rows. As information fragments are passed to the table for processing, if the row specified in the fragments does not already exist, a new row is created, added to the table, and the information is recorded in the row.

The AbstractTable class is a generic type, and is extended to create a row-specific tables by specifying using the row type parameter. For example:
    public class SensorsTable extends AbstractTable<Sensor>

The problem I have is since the actual class type is not available at runtime, I am unable to create a new instance of a specific row class using something like new T() or T.newInstance().

The two solutions that I have found are:
    - Use annotation metadata to specify the row class (see SensorsTable class)
    - Pass the class to the super class's constructor (see SensorsTable2 class).

I'm not really happy with either way, and I am wondering what other alternatives there are; possibility not using generics. There will be lots of different Table and Row classes, so I would like to minimize the amount of code (especially boilerplate) in those classes.

I apologize for the huge dump of code, but I though it would be useful to make clear what I am trying to do.
Runtime output:
    Sensor [deviceId=5521R2, value=-12]
 
Ranch Hand
Posts: 115
11
IntelliJ IDE Clojure Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right that you can't take the "obvious" approach of creating a new T, since T does not exist at runtime. You have to work around this limitation by introducing some level of abstraction. The way I would do it is more along the lines of your second approach:



How you handle the potential exceptions from newRow() are up to you. Note that in process(), you don't take that into account right now. The row variable could still be null even after you create one.

If you can't think of any meaningful way to deal with that, then maybe some sort of RowFactory is needed. This works basically the same way as what you have now, just instead of using the Class as the factory, you use your own object that doesn't do any reflection and so doesn't run the risk of throwing exceptions:

 
Ron McLeod
Marshal
Posts: 4017
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jason - thanks for the great feedback. The code is still only proof on concept so I didn't really put much thought in to completeness or robustness yet - I know there are a number of holes.

Also, thanks for pointing out where I went wrong with the type for rowClass - I new I had something wrong, but couldn't put my finger on it.

Enjoy a slice of pie.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:Jason - thanks for the great feedback. The code is still only proof on concept so I didn't really put much thought in to completeness or robustness yet - I know there are a number of holes.


And mine would be the basic mismatch between a Java object and a database table. But kudos to you for trying.

The basic problem from an OO point of view is that pretty much everything in your "Table" - including data (getting and setting of), and possibly indexes and subset views as well, has to be public. So unless you're very careful, any program that uses it becomes an "SQL proxy".
But as an "interim" stage - eg a DAO object or Bean - between a database and a Java program, I see no reason why it shouldn't work.

But don't expect that just because you've found a way to map an SQL Table to a Java class that your problems end there.

Winston
 
Ron McLeod
Marshal
Posts: 4017
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case, the backend is an SNMP agent, not a relational database. When I query the agent, I get back a collection of SNMP objects, where each object represents the value of a single field for an particular row. I am taking these bits of data and assembling them into table of rows so that the application doesn't need to be concerned how the data was natively stored/accessed.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:I am taking these bits of data and assembling them into table of rows so that the application doesn't need to be concerned how the data was natively stored/accessed.


In which my question would be: why not marshal them into a List (or Set) of Mail (or actual) objects that make sense to your Java program? I worry a bit that the "table" abstraction may be a step too many.

That said, I've never had to deal with SNMP - although I suspect there may be libraries out there to do it.

Winston
 
Get me the mayor's office! I need to tell him about this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic