Created
September 16, 2018 00:52
-
-
Save rupert-ong/5a6f4c107ac8e9530898af624a3ac13d to your computer and use it in GitHub Desktop.
Java: Instance Creation and Invocation with Runtime Reflection #java #reflection #runtime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ps.utils; | |
import com.ps.finance.BankAccount; | |
import com.ps.finance.HighVolumeAccount; | |
public class AccountWorker implements Runnable { | |
private BankAccount ba; | |
private HighVolumeAccount hva; | |
private char txType = 'w'; | |
public AccountWorker(BankAccount ba) { | |
this.ba = ba; | |
} | |
public AccountWorker(HighVolumeAccount hva) { | |
this.hva = hva; | |
} | |
public void doWork() { | |
// HighVolumeBankAccount already implements Runnable | |
Thread t = new Thread(hva != null ? hva : this); | |
t.start(); | |
try { | |
t.join(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void setTxType(char type) { | |
this.txType = type; | |
} | |
@Override | |
public void run() { | |
int amt = 50; | |
if (txType == 'w') | |
ba.withdraw(amt); | |
else | |
ba.deposit(amt); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ps.finance; | |
public class BankAccount { | |
private String id; | |
private int balance; | |
public BankAccount(String id) { | |
this.id = id; | |
} | |
public BankAccount(String id, int balance) { | |
this.id = id; | |
this.balance = balance; | |
} | |
public String getId() { | |
return id; | |
} | |
public synchronized int getBalance() { | |
return balance; | |
} | |
public synchronized void deposit (int amount) { | |
this.balance += amount; | |
} | |
public synchronized void withdraw (int amount) { | |
this.balance -= amount; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ps.finance; | |
public final class HighVolumeAccount extends BankAccount implements Runnable { | |
public HighVolumeAccount(String id) { super(id); } | |
public HighVolumeAccount(String id, int balance) { super(id, balance); } | |
private int[] readDailyDeposits() { | |
return new int[100]; | |
} | |
private int[] readDailyWithdrawals() { | |
return new int[100]; | |
} | |
@Override | |
public void run() { | |
for(int depositAmt : readDailyDeposits()) | |
deposit(depositAmt); | |
for(int withdrawalAmt : readDailyWithdrawals()) | |
withdraw(withdrawalAmt); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ps; | |
import com.ps.finance.BankAccount; | |
import java.lang.reflect.Constructor; | |
import java.lang.reflect.Method; | |
public class InstanceCreationAndInvocation { | |
public static void main(String[] args) { | |
doWorkOnBankAccount(); | |
} | |
private static void doWorkOnBankAccount() { | |
BankAccount acct = new BankAccount("1234"); | |
startWork("com.ps.utils.AccountWorker", acct); | |
System.out.println("New balance is " + acct.getBalance()); | |
} | |
private static void startWork(String workerTypeName, Object workerTarget) { | |
try { | |
Class<?> workerType = Class.forName(workerTypeName); | |
Class<?> targetType = workerTarget.getClass(); | |
// Get constructor based on the Class parameter you pass to it | |
Constructor c = workerType.getConstructor(targetType); | |
Object worker = c.newInstance(workerTarget); | |
// Get method and invoke | |
Method doWork = workerType.getMethod("doWork"); | |
doWork.invoke(worker); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment