1、接口及实现类定义

UserService 接口 :

package com.journey.sdk;

import java.util.List;
import java.util.Map;

public interface UserService {

    Result<String> addUser(int id, String name, int age, String[] phones, List<String> hobbies, Map<String, String> scores);
}

UserServiceImpl 实现类 :

package com.journey.sdk.impl;

import com.journey.sdk.Result;
import com.journey.sdk.UserService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {
    public Result<String> addUser(int id, String name, int age, String[] phones, List<String> hobbies, Map<String, String> scores) {
        System.out.println("用户id :" + id);
        System.out.println("用户name :" + name);
        System.out.println("用户age :" + age);
        System.out.println("用户phones :" + Arrays.asList(phones));
        System.out.println("用户hobbies :" + hobbies);
        System.out.println("用户scores :" + scores);

        Result<String> result = new Result<String>();
        result.setCode(1);
        result.setMessage("success");
        return result;
    }
}

Result 结果返回类 :

package com.journey.sdk;

public class Result<T> {

    private int code;
    private String message;
    private T data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}

2、反射

package com.cestc.bigdata.java;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Demo {

    public static void main(String[] args) {
        // jar包地址
        String jarPath="/Users/xxxx/Downloads/my-sdk-1.0-SNAPSHOT.jar";
        try {
            // 获取生成的URL地址
            URL url = new URL("file:" + jarPath);
            //获取类加载器
            URLClassLoader classLoader = new URLClassLoader(new URL[]{url},
                    Thread.currentThread().getContextClassLoader());
            // 获取对应的类
            Class<?> clazz = classLoader.loadClass("com.journey.sdk.impl.UserServiceImpl");
            // 获取实例
            Object object = clazz.newInstance();

            System.out.println(object);

            List<String> hobbies = Arrays.asList("football", "basketball");
            String[] phones = {"111111111111", "22222222222"};
            Map<String, String> scores = new HashMap<>();
            scores.put("yuwen", "20");
            scores.put("shuxue", "100");

            Method addUserMethod = clazz.getDeclaredMethod("addUser",
                    int.class, String.class, int.class, String[].class, List.class, Map.class);

            Object result = addUserMethod.invoke(object, 1, "zhangsan", 20, phones, hobbies, scores);
            System.out.println("result -> " + result);


            Class<?> resultClazz = result.getClass();
            Field field = resultClazz.getDeclaredField("code");
            field.setAccessible(true);
            int code = (int)field.get(result);

            System.out.println("code -> " + code);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}

结果 :

com.journey.sdk.impl.UserServiceImpl@12a3a380
用户id :1
用户name :zhangsan
用户age :20
用户phones :[111111111111, 22222222222]
用户hobbies :[football, basketball]
用户scores :{yuwen=20, shuxue=100}
result -> Result{code=1, message='success', data=null}
code -> 1

如感兴趣,点赞加关注,谢谢!!!


journey
32 声望22 粉丝