#Instantiate class with String name

1 messages · Page 1 of 1 (latest)

slim fern
#

This is a bit difficult to explain, I have the following code (names are placeholders)

foo bar = new foo();
bar.method1();
bar.method2();

I would like foo to be a String so that if foo = "lorem" the code would instead be

lorem bar = new lorem();
fiery solarBOT
#

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

cinder cipher
slim fern
# cinder cipher 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;
  }
}
teal elbow
#

many ways doing it, all those classes have a common interface?

teal elbow
#

but why would you want that in the first place?

#

you should avoid using reflection

slim fern
#

I don't want to write 400 cases

#

I could automate it a bit with bash, sure, but it'll still be messy code

teal elbow
#

nah I mean why are you referencing a class by string in the first place

slim fern
#

...because I just want to use a for loop for it instead of writing cases

teal elbow
#

I am asking you for context

slim fern
#

maybe I'm misunderstanding your question

teal elbow
#

for me it sounds like you doing smth wrong in the first place

slim fern
#

I have a bunch of classes and I need to access one depending on user input

slim fern
#

I need to execute very specific very different code based on user input

compact otter
#

just share your actual code and actual goal instead of placeholders

cinder cipher
cinder cipher
slim fern
#

can you just tell me how to do it

cinder cipher
cinder cipher
# slim fern ** **

So you still want to instantiate classes from name even if it is a wrong approach ?

slim fern
#

well nobody has told me the right approach yet

#

just told me im wrong

cinder cipher
#
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

slim fern
#

still a bit annoying that its done manually

slim fern
cinder cipher
slim fern
#

what do you mean by that

cinder cipher
#

what do you not understand ?

slim fern
#

"you will tie your user interaction to your code structure"

#

what does that mean

cinder cipher
#

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

slim fern
#

yea i dont see a problem with that for this

rocky valley
#

you can lookup a class by name

#

i'm gonna be "cool uncle" and show you what you shouldn't do

cinder cipher
#

also, do all your classes that you want to instantiate by name have the same constructor parameters ?

slim fern
cinder cipher
#

AnUserDoesntSpeakCamelCase

#

and if there is a problem with a name that you want to change

#

you simply can't

slim fern
#

why cant i change the name?

slim fern
cinder cipher
#

because you will have to rename the class

slim fern
cinder cipher
#

which may be way heavier than changing a string

rocky valley
cinder cipher
#

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

slim fern
rocky valley
#
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");
    }  
}
slim fern
rocky valley
#

but this is how you do that

#

or at least one way

#

there is another, better, way using a service provider

cinder cipher
#

@slim fern and also, do all your classes have the same constructor parameters ?

slim fern
#

yea

rocky valley
#

you still need to write out all 400 things

cinder cipher
# slim fern yea

and are all the classes you want to instantiate in the same package ?

rocky valley
#

but it isn't as vulnerable to remote code execution

cinder cipher
#

you can use Ethan code

slim fern
slim fern
#

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"

rocky valley
slim fern
#

are maven.compiler.source and maven.compiler.target not the source and target for the maven version?

humble agate
#

what does it say for JDK for importer?

slim fern
#

I'm not using intellij

#

I'm using neovim

humble agate
#

oh, no clue how neovim integrates maven

slim fern
#

why would that matter

#

maven is its own standalone thing

humble agate
#

are you running using mvn command?

slim fern
#

yea

#

package it to a jar with mvn package then run that jar

humble agate
#

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

slim fern
#

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

humble agate
#

just checked your pom, your settings are for 3.9.2

humble agate
#

have you looked up the properties online?

#

pretty sure just about any article about them would explain

humble agate
slim fern
humble agate
#

...what?

#

just because i'm not spoonfeeding you doesn't mean i dont know what it does

slim fern
#

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

humble agate
#

you are getting help, are you not?

#

free help, to say the least

#

theres the docs for the properties

slim fern
#

I've already read it

humble agate
#

okay, but you have 3.9.2 in your pom

slim fern
#

and I already said i've changed it to 20 which did nothing

humble agate
#

okay, so show your updated pom, so we can make sure you did it right

slim fern
#

I've also tried setting the release property which also did nothing

humble agate
#

also show that you're using a proper version of java for that feature

slim fern
slim fern
viral nimbus
#

What command did you use to compile the project

slim fern
#

mvn package

humble agate
#

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

viral nimbus
slim fern
rocky valley
#

so you want probably anything 11-21 in there