Example java static method calls using reflection

brief introduction

It introduces the mechanism uses reflection to call the static method to perform class.

Static method

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;
    }
}

transfer

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());

Output

14 2019-07-03: 51 is: the INFO 332 08.816 --- [NiO-Exec-2-8080 ] com.lgt.demo2.gisService.util.GisUtil: entering gisService1 method, the parameter [STR]: test_service_1
 2019-07- 14 03: 51 is: the INFO 332 08.852 --- [NiO-Exec-2-8080 ] cldfcontroller.StringController: gisService1 returns:
 2019-07-03 14: 51 is: the INFO 332 08.853 --- [NiO-8080-2-Exec ] cldfcontroller.StringController: the jobId: 2019-07-03 14:51:08 
2019-07-03 14: 51 is: the INFO 332 08.853 --- [NiO-Exec-2-8080 ] cldfcontroller.StringController: OutFilePath: E: \ DocDirectory \ Test.doc
 2019-07-03 14: 51 is: the INFO 332 08.853 --- [NiO-Exec-2-8080 ] com.lgt.demo2.gisService.util.GisUtil: method ... enter 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

Used in the entity class

public  class ExportMethodResultDto the implements the Serializable { 

    Private String outFilePath; // generated absolute file path 
    Private String the jobId; // of GIS calls an asynchronous service response the jobId 

    public String getOutFilePath () {
         return outFilePath; 
    } 

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

    public String getJobId () {
         return the jobId; 
    } 

    public  void setJobId (String the jobId) {
         the this.jobId = jobId;
    }
}
ExportMethodResultDto
public  class ExportProjectRasterParam the implements the Serializable {
     Private String inCoorSystem; // input coordinates 
    Private String outCoorSystem; // output coordinate system 
    Private String resamplingType; // resampling algorithm (optional) 
    Private String CELLSIZE; // Cell Size (optional) 

    public String getInCoorSystem () {
         return inCoorSystem; 
    } 

    public  void setInCoorSystem (String inCoorSystem) {
         the 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

 

Guess you like

Origin www.cnblogs.com/codecat/p/11114469.html