BAEL-6669, Passing Class Name as Parameter in Java

This commit is contained in:
parthiv39731
2023-09-06 03:02:37 -07:00
parent 6477f9694a
commit b066bce2b6
17 changed files with 305 additions and 0 deletions
@@ -0,0 +1,21 @@
package com.baeldung.reflection.createobject.basic;
import java.lang.reflect.InvocationTargetException;
public class BronzeJobCard {
private Object jobType;
public void setJobType(String jobType) throws ClassNotFoundException,
NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Class jobTypeClass = Class.forName(jobType);
this.jobType = jobTypeClass.getDeclaredConstructor().newInstance();
}
public String startJob() {
if(this.jobType instanceof RepairJob) {
return "Start Bronze " + ((RepairJob) this.jobType).getJobType();
}
if(this.jobType instanceof MaintenanceJob) {
return "Start Bronze " + ((MaintenanceJob) this.jobType).getJobType();
}
return "Bronze Job Failed";
}
}
@@ -0,0 +1,17 @@
package com.baeldung.reflection.createobject.basic;
import java.lang.reflect.InvocationTargetException;
public class GoldJobCard<T> {
private T jobType;
public void setJobType(Class<T> jobTypeClass) throws
NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
this.jobType = jobTypeClass.getDeclaredConstructor().newInstance();
}
public String startJob() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
return "Start Gold " + this.jobType.getClass().getMethod("getJobType")
.invoke(this.jobType).toString();
}
}
@@ -0,0 +1,7 @@
package com.baeldung.reflection.createobject.basic;
public class MaintenanceJob {
public String getJobType() {
return "Maintenance Job";
}
}
@@ -0,0 +1,7 @@
package com.baeldung.reflection.createobject.basic;
public class PaintJob {
public String getJobType() {
return "Paint Job";
}
}
@@ -0,0 +1,7 @@
package com.baeldung.reflection.createobject.basic;
public class RepairJob {
public String getJobType() {
return "Repair Job";
}
}
@@ -0,0 +1,22 @@
package com.baeldung.reflection.createobject.basic;
import java.lang.reflect.InvocationTargetException;
public class SilverJobCard {
private Object jobType;
public void setJobType(Class jobTypeClass) throws
NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
this.jobType = jobTypeClass.getDeclaredConstructor().newInstance();
}
public String startJob() {
if (this.jobType instanceof RepairJob) {
return "Start Silver " + ((RepairJob) this.jobType).getJobType();
}
if (this.jobType instanceof MaintenanceJob) {
return "Start Silver " + ((MaintenanceJob) this.jobType).getJobType();
}
return "Silver Job Failed";
}
}
@@ -0,0 +1,5 @@
package com.baeldung.reflection.createobject.special;
public interface Job {
String getJobType();
}
@@ -0,0 +1,7 @@
package com.baeldung.reflection.createobject.special;
public class MaintenanceJob implements Job {
public String getJobType() {
return "Maintenance Job";
}
}
@@ -0,0 +1,8 @@
package com.baeldung.reflection.createobject.special;
public class PaintJob implements Job {
@Override
public String getJobType() {
return "Paint Job";
}
}
@@ -0,0 +1,16 @@
package com.baeldung.reflection.createobject.special;
import java.lang.reflect.InvocationTargetException;
public class PlatinumJobCard<T extends Job> {
private T jobType;
public void setJobType(Class<T> jobTypeClass) throws
NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
this.jobType = jobTypeClass.getDeclaredConstructor().newInstance();
}
public String startJob() {
return "Start Platinum " + this.jobType.getJobType();
}
}
@@ -0,0 +1,7 @@
package com.baeldung.reflection.createobject.special;
public class RepairJob implements Job {
public String getJobType() {
return "Repair Job";
}
}
@@ -0,0 +1,20 @@
@startuml
'https://plantuml.com/class-diagram
class BronzeJobCard {
-Object jobType
+setJobType(String jobType)
+startJob()
}
class MaintenanceJob {
+getJobType()
}
class RepairJob {
+getJobType()
}
BronzeJobCard -left-> MaintenanceJob:creates
BronzeJobCard -right-> RepairJob:creates
@enduml
@@ -0,0 +1,20 @@
@startuml
'https://plantuml.com/class-diagram
class GoldJobCard<T> {
-T jobType
+setJobType(Class<T> jobTypeClass)
+startJob()
}
class MaintenanceJob {
+getJobType()
}
class RepairJob {
+getJobType()
}
GoldJobCard -left-> MaintenanceJob:creates
GoldJobCard -right-> RepairJob:creates
@enduml
@@ -0,0 +1,25 @@
@startuml
'https://plantuml.com/class-diagram
interface Job {
+getJobType
}
class PlatinumJobCard <T extends Job> {
+setJobType(Class<T> jobTypeClass)
+startJob()
}
class MaintenanceJob implements Job {
+getJobType()
}
class RepairJob implements Job {
+getJobType()
}
class PaintJob implements Job {
+getJobType()
}
PlatinumJobCard -up-> MaintenanceJob:creates
PlatinumJobCard -up-> RepairJob:creates
PlatinumJobCard -up-> PaintJob:creates
@enduml
@@ -0,0 +1,20 @@
@startuml
'https://plantuml.com/class-diagram
class SilverJobCard {
-Object jobType
+setJobType(Class jobTypeClass);
+startJob();
}
class MaintenanceJob {
+getJobType();
}
class RepairJob {
+getJobType();
}
SilverJobCard -left-> MaintenanceJob:creates
SilverJobCard -right-> RepairJob:creates
@enduml