#How to get the class of a generic at runtime?

1 messages · Page 1 of 1 (latest)

chilly vaultBOT
#

<@&1004656351647117403> please have a look, thanks.

reef narwhal
#

Title really should be "How do I use hibernate with generics"

#

not interested in rewriting this dao and repo pattern for every class I have

rotund sierra
#

short answer: you can't

reef narwhal
#

why?

reef narwhal
#

Or am I going to have to copy and paste for all of my entities

#

it's 4 files for every one entity

#

it'd be really nice if I could just have one file that implements the only thing that's different between all of them

pine sage
#

gimmie a sec i'll write you a demo

rotund sierra
pine sage
#
import java.lang.reflect.ParameterizedType;

abstract class BaseClass<T> {
    final Class<T> param;
    BaseClass() {
        param = (Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }
}
#
class Apple extends BaseClass<String> {

}
#
void main() {
    var a = new Apple();
    IO.println(a.param);
}
#

param = (Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];

#

lots of assumptions baked into this line

#

but as you see, we do preserve generic info with extension

reef narwhal
#

I found that solution online

#

and ow

pine sage
#

i know, i am talking to you online

reef narwhal
#

I meant stack overflow :p

#

It's just...nasty

#

what is that

pine sage
#

there are multiple answers to that

reef narwhal
reef narwhal
#

i don't understand how classes are handled in java

pine sage
#

would you prefer

  • The historical answer, where we blame it on compatibility
  • The "measured" answer where we go through the alternatives to erasure
  • Something else
reef narwhal
#

I'd prefer java to not need classes up my ass and out my eyes D:

#

(I am coming from C++ primarily)

pine sage
# reef narwhal what assumptions?
  • that the class is only being extended once - otherwise this.getClass().getGenericSuperclass() might not be BaseClass
  • That the type arguments are supplied
  • That the type argument is a class and not itself another generic
reef narwhal
#

those are true for this case

#

so I think it will work

pine sage
#

which means the assumptions hold

#

you can make checks to ensure they do - which might be helpful since you can provide better errors

pine sage
#

the bad news is that the JVM deals in classfiles. You gonna have classes.

#

the good news is that if you write this

#
public final class Fruits {
    private Fruits() {}
}
#

then the class is essentially just a namespace for functions

#
public final class Fruits {
    private Fruits() {}

    public static void eat(/* ... */) {
        // ...
    }
}
Fruits.eat(...);
reef narwhal
#

the ship just needs to get to shore 🫡

pine sage
#

and a lot of stuff - like public everywhere - are borne out of monkey see monkey do more often than legitimate need

reef narwhal
#

C/C++ have plenty of issues, but I've stepped on enough of their piles of glass that I know where not to step lol

#

gotta go through the same process with Java

pine sage
#

there are languages based on the JVM which genuinely just have functions

reef narwhal
#

I've heard good things about Kotlin

pine sage
#

so the fact that its classfiles under the hood isn't that important

reef narwhal
#

No, I mean, it's turing complete and it's not super obnoxious, it's just one of those things that is more annoying to deal with in Java than C++ which is rare

#

you can make spaghetti monsters of six different function calls that all resolve at compile time in C++

#

I've fucked myself way harder with templates in C++

pine sage
# reef narwhal I've heard good things about Kotlin

my opinion on kotlin is that when it came out it was much better than Java. It had many things Java didn't have. Nowadays Java has 80% caught up and kotlin hasn't really kept up + some of the things unique to kotlin actually are net negatives

#

so i sorta don't care - but yeah they do have package level functions

reef narwhal
pine sage
#

it can certainly get insane, but in a different way than c++

#

and you're in the same ballpark speedwise for most things

#

the original target audience for Java was legitimately C++ developers - you can see it in documentation, marketing, etc.

reef narwhal
#

Alright well I gave up on generics

#

going to be easier to copy and paste 36 files than deal with Java generics

pine sage
#

yeah life advice - just write the code

#

not related to java generics even, just a thing

reef narwhal
#

not really sure what that means

#

i'm writing the code? I just was trying to make life easier

#

it was worth a shot

#

just calling it quits because the investment is no longer worth the outcome

reef narwhal
#

I'm also not really writing code, just refactoring by hand

#

and copy pasting

pine sage
reef narwhal
#

that's why i reached for generics

pine sage
#

some of these work out

#

some don't

#

but the dumbest possible solution will often work - like just copy pasting the code

#

and you know how long it will take

reef narwhal
#

I don't midn doing it a few times, but I am not kidding when I say I have to copy past 36 different files

#

I always say at work, "sometimes you gotta earn your check"

#

chores suck 😩

pine sage
#

a key insight is that, no matter what, writing code takes up a lot less of your time than reading code

#

which isn't true for an assignment but is true for real code and real software

reef narwhal
#

oh yeah, i worked in dolphin (open source game cube emulator) and writing code was such a minimal part of that project lol

#

you can just fuck yourself by making it hard to read

#

so hope you don't ever have to extend it

pine sage
#

so if you do something that makes it easier to write the code (like what you were thinking with the generics) but makes it more complicated/harder to read/spreads out logic - that is often a net negative

reef narwhal
#

oh i see what you're saying

#

that's a good point

#

huh

#

hadn't thought of it like that before

#

sometimes a 300 line switch statement is the way to handle all of the dialogue in a game

#

(sorry, that's how undertale did his dialogue. I realize that might not be common knowledge)

pine sage
#

but you know what? pretty simple and you aren't looking at all 10,000 lines at once

reef narwhal
pine sage
reef narwhal
#

idk, the more things i do and make professionally (in manufacturing and software) the more I realize the answer to "what's the best way to do this?" is rarely a relevant question

#

"what will get this done by the due date?" is a much more relevant question

pine sage
#

but you can never escape needing to write code for other people to read

#

since you of a month from now is meaningfully different than the you of today

reef narwhal
#

a month?

#

try an hour

#

lol

pine sage
#

okay dumbass

#

most of us ship of theseus slower

#

but you get what I mean

#

there is a cost that you pay later for abstraction and indirection

pine sage
#

you make a choice that helps you today but gets in the way of what you want to do tomorrow. It is unavoidable to end up with some in the presence of any deadlines

#

its not intrinsically good or bad

reef narwhal
#

i was just making a self-deprecating joke

#

my memory is pretty bad

#

and it gets worse the more i use it in a day

pine sage