mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-15 02:44:47 +01:00
Add parser for annotations
This commit is contained in:
parent
ec1fc643c2
commit
ec93b799b9
79
src/de/inetsoftware/classparser/Annotations.java
Normal file
79
src/de/inetsoftware/classparser/Annotations.java
Normal 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 );
|
||||
}
|
||||
}
|
@ -1,138 +1,152 @@
|
||||
/*
|
||||
Copyright 2011 - 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 de.inetsoftware.classparser.Attributes.AttributeInfo;
|
||||
|
||||
/**
|
||||
* @author Volker Berlin
|
||||
*/
|
||||
public class MethodInfo {
|
||||
|
||||
private final int accessFlags;
|
||||
|
||||
private final String name;
|
||||
|
||||
private final String description;
|
||||
|
||||
private final Attributes attributes;
|
||||
|
||||
private final ConstantPool constantPool;
|
||||
|
||||
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
|
||||
*
|
||||
* @param input
|
||||
* @param constantPool
|
||||
* @throws IOException
|
||||
*/
|
||||
MethodInfo( DataInputStream input, ConstantPool constantPool ) throws IOException {
|
||||
this.accessFlags = input.readUnsignedShort();
|
||||
this.name = (String)constantPool.get( input.readUnsignedShort() );
|
||||
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
|
||||
*
|
||||
* @return the flags
|
||||
*/
|
||||
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
|
||||
* @return true, if static
|
||||
* @see #getAccessFlags()
|
||||
*/
|
||||
public boolean isStatic() {
|
||||
return (accessFlags & 0x0008) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the attributes
|
||||
*/
|
||||
public Attributes getAttributes() {
|
||||
return attributes;
|
||||
}
|
||||
|
||||
public Code getCode() throws IOException {
|
||||
if( code != null ){
|
||||
return code;
|
||||
}
|
||||
AttributeInfo data = attributes.get( "Code" );
|
||||
if( data != null ) {
|
||||
code = new Code( data.getDataInputStream(), constantPool );
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the signature of the method without generic types.
|
||||
*/
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the signature of the method with generic types.
|
||||
*/
|
||||
public String getSignature() throws IOException {
|
||||
AttributeInfo info = getAttributes().get( "Signature" );
|
||||
if( info != null ) {
|
||||
int idx = info.getDataInputStream().readShort();
|
||||
return (String)constantPool.get( idx );
|
||||
} else {
|
||||
return description;
|
||||
}
|
||||
}
|
||||
|
||||
public Exceptions getExceptions() throws IOException {
|
||||
if( exceptions != null ) {
|
||||
return exceptions;
|
||||
}
|
||||
AttributeInfo data = attributes.get( "Exceptions" );
|
||||
if( data != null ) {
|
||||
exceptions = new Exceptions( data.getDataInputStream(), constantPool );
|
||||
}
|
||||
return exceptions;
|
||||
}
|
||||
}
|
||||
/*
|
||||
Copyright 2011 - 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.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
import de.inetsoftware.classparser.Attributes.AttributeInfo;
|
||||
|
||||
/**
|
||||
* @author Volker Berlin
|
||||
*/
|
||||
public class MethodInfo {
|
||||
|
||||
private final int accessFlags;
|
||||
|
||||
private final String name;
|
||||
|
||||
private final String description;
|
||||
|
||||
private final Attributes attributes;
|
||||
|
||||
private final ConstantPool constantPool;
|
||||
|
||||
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
|
||||
*
|
||||
* @param input
|
||||
* @param constantPool
|
||||
* @throws IOException
|
||||
*/
|
||||
MethodInfo( DataInputStream input, ConstantPool constantPool ) throws IOException {
|
||||
this.accessFlags = input.readUnsignedShort();
|
||||
this.name = (String)constantPool.get( input.readUnsignedShort() );
|
||||
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
|
||||
*
|
||||
* @return the flags
|
||||
*/
|
||||
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
|
||||
* @return true, if static
|
||||
* @see #getAccessFlags()
|
||||
*/
|
||||
public boolean isStatic() {
|
||||
return (accessFlags & 0x0008) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the attributes
|
||||
*/
|
||||
public Attributes getAttributes() {
|
||||
return attributes;
|
||||
}
|
||||
|
||||
public Code getCode() throws IOException {
|
||||
if( code != null ){
|
||||
return code;
|
||||
}
|
||||
AttributeInfo data = attributes.get( "Code" );
|
||||
if( data != null ) {
|
||||
code = new Code( data.getDataInputStream(), constantPool );
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the signature of the method without generic types.
|
||||
*/
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the signature of the method with generic types.
|
||||
*/
|
||||
public String getSignature() throws IOException {
|
||||
AttributeInfo info = getAttributes().get( "Signature" );
|
||||
if( info != null ) {
|
||||
int idx = info.getDataInputStream().readShort();
|
||||
return (String)constantPool.get( idx );
|
||||
} else {
|
||||
return description;
|
||||
}
|
||||
}
|
||||
|
||||
public Exceptions getExceptions() throws IOException {
|
||||
if( exceptions != null ) {
|
||||
return exceptions;
|
||||
}
|
||||
AttributeInfo data = attributes.get( "Exceptions" );
|
||||
if( data != null ) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user