Add parser for annotations

This commit is contained in:
Volker Berlin 2017-03-31 20:40:49 +02:00
parent ec1fc643c2
commit ec93b799b9
2 changed files with 231 additions and 138 deletions

View File

@ -0,0 +1,79 @@
/*
* Copyright 2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.inetsoftware.classparser;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @author Volker Berlin
*/
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
* @param input
* @param constantPool
* @throws IOException
*/
public Annotations( DataInputStream input, ConstantPool constantPool ) throws IOException {
int count = input.readUnsignedShort();
for( int i = 0; i < count; i++ ) {
String className = (String)constantPool.get( input.readUnsignedShort() );
className = className.substring( 1, className.length() - 1 ).replace( '/', '.' ); // has the form: "Lcom/package/ClassName;"
Map<String,Object> valuePairs = new HashMap<>();
annotations.put( className, valuePairs );
int valuePairCount = input.readUnsignedShort();
for( int p = 0; p < valuePairCount; p++ ) {
String key = (String)constantPool.get( input.readUnsignedShort() );
int type = input.readUnsignedByte();
Object value;
switch( type ) {
case 'B':
case 'C':
case 'D':
case 'F':
case 'I':
case 'J':
case 'S':
case 'Z':
case 's':
value = constantPool.get( input.readUnsignedShort() );
break;
default:
// TODO other possible values for type: e c @ [
throw new IOException( "Unknown annotation value type pool type: " + type );
}
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 );
}
}

View File

@ -1,138 +1,152 @@
/* /*
Copyright 2011 - 2017 Volker Berlin (i-net software) Copyright 2011 - 2017 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.
You may obtain a copy of the License at You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package de.inetsoftware.classparser; package de.inetsoftware.classparser;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.Retention;
import de.inetsoftware.classparser.Attributes.AttributeInfo; import java.lang.annotation.RetentionPolicy;
/** import de.inetsoftware.classparser.Attributes.AttributeInfo;
* @author Volker Berlin
*/ /**
public class MethodInfo { * @author Volker Berlin
*/
private final int accessFlags; public class MethodInfo {
private final String name; private final int accessFlags;
private final String description; private final String name;
private final Attributes attributes; private final String description;
private final ConstantPool constantPool; private final Attributes attributes;
private Code code; private final ConstantPool constantPool;
private Exceptions exceptions; private Code code;
/** private Exceptions exceptions;
* 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 * Read the method_info structure
* * http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.6
* @param input * http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html#1513
* @param constantPool *
* @throws IOException * @param input
*/ * @param constantPool
MethodInfo( DataInputStream input, ConstantPool constantPool ) throws IOException { * @throws IOException
this.accessFlags = input.readUnsignedShort(); */
this.name = (String)constantPool.get( input.readUnsignedShort() ); MethodInfo( DataInputStream input, ConstantPool constantPool ) throws IOException {
this.description = (String)constantPool.get( input.readUnsignedShort() ); this.accessFlags = input.readUnsignedShort();
this.constantPool = constantPool; this.name = (String)constantPool.get( input.readUnsignedShort() );
this.attributes = new Attributes( input, constantPool ); this.description = (String)constantPool.get( input.readUnsignedShort() );
} this.constantPool = constantPool;
this.attributes = new Attributes( input, constantPool );
/** }
* Get the access flags of the method.
* http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.6-200-A /**
* http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html#1522 * Get the access flags of the method.
* * http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.6-200-A
* @return the flags * http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html#1522
*/ *
public int getAccessFlags() { * @return the flags
return accessFlags; */
} public int getAccessFlags() {
return accessFlags;
/** }
* If the method is a static method.
* http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.6-200-A /**
* http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html#1522 * If the method is a static method.
* @return true, if static * http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.6-200-A
* @see #getAccessFlags() * http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html#1522
*/ * @return true, if static
public boolean isStatic() { * @see #getAccessFlags()
return (accessFlags & 0x0008) > 0; */
} public boolean isStatic() {
return (accessFlags & 0x0008) > 0;
/** }
* @return the name
*/ /**
public String getName() { * @return the name
return name; */
} public String getName() {
return name;
/** }
* @return the attributes
*/ /**
public Attributes getAttributes() { * @return the attributes
return attributes; */
} public Attributes getAttributes() {
return attributes;
public Code getCode() throws IOException { }
if( code != null ){
return code; public Code getCode() throws IOException {
} if( code != null ){
AttributeInfo data = attributes.get( "Code" ); return code;
if( data != null ) { }
code = new Code( data.getDataInputStream(), constantPool ); AttributeInfo data = attributes.get( "Code" );
} if( data != null ) {
return code; code = new Code( data.getDataInputStream(), constantPool );
} }
return code;
}
/**
* Get the signature of the method without generic types.
*/ /**
public String getDescription() { * Get the signature of the method without generic types.
return description; */
} public String getDescription() {
return description;
/** }
* Get the signature of the method with generic types.
*/ /**
public String getSignature() throws IOException { * Get the signature of the method with generic types.
AttributeInfo info = getAttributes().get( "Signature" ); */
if( info != null ) { public String getSignature() throws IOException {
int idx = info.getDataInputStream().readShort(); AttributeInfo info = getAttributes().get( "Signature" );
return (String)constantPool.get( idx ); if( info != null ) {
} else { int idx = info.getDataInputStream().readShort();
return description; return (String)constantPool.get( idx );
} } else {
} return description;
}
public Exceptions getExceptions() throws IOException { }
if( exceptions != null ) {
return exceptions; public Exceptions getExceptions() throws IOException {
} if( exceptions != null ) {
AttributeInfo data = attributes.get( "Exceptions" ); return exceptions;
if( data != null ) { }
exceptions = new Exceptions( data.getDataInputStream(), constantPool ); AttributeInfo data = attributes.get( "Exceptions" );
} if( data != null ) {
return exceptions; exceptions = new Exceptions( data.getDataInputStream(), constantPool );
} }
} return exceptions;
}
/**
* Get the annotations with @Retention(RetentionPolicy.CLASS)
* @return the annotations if there any exists else null
*/
public Annotations getRuntimeInvisibleAnnotations() throws IOException {
AttributeInfo data = attributes.get( "RuntimeInvisibleAnnotations" );
if( data != null ) {
return new Annotations( data.getDataInputStream(), constantPool );
}
return null;
}
}