Last active
December 14, 2015 11:29
-
-
Save 3100/5079902 to your computer and use it in GitHub Desktop.
a class reflection sample in Java.
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
public class DryRunner extends RunnerBase { | |
public DryRunner(String file) { | |
super(file); | |
} | |
public Boolean delete() | |
{ | |
log("dry:delete."); | |
return true; | |
} | |
public Boolean doComplexThing(){ | |
log("dry:complex"); | |
return true; | |
} | |
} |
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
public interface IRunner { | |
public Boolean delete(); | |
public Boolean doComplexThing(); | |
} |
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
import java.lang.reflect.Constructor; | |
public class ReflectionSample { | |
public static void main(String[] args) { | |
run("huga", "DryRunner"); | |
run("hoge", "WetRunner"); | |
} | |
// 簡便のため、String fileに変更している。runnerNameにクラス名を指定。 | |
public static void run(String file, String runnerName){ | |
// 引数の型を定義 | |
Class<? extends String> fileParam = String.class; | |
try{ | |
Class<?> cls = Class.forName(runnerName); | |
Constructor ct = cls.getConstructor(fileParam); | |
RunnerBase runner = (RunnerBase)ct.newInstance(file);_ | |
runner.delete(); | |
runner.doComplexThing(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
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
public class RunnerBase implements IRunner { | |
protected String _file; | |
public RunnerBase(String file) { | |
_file = file; | |
} | |
public Boolean delete() | |
{ | |
return false; | |
} | |
public Boolean doComplexThing(){ | |
return false; | |
} | |
protected void log(String str){ | |
System.out.println(str); | |
} | |
} |
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
public class WetRunner extends RunnerBase { | |
public WetRunner(String file) { | |
super(file); | |
} | |
public Boolean delete() | |
{ | |
log("wet:delete."); | |
return true; | |
} | |
public Boolean doComplexThing(){ | |
log("wet:complex"); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment