November 13th, 2007 No Comments »
회사서 Class를 조사할 일이 있는데 reflect를 까먹어서 한동안 googling 해서 다시 학습…
java.lang.String의 method를 출력하는 루틴의 골자만 요약하면….
import java.lang.reflect.*;
public class ClassExam{
public void printClassInfo(String className){
try{
Class a = this.getClass().getClassLoader().loadClass(className);
Method[] methods = a.getDeclaredMethods();
for(int i=0; i<methods.length; i++){
System.out.println(methods[i].toString());
}
}catch(Exception e){
}
}
public static void main(String[] args){
ClassExam a = new ClassExam();
a.printClassInfo("java.lang.String");
}
}
태그: codemonkey, codesnipet, Computer, java, 프로그래밍
카테고리: Computer
August 30th, 2006 No Comments »
Unix스타일의 프로그램 파라미터 처리를 해주는 모듈이 Getopt인데
자주 사용하다 보니 codesnipet으로 사용하려고, 내가 사용하는 템플릿을 여기에 등록해 둔다.
main::HELP_MESSAGE(), main::VERSION_MESSAGE() 를 구현해 두면 - -help, - -version 옵션에 반응하고, getopts(c:hv)에 사용할 옵션들을 나열하면 된다.
위 예에서 c뒤에 : 의 의미는 파라미터를 받는 다는 의미이다.
입력된 파라미터는 $Getopt::Std::opt_c로 사용할 수 있다.
#!/usr/bin/perl
use strict;
use Getopt::Std;
$Getopt::Std::STANDARD_HELP_VERSION = 1;
sub main::VERSION_MESSAGE(){
print "$0 Version 0.5 hyeonkwen@gmail.com";
}
sub main::HELP_MESSAGE(){
print <<END;
Description of this program.
Usage: $0 OPTION DIRECTORY
OPTION
-c description of c option
-h view help message
-v view version information
END
}
sub main(){
if(!getopts("c:hv") || $Getopt::Std::opt_h){
main::HELP_MESSAGE();
exit;
}elsif($Getopt::Std::opt_v){
main::VERSION_MESSAGE();
exit;
}
}
main();
태그: codesnipet, perl, 프로그래밍
카테고리: Computer