java 利用反射调用静态方法的示例

内容简介

主要介绍使用反射的机制来调用执行类中的静态方法。

静态方法

public class GisUtil {

    private final static Logger logger = LoggerFactory.getLogger(GisUtil.class);

    public static ExportMethodResultDto gisService1 (String str){
        logger.info("进入gisService1方法,参数[str]:" + str);
        ExportMethodResultDto dto = new ExportMethodResultDto();
        LocalDateTime currentTime = LocalDateTime.now();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        dto.setJobId(df.format(currentTime));
        dto.setOutFilePath("E:\\DocDirectory\\test.doc");
        return dto;
    }

    public static ExportMethodResultDto gisService2 (ExportProjectRasterParam param){
        logger.info("进入gisService2方法...");
        logger.info("参数[param]=" + param);
        ExportMethodResultDto dto = new ExportMethodResultDto();
        String uuId = StrUtil.getUUID();
        dto.setJobId(uuId);
        dto.setOutFilePath("E:\\"+ uuId +"\\srtm_49_02_raster.shp");
        return dto;
    }
}

调用

String str = "test_service_1";
Class<?> threadClazz = Class.forName("com.lgt.demo2.gisService.util.GisUtil");
Method method = threadClazz.getMethod("gisService1", String.class);
ExportMethodResultDto dto = (ExportMethodResultDto)method.invoke(null, str);
logger.info("gisService1返回:");
logger.info("   jobId:"+ dto.getJobId());
logger.info("   OutFilePath:" + dto.getOutFilePath());

ExportProjectRasterParam param = new ExportProjectRasterParam();
param.setCellSize("1");
param.setResamplingType("NEAREST");
param.setInCoorSystem("GCS_WGS_1984");
param.setOutCoorSystem("GCS_China_Geodetic_Coordinate_System_2000");
Method method2 = threadClazz.getMethod("gisService2", ExportProjectRasterParam.class);
ExportMethodResultDto dto2 = (ExportMethodResultDto)method2.invoke(null,param);
logger.info("gisService2返回:");
logger.info("   jobId:"+ dto2.getJobId());
logger.info("   OutFilePath:" + dto2.getOutFilePath());

输出结果

2019-07-03 14:51:08.816  INFO 332 --- [nio-8080-exec-2] com.lgt.demo2.gisService.util.GisUtil    : 进入gisService1方法,参数[str]:test_service_1
2019-07-03 14:51:08.852  INFO 332 --- [nio-8080-exec-2] c.l.d.f.controller.StringController      : gisService1返回:
2019-07-03 14:51:08.853  INFO 332 --- [nio-8080-exec-2] c.l.d.f.controller.StringController      :    jobId:2019-07-03 14:51:08
2019-07-03 14:51:08.853  INFO 332 --- [nio-8080-exec-2] c.l.d.f.controller.StringController      :    OutFilePath:E:\DocDirectory\test.doc
2019-07-03 14:51:08.853  INFO 332 --- [nio-8080-exec-2] com.lgt.demo2.gisService.util.GisUtil    : 进入gisService2方法...
2019-07-03 14:51:08.854  INFO 332 --- [nio-8080-exec-2] com.lgt.demo2.gisService.util.GisUtil    : 参数[param]=[inCoorSystem:GCS_WGS_1984,outCoorSystem:GCS_China_Geodetic_Coordinate_System_2000,resamplingType:NEAREST,cellSize:1]
2019-07-03 14:51:08.858  INFO 332 --- [nio-8080-exec-2] c.l.d.f.controller.StringController      : gisService2返回:
2019-07-03 14:51:08.859  INFO 332 --- [nio-8080-exec-2] c.l.d.f.controller.StringController      :    jobId:79b4e82ef4564855881ecb4030ba39e2
2019-07-03 14:51:08.859  INFO 332 --- [nio-8080-exec-2] c.l.d.f.controller.StringController      :    OutFilePath:E:\79b4e82ef4564855881ecb4030ba39e2\srtm_49_02_raster.shp

用到的实体类

public class ExportMethodResultDto implements Serializable {

    private String outFilePath;//生成文件的绝对路径
    private String jobId;//GIS调用异步服务响应的jobId

    public String getOutFilePath() {
        return outFilePath;
    }

    public void setOutFilePath(String outFilePath) {
        this.outFilePath = outFilePath;
    }

    public String getJobId() {
        return jobId;
    }

    public void setJobId(String jobId) {
        this.jobId = jobId;
    }
}
ExportMethodResultDto
public class ExportProjectRasterParam implements Serializable {
    private String inCoorSystem;//输入坐标系
    private String outCoorSystem;//输出坐标系
    private String resamplingType;//重采样算法(可选)
    private String cellSize;//像元大小(可选)

    public String getInCoorSystem() {
        return inCoorSystem;
    }

    public void setInCoorSystem(String inCoorSystem) {
        this.inCoorSystem = inCoorSystem;
    }

    public String getOutCoorSystem() {
        return outCoorSystem;
    }

    public void setOutCoorSystem(String outCoorSystem) {
        this.outCoorSystem = outCoorSystem;
    }

    public String getResamplingType() {
        return resamplingType;
    }

    public void setResamplingType(String resamplingType) {
        this.resamplingType = resamplingType;
    }

    public String getCellSize() {
        return cellSize;
    }

    public void setCellSize(String cellSize) {
        this.cellSize = cellSize;
    }

    public String toString(){
        return String.format("[inCoorSystem:%s,outCoorSystem:%s,resamplingType:%s,cellSize:%s]",this.inCoorSystem,this.outCoorSystem,this.resamplingType,this.cellSize);
    }
}
ExportProjectRasterParam

 

posted @ 2019-07-03 15:16  代码猫  阅读(24021)  评论(0编辑  收藏  举报