Created
November 14, 2012 22:04
-
-
Save WayneSan/4075149 to your computer and use it in GitHub Desktop.
Construct inner class instance base on extended class.
This file contains hidden or 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
import java.lang.reflect.Constructor; | |
public class Master | |
{ | |
public Master() | |
{ | |
System.out.println( "Master >> " + this.getClass().getName() ); | |
} | |
public interface IJob { | |
IJob getJob(); | |
} | |
public class JobDecorator implements IJob | |
{ | |
private IJob job; | |
@Override | |
public IJob getJob() { | |
return job; | |
} | |
public JobDecorator( Master master ) | |
{ | |
Class<? extends Master> master_class = master.getClass(); | |
String job_class_name = master_class.getName() + "$Job"; | |
try { | |
Class<? extends IJob> job_class = | |
(Class<? extends IJob>) Class.forName( job_class_name ); | |
Constructor<? extends IJob> job_constructor = | |
job_class.getConstructor( master_class ); | |
this.job = job_constructor.newInstance( master ); | |
} catch ( ClassNotFoundException e ) { | |
// TODO: find Job in super class | |
e.printStackTrace(); | |
} catch ( Exception e ) { | |
System.err.println( "Can not construct " + job_class_name + " object." ); | |
e.printStackTrace(); | |
} | |
} | |
} | |
public class Job implements IJob | |
{ | |
public Job() | |
{ | |
System.out.println( "Master.Job >> " + getJob().getClass().getName() ); | |
} | |
@Override | |
public IJob getJob() { | |
return this; | |
} | |
} | |
} |
This file contains hidden or 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
Master >> SubMaster | |
SubMaster >> SubMaster | |
RunMaster >> SubMaster | |
Master.Job >> SubMaster$Job | |
SubMaster.Job >> SubMaster$Job | |
RunMaster.Job >> SubMaster$Job | |
main() >> SubMaster$Job |
This file contains hidden or 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
public class RunMaster { | |
Object master; | |
Job job; | |
public RunMaster( String classname ) throws Exception | |
{ | |
master = Class.forName( classname ).newInstance(); | |
System.out.println( "RunMaster >> " + master.getClass().getName() ); | |
job = new Job(); | |
} | |
class Job extends Master.JobDecorator | |
{ | |
public Job() { | |
( (Master) master ).super( (Master) master ); | |
System.out.println( "RunMaster.Job >> " + getJob().getClass().getName() ); | |
} | |
} | |
public static void main( String[] args ) throws Exception { | |
Master.IJob job = new RunMaster( "SubMaster" ).job.getJob(); | |
System.out.println( "main() >> " + job.getClass().getName() ); | |
} | |
} |
This file contains hidden or 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
public class SubMaster extends Master | |
{ | |
public SubMaster() | |
{ | |
System.out.println( "SubMaster >> " + this.getClass().getName() ); | |
} | |
public class Job extends Master.Job | |
{ | |
public Job() | |
{ | |
super(); | |
System.out.println( "SubMaster.Job >> " + getJob().getClass().getName() ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment