Reflection: Get the interface and specified comments.

Source: Internet
Author: User

Obtain the original class 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 ;}@ Referencepublic ITest getDao () {return dao;} public String getProperty1 () {return property1 ;}@ Property public void setProperty 1 (String property1) {this. property1 = property1;} public void insert (Object data) {dao. printStr () ;}} the string provides the package com. sunsheen. ids. composite. utils;/***** string provider ** @ author minner **/public class StringValueProvider {/***** annotation mark *-@ org. osoa. sca. annotations. property * -- @ org. osoa. sca. annotations. reference ** @ author minner **/public static class Annotations_Value {/************ @ Property attribute Annotation Note * ******/public static String ANNPRO = "@ org. osoa. sca. annotations. property ";/*********** @ Reference Service annotation mark ********/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. sunshee N. 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; /***** reflection tool class ** @ author minner **/public class ReflectUtil {/***** obtain interface through reflection ** @ param claName * class name * @ return Port array */public static List getRefIner (CtClass claName) {// save all interfaces List inter = new ArrayList (); CtClass intes [] = null; try {intes = claName. getInterfaces ();} catch (NotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}for (int I = 0; I <intes. length; I ++) {String iner = intes [I]. getName (); // capture the interface name if (iner. lastIndexOf (". ")! =-1) {inter. add (iner. substring (iner. lastIndexOf (". ") + 1, iner. length () ;}} return inter ;} /*** get all the variables of the set method containing a comment ** @ param claName * class name * @ return Map <String, list> annotation name ref pro name */public static Map <String, List> getAnnRefAndPro (CtClass claName) {// define a map to mount all attributes and reference the annotated method Map <String, List> map = new HashMap <String, List> (); list <String> annpro = new ArrayList <String> (); List <String> annref = new RrayList <String> (); // get all methods CtMethod [] MS = claName. getMethods (); for (int I = 0; I <ms. length; I ++) {// obtain all method information MethodInfo methodInfo = MS [I]. getMethodInfo (); // obtain the annotation information AnnotationsAttribute atrr = (AnnotationsAttribute) methodInfo. getAttribute (AnnotationsAttribute. visibleTag); if (atrr! = Null) {if (atrr. toString (). equals (StringValueProvider. annotations_Value.ANNPRO) {if (MS [I]. getName (). startsWith ("set") {// retrieves the annpro variable from the method name. 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 ;}} test class 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); // obtain the Map variable <String, List> map = ReflectUtil through annotation. getAnnRefAndPro (cc); List inter = ReflectUtil. getRefIner (cc); for (int I = 0; I <inter. size (); I ++) {System. out. println ("interface:" + inter. get (I);} List annpro = map. get (StringValueProvider. annotations_Value.ANNPRO); for (int I = 0; I <annpro. size (); I ++) {System. out. println ("All set methods include @ org. osoa. sca. annotations. property annotation variable: "+ annpro. get (I);} List annref = map. get (StringValueProvider. annotations_Value.ANNREF); for (int I = 0; I <annpro. size (); I ++) {System. out. println ("All set methods include @ org. osoa. sca. annotations. reference annotation variable: "+ annpro. get (I) ;}} catch (Exception e) {e. printStackTrace ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.