当前位置:编程学习 > JAVA >>

反射获取接口跟指定注释的变量

通过反射获取类的一些属性
 
原始类
package test;
 
 
import org.osoa.sca.annotations.Property;
import org.osoa.sca.annotations.Reference;
 
 
public class AmdManagerImpl implements IAmdManager
{
ITest dao = null;
 
 
private String property1 = "";
 
 
@Reference
 
public void setDao(ITest dao) {
 
 
this.dao = dao;
}
@Reference
public ITest getDao() {
return dao;
}
 
 
public String getProperty1() {
return property1;
}
 
 
@Property
 
public void setProperty1(String property1) {
this.property1 = property1;
}
 
 
public void insert(Object data) {
dao.printStr();
}
 
 
}
字符串提供工具
package com.sunsheen.ids.composite.utils;
 
 
/***
 * 
 * 字符串提供器
 * 
 * @author minner
 * 
 */
public class StringValueProvider {
/**
* 注释标记
*  -@org.osoa.sca.annotations.Property 
* --@org.osoa.sca.annotations.Reference
* @author minner
*/
public static class Annotations_Value {
/*********** @Property 属性注释标记********/
public static String ANNPRO = "@org.osoa.sca.annotations.Property";
/*********** @Reference 服务注释标记 ********/
public static String ANNREF = "@org.osoa.sca.annotations.Reference";
 
 
}
 
}
}
 
package com.sunsheen.ids.composite.utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
 
import com.sunsheen.jfids.studio.javaassist.ClassPool;
import com.sunsheen.jfids.studio.javaassist.CtClass;
import com.sunsheen.jfids.studio.javaassist.CtMethod;
import com.sunsheen.jfids.studio.javaassist.NotFoundException;
import com.sunsheen.jfids.studio.javaassist.bytecode.AnnotationsAttribute;
import com.sunsheen.jfids.studio.javaassist.bytecode.MethodInfo;
 
 
/**
 * 
 * 反射工具类
 * 
 * @author minner
 * 
 */
public class ReflectUtil {
 
/**
* 通过反射获取接口
* @param claName
*            类名
* @return 接口数组
*/
public static List getRefIner(CtClass claName) {
// 保存所有的接口
List inter = new ArrayList();
CtClass intes[] = null;
 
 
try {
intes = claName.getInte易做图ces();
} catch (NotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 
 
for (int i = 0; i < intes.length; i++) {
String iner = intes[i].getName();
// 截取接口名字
if (iner.lastIndexOf(".") != -1) {
inter.add(iner.substring(iner.lastIndexOf(".") + 1,
iner.length()));
 
 
}
}
 
 
return inter;
}
 
/**
* 获取所有包含了某个注释的set方法的变量
* @param claName
*            类名
* @return Map<String, List> 注释名 ref pro 名字
*/
public static Map<String, List> getAnnRefAndPro(CtClass claName) {
// 定义一个map装所有属性跟引用用了注释的方法
Map<String, List> map = new HashMap<String, List>();
 
 
List<String> annpro = new ArrayList<String>();
List<String> annref = new ArrayList<String>();
 
 
// 获取所有方法
CtMethod[] ms = claName.getMethods();
for (int i = 0; i < ms.length; i++) {
// 获取所有方法信息
MethodInfo methodInfo = ms[i].getMethodInfo();
// 获取注释信息
AnnotationsAttribute atrr = (AnnotationsAttribute) methodInfo
.getAttribute(AnnotationsAttribute.visibleTag);
if (atrr != null) {
if (atrr.toString().equals(
StringValueProvider.Annotations_Value.ANNPRO)) {
if (ms[i].getName().startsWith("set")) {
//截取方法名获取变量
annpro.add(ms[i].getName().substring(3).toLowerCase());
 
 
}
 
 
}
if (atrr.toString().equals(
StringValueProvider.Annotations_Value.ANNREF)) {
if (ms[i].getName().startsWith("set")) {
annref.add(ms[i].getName().substring(3).toLowerCase());
 
 
}
 
 
}
}
 
 
}
map.put(StringValueProvider.Annotations_Value.ANNPRO, annpro);
map.put(StringValueProvider.Annotations_Value.ANNREF, annref);
return map;
}
测试类
package com.sunsheen.ids.composite.test;
 
 
import java.util.List;
import java.util.Map;
 
 
import com.sunsheen.ids.composite.utils.ReflectUtil;
import com.sunsheen.ids.composite.utils.StringValueProvider;
import com.sunsheen.jfids.studio.javaassist.ClassPool;
import com.sunsheen.jfids.studio.javaassist.CtClass;
 
 
public class Test2 {
public static void main(String[] args) {
ClassPool pool = new ClassPool();
 
 
String projectPath = "E:\\developmentEnvironment\\runtime-EclipseApplication\\p2";
 
 
try {
pool.insertClassPath(projectPath + "\\WebRoot\\WEB-INF\\classes");
String packagePath = "resources/f1/m1/src/test/AmdManagerImpl.java";
packagePath = packagePath.substring(packagePath.indexOf("src")+4,
packagePath.length());
packagePath = packagePath.replace(".java", "");
packagePath = packagePath.replace("/", ".");
packagePath = packagePath.replace("\\", ".");
CtClass cc = pool.get(packagePath);
//通过注释获取变量
Map<String, List> map = ReflectUtil.getAnnRefAndPro(cc);
 
List inter=ReflectUtil.getRefIner(cc);
for (int i = 0; i < inter.size(); i++) {
System.out.println("接口:" + inter.get(i)
补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,