Merge pull request #14727 from parthiv39731/PR-6669
BAEL-6669, Passing Class Name as Parameter in Java
This commit is contained in:
commit
8bdab701bf
@ -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
|
@ -0,0 +1,67 @@
|
|||||||
|
package com.baeldung.reflection.createobject;
|
||||||
|
|
||||||
|
import com.baeldung.reflection.createobject.basic.*;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
public class CreateObjectBasicUnitTest {
|
||||||
|
@Test
|
||||||
|
public void givenBronzeJobCard_whenJobTypeRepairAndMaintenance_thenStartJob() throws ClassNotFoundException,
|
||||||
|
InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
|
||||||
|
|
||||||
|
BronzeJobCard bronzeJobCard1 = new BronzeJobCard();
|
||||||
|
bronzeJobCard1.setJobType("com.baeldung.reflection.createobject.basic.RepairJob");
|
||||||
|
assertEquals("Start Bronze Repair Job", bronzeJobCard1.startJob());
|
||||||
|
|
||||||
|
BronzeJobCard bronzeJobCard2 = new BronzeJobCard();
|
||||||
|
bronzeJobCard2.setJobType("com.baeldung.reflection.createobject.basic.MaintenanceJob");
|
||||||
|
assertEquals("Start Bronze Maintenance Job", bronzeJobCard2.startJob());
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void givenBronzeJobCard_whenJobTypePaint_thenFailJob() throws ClassNotFoundException,
|
||||||
|
InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
|
||||||
|
|
||||||
|
BronzeJobCard bronzeJobCard = new BronzeJobCard();
|
||||||
|
bronzeJobCard.setJobType("com.baeldung.reflection.createobject.basic.PaintJob");
|
||||||
|
assertEquals("Bronze Job Failed", bronzeJobCard.startJob());
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void givenSilverJobCard_whenJobTypeRepairAndMaintenance_thenStartJob() throws InvocationTargetException,
|
||||||
|
NoSuchMethodException, InstantiationException, IllegalAccessException {
|
||||||
|
|
||||||
|
SilverJobCard silverJobCard1 = new SilverJobCard();
|
||||||
|
silverJobCard1.setJobType(RepairJob.class);
|
||||||
|
assertEquals("Start Silver Repair Job", silverJobCard1.startJob());
|
||||||
|
|
||||||
|
SilverJobCard silverJobCard2 = new SilverJobCard();
|
||||||
|
silverJobCard2.setJobType(MaintenanceJob.class);
|
||||||
|
assertEquals("Start Silver Maintenance Job", silverJobCard2.startJob());
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void givenSilverJobCard_whenJobTypePaint_thenFailJob() throws ClassNotFoundException,
|
||||||
|
InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
|
||||||
|
|
||||||
|
SilverJobCard silverJobCard = new SilverJobCard();
|
||||||
|
silverJobCard.setJobType(PaintJob.class);
|
||||||
|
assertEquals("Silver Job Failed", silverJobCard.startJob());
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void givenGoldJobCard_whenJobTypeRepairMaintenanceAndPaint_thenStartJob() throws InvocationTargetException,
|
||||||
|
NoSuchMethodException, InstantiationException, IllegalAccessException {
|
||||||
|
|
||||||
|
GoldJobCard<RepairJob> goldJobCard1 = new GoldJobCard<RepairJob>();
|
||||||
|
goldJobCard1.setJobType(RepairJob.class);
|
||||||
|
assertEquals("Start Gold Repair Job", goldJobCard1.startJob());
|
||||||
|
|
||||||
|
GoldJobCard<MaintenanceJob> goldJobCard2 = new GoldJobCard<MaintenanceJob>();
|
||||||
|
goldJobCard2.setJobType(MaintenanceJob.class);
|
||||||
|
assertEquals("Start Gold Maintenance Job", goldJobCard2.startJob());
|
||||||
|
|
||||||
|
GoldJobCard<PaintJob> goldJobCard3 = new GoldJobCard<PaintJob>();
|
||||||
|
goldJobCard3.setJobType(PaintJob.class);
|
||||||
|
assertEquals("Start Gold Paint Job", goldJobCard3.startJob());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.baeldung.reflection.createobject;
|
||||||
|
|
||||||
|
import com.baeldung.reflection.createobject.special.MaintenanceJob;
|
||||||
|
import com.baeldung.reflection.createobject.special.PaintJob;
|
||||||
|
import com.baeldung.reflection.createobject.special.PlatinumJobCard;
|
||||||
|
import com.baeldung.reflection.createobject.special.RepairJob;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
|
||||||
|
public class CreateObjectSpecialUnitTest {
|
||||||
|
@Test
|
||||||
|
public void givenPlatinumJobCard_whenJobTypeRepairMaintenanceAndPaint_thenStartJob() throws InvocationTargetException,
|
||||||
|
NoSuchMethodException, InstantiationException, IllegalAccessException {
|
||||||
|
|
||||||
|
PlatinumJobCard<RepairJob> platinumJobCard1 = new PlatinumJobCard<RepairJob>();
|
||||||
|
platinumJobCard1.setJobType(RepairJob.class);
|
||||||
|
assertEquals("Start Platinum Repair Job", platinumJobCard1.startJob());
|
||||||
|
|
||||||
|
PlatinumJobCard<MaintenanceJob> platinumJobCard2 = new PlatinumJobCard<MaintenanceJob>();
|
||||||
|
platinumJobCard2.setJobType(MaintenanceJob.class);
|
||||||
|
assertEquals("Start Platinum Maintenance Job", platinumJobCard2.startJob());
|
||||||
|
|
||||||
|
PlatinumJobCard<PaintJob> platinumJobCard3 = new PlatinumJobCard<PaintJob>();
|
||||||
|
platinumJobCard3.setJobType(PaintJob.class);
|
||||||
|
assertEquals("Start Platinum Paint Job", platinumJobCard3.startJob());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user