package com.offcn.TestUnti;

import java.util.HashMap;

import com.offcn.interfaces.API;


/**
 * 反射工具类
 * 
 * @author yaoshuai
 *
 */
public class Reflect_api {

	public Class<?> ReflecClass(HashMap<String, Object> data) {
		String ClassName = Reflect_api.getRefleserviceUrlxClassName(data);
		Class<?> c = null;
		int count=1;
		while(true){
			try {
				c = Class.forName(ClassName);
				break;
			}catch (ClassNotFoundException e) {
				String serviceUrl = data.get("serviceUrl").toString();
				String CalssName_new = serviceUrl.split("/")[serviceUrl.split("/").length - 1];
				String system = MapUtil.getValue("system", data);
				String classRoute = ReadProperties.GetPropertyByKey(system);
				ClassName=classRoute+"process."+CalssName_new;
//				String system = MapUtil.getValue("system", data);
//				String classRoute = ReadProperties.GetPropertyByKey(system);
				count++;

			} 
			if(count==3){
				Log.logError("Reflect_api,ReflexClass Error，Classname=" + ClassName);
				break;
			}
		}
		return c;
	}
	public Object ReflecObj(Class<?> c){
		Object obj = null;
		try{
			obj = c.newInstance();
		}catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return obj;
	}

	// 获得数据中serviceUrl所对应的类名
	private static String getRefleserviceUrlxClassName(
			HashMap<String, Object> data) {
		String serviceUrl = data.get("serviceUrl").toString();
		String CalssName = serviceUrl.split("/")[serviceUrl.split("/").length - 1];

		String system = MapUtil.getValue("system", data);
		
		String classRoute = ReadProperties.GetPropertyByKey(system);
		//判断同样的类地址时候走哪一个测试类脚本
		if(system.contains("study")){
			String classReflectName = ReadProperties.GetPropertyByClassName(serviceUrl,system);
			if (	classReflectName!=null 	) {
				return classRoute+ classReflectName;
			}
		}
		
		return classRoute+CalssName;
	}
}
