Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new implementation of AnnotationProvider #46

Closed
mhgrove opened this issue Oct 15, 2010 · 1 comment
Closed

add new implementation of AnnotationProvider #46

mhgrove opened this issue Oct 15, 2010 · 1 comment
Labels

Comments

@mhgrove
Copy link
Owner

mhgrove commented Oct 15, 2010

From the mailing list, good suggestion for using the JDK instrumentation:

The implementation Ive built uses JDK instrumentation to find all loaded classes and inspect for annotations. It seems to work, and takes about 1sec to do its job.
i) The Instrument class - export as jar file with MANIFEST.mf like in ii)

import java.lang.instrument.*; 
public class Instrument { 
     public Instrument() {
        super();
        // TODO Auto-generated constructor stub
    }

    private static Instrumentation inst; 

     public static Instrumentation getInstrumentation() { return inst; } 

     public static void premain(String agentArgs, Instrumentation inst) { 
         System.out.println(inst.getClass() + ": " + inst); 
         Instrument.inst = inst; 
     } 
 } 

ii) the Manifest

Manifest-Version: 1.0
Implementation-Vendor: ArchiveLink
Implementation-Title: ArchiveLink Instrument
Implementation-Version: 0.0.1
Specification-Vendor: ArchiveLink
Name: net/archivelink/instrument/
Premain-Class: net.archivelink.instrument.Instrument
Specification-Title: ArchiveLink Instrument Class
Specification-Version: 0.0.1

iii) jvm option : -javaagent:c:\tmp\instrument.jar

iv) From the EmpireAnnotationProvider impl

@Override
    public Collection<Class<?>> getClassesWithAnnotation(Class<? extends Annotation> theAnnotation) {

        Set<Class<?>> aClasses = new HashSet<Class<?>>();
        Instrumentation inst = Instrument.getInstrumentation();


        //*** use -javaagent:pathToJarFile
        //depends on Instrument being loaded as an agent to the JVM
        //
        //http://www.coderanch.com/t/329407/java/java/find-all-loaded-classes-classloaders
        //http://download.oracle.com/javase/6/docs/api/java/lang/instrument/package-summary.html
        Class[] css = inst.getAllLoadedClasses();

        for ( Class aClass : css ) {
            try {
                if (aClass.isAnnotationPresent(theAnnotation)) {
                    aClasses.add(aClass);
                }
                else {
                    if ( LOGGER.isDebugEnabled() )
                    LOGGER.debug("Class " + aClass.getName() + "' " +
                                "does not actually have the specified annotation '" + theAnnotation + "'");
                }
            } catch (Exception e) {
                if ( LOGGER.isDebugEnabled() )
                LOGGER.debug("Exception during annotation inspection : " + e.toString() );

            }
        }
        return aClasses;
    }
@mhgrove
Copy link
Owner Author

mhgrove commented Mar 22, 2011

loading of empire configuration from a jar, closed by 62c70f4. also settling on the conventional name for the configuration file and format, noted in the docs on the wiki, we've deprecated trying a bunch of different file names and will remove this in a later version. also adding a new annotation provider based on empire being the java agent for the application and it will load the information about what classes are annotated with what based on what the JVM told the instrumentor on startup. This will override any annotation provider specified in the configuration when used. closed by 62c70f4

@mhgrove mhgrove closed this as completed Mar 22, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant