Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Why can't my code see annotations in a class file?

870997Aug 30 2011 — edited Sep 7 2011
So I have a JAR file with some classes in it. I have used DJ Java Decompiler to inspect the class in question and I can definitely see the annotations in it.

I'm working on a Maven MOJO that reads the JAR files to find the annotations, but for some reason my code does not find any annotations at all. Does anyone have any ideas what the problem might be?

The StreamLoader seems to be working because when I load the classes, their names get logged properly. Could there be something in the StreamLoader that is stripping off the annotations?

Cheers, Eric
try
{
    JarFile jarFile = new JarFile(artifact.getFile());
    Enumeration<JarEntry> jarEntries = jarFile.entries();
    while (jarEntries.hasMoreElements())
    {
        JarEntry jarEntry = jarEntries.nextElement();
    	if (!jarEntry.isDirectory() && jarEntry.getName().endsWith(".class"))
    	{
            getLog().info("jarEntry.getName() = " + jarEntry.getName());

            try
	    {
	        Class<?> entry = streamLoader.loadClass(jarFile.getInputStream(jarEntry));
	        getLog().info("entry " + entry.getName());
	        if (entry.isAnnotationPresent(DotNet.class))
	        {
	            getLog().info("including " + entry.getName());
	        }
	        DotNet dotNet = entry.getAnnotation(net.sf.jni4net.annotations.DotNet.class);
	        if (dotNet == null)
	        {
	            Annotation [] annotations = entry.getAnnotations();
	            for (Annotation annotation : annotations)
	            {
	                getLog().info("annotation " + annotation.toString());
	            }
	        }
	        else
	        {
	            getLog().info("including " + entry.getName());
	        }
            }
	    catch (Throwable t)
	    {
	        getLog().warn("can't find class " + jarEntry.getName());
	    }
    	}
    }
}
catch (IOException e)
{
    getLog().error(e.getMessage(), e);
}
class StreamLoader extends ClassLoader
{
    public StreamLoader(ClassLoader parent)
    {
        super(parent);
    }

    public Class<?> loadClass(InputStream input) throws ClassNotFoundException
    {
        try
        {
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            int data = input.read();

            while(data != -1)
            {
                buffer.write(data);
                data = input.read();
            }

            input.close();

            byte[] classData = buffer.toByteArray();

            return defineClass(null, classData, 0, classData.length);
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace(); 
        }

        return null;
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 5 2011
Added on Aug 30 2011
6 comments
2,335 views