improve getAnnotation(String)

This commit is contained in:
Volker Berlin 2019-02-24 14:49:36 +01:00
parent e94d80dc7f
commit 9d1e4ef6f3
2 changed files with 31 additions and 39 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2017 Volker Berlin (i-net software) * Copyright 2017 - 2019 Volker Berlin (i-net software)
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,16 +25,19 @@ import java.util.Map;
*/ */
public class Annotations { public class Annotations {
private final Map<String,Map<String,Object>> annotations = new HashMap<>();
/** /**
* Read the annotations structure. * Read the annotations structure. http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.17
* http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.17 *
* @param input * @param input
* the stream of the RuntimeInvisibleAnnotations attribute
* @param constantPool * @param constantPool
* the ConstantPool of the class
* @throws IOException * @throws IOException
* if an I/O error occurs
* @return the map of the annotation names to its attributes
*/ */
public Annotations( DataInputStream input, ConstantPool constantPool ) throws IOException { static Map<String,Map<String,Object>> read( DataInputStream input, ConstantPool constantPool ) throws IOException {
Map<String,Map<String,Object>> annotations = new HashMap<>();
int count = input.readUnsignedShort(); int count = input.readUnsignedShort();
for( int i = 0; i < count; i++ ) { for( int i = 0; i < count; i++ ) {
String className = (String)constantPool.get( input.readUnsignedShort() ); String className = (String)constantPool.get( input.readUnsignedShort() );
@ -66,14 +69,6 @@ public class Annotations {
valuePairs.put( key, value ); valuePairs.put( key, value );
} }
} }
} return annotations;
/**
* Get the key values of the annotation for the given class.
* @param className the class name of the annotation
* @return a map with the properties of the annotation or null if there is no annotation.
*/
public Map<String, Object> get( String className ) {
return annotations.get( className );
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2011 - 2018 Volker Berlin (i-net software) Copyright 2011 - 2019 Volker Berlin (i-net software)
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -18,6 +18,7 @@ package de.inetsoftware.classparser;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Collections;
import java.util.Map; import java.util.Map;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -45,17 +46,20 @@ public class MethodInfo implements Member {
private ClassFile classFile; private ClassFile classFile;
private Annotations annotations; private Map<String,Map<String,Object>> annotations;
/** /**
* Read the method_info structure * Read the method_info structure http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.6
* http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.6
* http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html#1513 * http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html#1513
* *
* @param input * @param input
* the stream of the class file
* @param constantPool * @param constantPool
* @param classFile the declaring class file * the ConstantPool of the class
* @param classFile
* the declaring class file
* @throws IOException * @throws IOException
* if an I/O error occurs
*/ */
MethodInfo( DataInputStream input, ConstantPool constantPool, ClassFile classFile ) throws IOException { MethodInfo( DataInputStream input, ConstantPool constantPool, ClassFile classFile ) throws IOException {
this.accessFlags = input.readUnsignedShort(); this.accessFlags = input.readUnsignedShort();
@ -141,6 +145,10 @@ public class MethodInfo implements Member {
/** /**
* Get the signature of the method with generic types. * Get the signature of the method with generic types.
*
* @return the signature
* @throws IOException
* if an I/O error occurs
*/ */
public String getSignature() throws IOException { public String getSignature() throws IOException {
AttributeInfo info = getAttributes().get( "Signature" ); AttributeInfo info = getAttributes().get( "Signature" );
@ -163,21 +171,6 @@ public class MethodInfo implements Member {
return exceptions; return exceptions;
} }
/**
* Get the annotations with @Retention(RetentionPolicy.CLASS)
* @return the annotations if there any exists else null
*/
@Nullable
public Annotations getRuntimeInvisibleAnnotations() throws IOException {
if( annotations == null ) {
AttributeInfo data = attributes.get( "RuntimeInvisibleAnnotations" );
if( data != null ) {
annotations = new Annotations( data.getDataInputStream(), constantPool );
}
}
return annotations;
}
/** /**
* Get a single annotation or null * Get a single annotation or null
* *
@ -189,11 +182,15 @@ public class MethodInfo implements Member {
*/ */
@Nullable @Nullable
public Map<String, Object> getAnnotation( String annotation ) throws IOException { public Map<String, Object> getAnnotation( String annotation ) throws IOException {
Annotations annotations = getRuntimeInvisibleAnnotations(); if( annotations == null ) {
if( annotations != null ) { AttributeInfo data = attributes.get( "RuntimeInvisibleAnnotations" );
return annotations.get( annotation ); if( data != null ) {
annotations = Annotations.read( data.getDataInputStream(), constantPool );
} else {
annotations = Collections.emptyMap();
}
} }
return null; return annotations.get( annotation );
} }
/** /**