#How to get the class of a generic at runtime?
1 messages · Page 1 of 1 (latest)
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
short answer: you can't
Is there a way to do this with generics?
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
yeah
gimmie a sec i'll write you a demo
because generics only exist at compile time
there is an exception tho
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
i know, i am talking to you online
there are multiple answers to that
what assumptions?
yeah, exactly, so it can get the class type, right?
i don't understand how classes are handled in java
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
I'd prefer java to not need classes up my ass and out my eyes D:
(I am coming from C++ primarily)
- that the class is only being extended once - otherwise
this.getClass().getGenericSuperclass()might not beBaseClass - That the type arguments are supplied
- That the type argument is a class and not itself another generic
which means the assumptions hold
you can make checks to ensure they do - which might be helpful since you can provide better errors
there is good and bad news
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(...);
this is just for a class assignment, i'm not worried about it after this class
the ship just needs to get to shore 🫡
and a lot of stuff - like public everywhere - are borne out of monkey see monkey do more often than legitimate need
yeah, that's the part that is really really wack coming from C
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
there are languages based on the JVM which genuinely just have functions
I've heard good things about Kotlin
so the fact that its classfiles under the hood isn't that important
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++
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
As Stroustrup said, "There are the languages that people complain about and the languages people don't use"
yeah you'll have a simpler life in java
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.
Alright well I gave up on generics
going to be easier to copy and paste 36 files than deal with Java generics
yeah life advice - just write the code
not related to java generics even, just a thing
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
please elaborate
I'm also not really writing code, just refactoring by hand
and copy pasting
there are a lot of ways to "be smart" in programming
that's why i reached for generics
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
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 😩
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
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
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
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)
its actually a common thing for "popular video game DESTROYED" for people to look at code that is like a 10,000 line function and go "eww bad"
but you know what? pretty simple and you aren't looking at all 10,000 lines at once
true, but unless you know what you're looking for, you have to scroll through all of it
correct. and that matters more on larger teams than smaller ones
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
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
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
this where "tech debt" comes from
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
oh 🙁
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
i know