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");
* you may not use this file except in compliance with the License.
@ -25,16 +25,19 @@ import java.util.Map;
*/
public class Annotations {
private final Map<String,Map<String,Object>> annotations = new HashMap<>();
/**
* Read the annotations structure.
* http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.17
* Read the annotations structure. http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.17
*
* @param input
* the stream of the RuntimeInvisibleAnnotations attribute
* @param constantPool
* the ConstantPool of the class
* @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();
for( int i = 0; i < count; i++ ) {
String className = (String)constantPool.get( input.readUnsignedShort() );
@ -66,14 +69,6 @@ public class Annotations {
valuePairs.put( key, value );
}
}
}
/**
* 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 );
return annotations;
}
}

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");
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.IOException;
import java.util.Collections;
import java.util.Map;
import javax.annotation.Nullable;
@ -45,17 +46,20 @@ public class MethodInfo implements Member {
private ClassFile classFile;
private Annotations annotations;
private Map<String,Map<String,Object>> annotations;
/**
* Read the method_info structure
* http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.6
* 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/se5.0/html/ClassFile.doc.html#1513
*
* @param input
* the stream of the class file
* @param constantPool
* @param classFile the declaring class file
* the ConstantPool of the class
* @param classFile
* the declaring class file
* @throws IOException
* if an I/O error occurs
*/
MethodInfo( DataInputStream input, ConstantPool constantPool, ClassFile classFile ) throws IOException {
this.accessFlags = input.readUnsignedShort();
@ -141,6 +145,10 @@ public class MethodInfo implements Member {
/**
* 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 {
AttributeInfo info = getAttributes().get( "Signature" );
@ -163,21 +171,6 @@ public class MethodInfo implements Member {
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
*
@ -189,11 +182,15 @@ public class MethodInfo implements Member {
*/
@Nullable
public Map<String, Object> getAnnotation( String annotation ) throws IOException {
Annotations annotations = getRuntimeInvisibleAnnotations();
if( annotations != null ) {
return annotations.get( annotation );
if( annotations == null ) {
AttributeInfo data = attributes.get( "RuntimeInvisibleAnnotations" );
if( data != null ) {
annotations = Annotations.read( data.getDataInputStream(), constantPool );
} else {
annotations = Collections.emptyMap();
}
}
return null;
return annotations.get( annotation );
}
/**