#Instantiate class with String name
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
I don't understand your question
I would like this to be done in a for loop rather than needing a bunch of cases, also assume that arg2 is valid
switch(arg2) {
case "foo":
foo.prepare();
foo.installation();
break;
case "bar":
bar.prepare();
bar.installation();
break;
}
}
many ways doing it, all those classes have a common interface?
I don't want to write 400 cases
I could automate it a bit with bash, sure, but it'll still be messy code
nah I mean why are you referencing a class by string in the first place
...because I just want to use a for loop for it instead of writing cases
I am asking you for context
maybe I'm misunderstanding your question
for me it sounds like you doing smth wrong in the first place
I have a bunch of classes and I need to access one depending on user input
but why?
I need to execute very specific very different code based on user input
just share your actual code and actual goal instead of placeholders
If you want to react from user input then it is wrong to instantiate a class from its name... since the user shouldn't know the internal structure of the code
they are just one liners
can you just tell me how to do it
do what ?
So you still want to instantiate classes from name even if it is a wrong approach ?
var instance = switch(arg2) {
case "foo" -> new Foo();
case "bar" -> new Bar();
};
instance.prepare();
instance.installation();
That's the correct approach
basically what you have but with one liners for each case
@slim fern
still a bit annoying that its done manually
also not sure why i said for loop here
the problem is, like I said, that if you go by class name, you will tie your user interaction to your code structure... when they should not be tied together
what do you mean by that
what do you not understand ?
What I mean
is
the user will have to enter class names
so the user will have to input stuff that are intern to your program
yea i dont see a problem with that for this
well okay
you can lookup a class by name
i'm gonna be "cool uncle" and show you what you shouldn't do
What if the class name isn't obvious for the user ? you can't just make a specific name
what if there is any problem with the name in fact, you won't be able to do anything, the structure of your program is now stuck
also, do all your classes that you want to instantiate by name have the same constructor parameters ?
the class name is obvious and if theres a problem with the name wont it just error out
that's not what I am talking about
AnUserDoesntSpeakCamelCase
and if there is a problem with a name that you want to change
you simply can't
why cant i change the name?
this is primarily for me
because you will have to rename the class
and whats the problem with that
which may be way heavier than changing a string
A browser interface to the Java compiler to experiment with the language
in term of code that you have to edit
and also
a class name cant be everything
but if it is for you I guess it is fine
kinda
in terms of code i have to edit doing this solution wil be a lot worse because it'll tack an extra 400+ lines onto my code
class Main {
public static void main(String[] args) throws Exception {
var input = "thing";
var o = (Op) (Class.forName(input).getConstructor().newInstance());
o.doThing();
}
}
interface Op {
void doThing();
}
class thing implements Op {
public thing() {}
@Override
public void doThing() {
System.out.println("a");
}
}
class other implements Op {
public other() {}
@Override
public void doThing() {
System.out.println("b");
}
}
just be very wary
do you have 400+ classes ?
i need this to be flexible, it could very well reach that number
but this is how you do that
or at least one way
there is another, better, way using a service provider
@slim fern and also, do all your classes have the same constructor parameters ?
yea
you still need to write out all 400 things
and are all the classes you want to instantiate in the same package ?
but it isn't as vulnerable to remote code execution
i'll implement this when i have access to my laptop
running this code in a standalone java file works just fine however putting it into maven it gives me the error "var cannot be resolved to a type"
set source and target to a newer java version
are maven.compiler.source and maven.compiler.target not the source and target for the maven version?
in IJ settings, go to Build, Execution, Deployment. go to Maven
what does it say for JDK for importer?
oh, no clue how neovim integrates maven
are you running using mvn command?
maven is its own thing, but the IDE also needs to support the functionality too. usually the IDE updates the language level based on maven config, sometimes it doesn't
you're describing lsp and I do have a java lsp for neovim but this is completely unrelated
I'm just running maven and java via the command line
it's purely a maven issue
just checked your pom, your settings are for 3.9.2
have you looked up the properties online?
pretty sure just about any article about them would explain
that kind of info would have been useful, could also say "im not using an IDE to run this"
so, you don't know what it does
...what?
just because i'm not spoonfeeding you doesn't mean i dont know what it does
well what does it do? I've changed it to 20 (my jdk version) and that's done nothing, I've tried changing a bunch of parameters and those have all done nothing
there's a reason I came for help here yknow
dont need to be rude
you are getting help, are you not?
free help, to say the least
theres the docs for the properties
I've already read it
okay, but you have 3.9.2 in your pom
and I already said i've changed it to 20 which did nothing
okay, so show your updated pom, so we can make sure you did it right
I've also tried setting the release property which also did nothing
also show that you're using a proper version of java for that feature
changing 3.9.2 to 20 was the only change made
I'm using openjdk 20, again this is a maven issue as just running it as a standalone java file works with no issues
What command did you use to compile the project
mvn package
maven uses the jdk, so understanding which jdk maven is using matters
what version of maven are you using?
and just because running standalone works doesnt mean maven will use the same java that's used in your standalone environment
Try mvn clean install
3.9.2
no those are the source and target for the java compiler
so you want probably anything 11-21 in there