This commit is contained in:
2024-11-30 19:03:49 +08:00
commit 1e6763c160
3806 changed files with 737676 additions and 0 deletions

View File

@@ -0,0 +1,190 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.util.bcel;
/**
* Constants for the project, mostly defined in the JVM specification.
*/
public final class Const {
/** One of the access flags for fields, methods, or classes.
* @see <a href="http://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html#jvms-4.1-200-E.1">
* Flag definitions for Classes in the Java Virtual Machine Specification (Java SE 9 Edition).</a>
* @see <a href="http://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html#jvms-4.5">
* Flag definitions for Fields in the Java Virtual Machine Specification (Java SE 9 Edition).</a>
* @see <a href="http://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html#jvms-4.6">
* Flag definitions for Methods in the Java Virtual Machine Specification (Java SE 9 Edition).</a>
* @see <a href="http://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html#jvms-4.7.6-300-D.1-D.1">
* Flag definitions for Inner Classes in the Java Virtual Machine Specification (Java SE 9 Edition).</a>
*/
public static final short ACC_FINAL = 0x0010;
/** One of the access flags for fields, methods, or classes.
*/
public static final short ACC_INTERFACE = 0x0200;
/** One of the access flags for fields, methods, or classes.
*/
public static final short ACC_ABSTRACT = 0x0400;
/** One of the access flags for fields, methods, or classes.
*/
public static final short ACC_ANNOTATION = 0x2000;
/**
* Marks a constant pool entry as type UTF-8.
* @see <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.7">
* The Constant Pool in The Java Virtual Machine Specification</a>
*/
public static final byte CONSTANT_Utf8 = 1;
/**
* Marks a constant pool entry as type Integer.
* @see <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.4">
* The Constant Pool in The Java Virtual Machine Specification</a>
*/
public static final byte CONSTANT_Integer = 3;
/**
* Marks a constant pool entry as type Float.
* @see <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.4">
* The Constant Pool in The Java Virtual Machine Specification</a>
*/
public static final byte CONSTANT_Float = 4;
/**
* Marks a constant pool entry as type Long.
* @see <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.5">
* The Constant Pool in The Java Virtual Machine Specification</a>
*/
public static final byte CONSTANT_Long = 5;
/**
* Marks a constant pool entry as type Double.
* @see <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.5">
* The Constant Pool in The Java Virtual Machine Specification</a>
*/
public static final byte CONSTANT_Double = 6;
/**
* Marks a constant pool entry as a Class
* @see <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.1">
* The Constant Pool in The Java Virtual Machine Specification</a>
*/
public static final byte CONSTANT_Class = 7;
/**
* Marks a constant pool entry as type String
* @see <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.3">
* The Constant Pool in The Java Virtual Machine Specification</a>
*/
public static final byte CONSTANT_String = 8;
/**
* Marks a constant pool entry as a Field Reference.
* @see <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.2">
* The Constant Pool in The Java Virtual Machine Specification</a>
*/
public static final byte CONSTANT_Fieldref = 9;
/**
* Marks a constant pool entry as a Method Reference.
* @see <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.2">
* The Constant Pool in The Java Virtual Machine Specification</a>
*/
public static final byte CONSTANT_Methodref = 10;
/**
* Marks a constant pool entry as an Interface Method Reference.
* @see <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.2">
* The Constant Pool in The Java Virtual Machine Specification</a>
*/
public static final byte CONSTANT_InterfaceMethodref = 11;
/**
* Marks a constant pool entry as a name and type.
* @see <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.6">
* The Constant Pool in The Java Virtual Machine Specification</a>
*/
public static final byte CONSTANT_NameAndType = 12;
/**
* Marks a constant pool entry as a Method Handle.
* @see <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.8">
* The Constant Pool in The Java Virtual Machine Specification</a>
*/
public static final byte CONSTANT_MethodHandle = 15;
/**
* Marks a constant pool entry as a Method Type.
* @see <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.9">
* The Constant Pool in The Java Virtual Machine Specification</a>
*/
public static final byte CONSTANT_MethodType = 16;
/**
* Marks a constant pool entry as dynamically computed.
* @see <a href="https://bugs.openjdk.java.net/secure/attachment/74618/constant-dynamic.html">
* Change request for JEP 309</a>
*/
public static final byte CONSTANT_Dynamic = 17;
/**
* Marks a constant pool entry as an Invoke Dynamic
* @see <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.10">
* The Constant Pool in The Java Virtual Machine Specification</a>
*/
public static final byte CONSTANT_InvokeDynamic = 18;
/**
* Marks a constant pool entry as a Module Reference.
*
* <p>Note: Early access Java 9 support- currently subject to change</p>
*
* @see <a href="http://cr.openjdk.java.net/~mr/jigsaw/spec/lang-vm.html#jigsaw-2.6">
* JPMS: Modules in the Java Language and JVM</a>
*/
public static final byte CONSTANT_Module = 19;
/**
* Marks a constant pool entry as a Package Reference.
*
* <p>Note: Early access Java 9 support- currently subject to change</p>
*
* @see <a href="http://cr.openjdk.java.net/~mr/jigsaw/spec/lang-vm.html#jigsaw-2.6">
* JPMS: Modules in the Java Language and JVM</a>
*/
public static final byte CONSTANT_Package = 20;
/**
* The names of the types of entries in a constant pool.
* Use getConstantName instead
*/
private static final String[] CONSTANT_NAMES = {
"", "CONSTANT_Utf8", "", "CONSTANT_Integer",
"CONSTANT_Float", "CONSTANT_Long", "CONSTANT_Double",
"CONSTANT_Class", "CONSTANT_String", "CONSTANT_Fieldref",
"CONSTANT_Methodref", "CONSTANT_InterfaceMethodref",
"CONSTANT_NameAndType", "", "", "CONSTANT_MethodHandle",
"CONSTANT_MethodType", "CONSTANT_Dynamic", "CONSTANT_InvokeDynamic",
"CONSTANT_Module", "CONSTANT_Package"};
public static String getConstantName(int index) {
return CONSTANT_NAMES[index];
}
}

View File

@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.util.bcel.classfile;
public class AnnotationElementValue extends ElementValue
{
// For annotation element values, this is the annotation
private final AnnotationEntry annotationEntry;
AnnotationElementValue(final int type, final AnnotationEntry annotationEntry,
final ConstantPool cpool)
{
super(type, cpool);
if (type != ANNOTATION) {
throw new RuntimeException(
"Only element values of type annotation can be built with this ctor - type specified: " + type);
}
this.annotationEntry = annotationEntry;
}
@Override
public String stringifyValue()
{
return annotationEntry.toString();
}
public AnnotationEntry getAnnotationEntry()
{
return annotationEntry;
}
}

View File

@@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.util.bcel.classfile;
import java.io.DataInput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.tomcat.util.bcel.Const;
/**
* represents one annotation in the annotation table
*/
public class AnnotationEntry {
private final int type_index;
private final ConstantPool constant_pool;
private final List<ElementValuePair> element_value_pairs;
/*
* Creates an AnnotationEntry from a DataInputStream
*
* @param input
* @param constant_pool
* @throws IOException
*/
AnnotationEntry(final DataInput input, final ConstantPool constant_pool) throws IOException {
this.constant_pool = constant_pool;
type_index = input.readUnsignedShort();
final int num_element_value_pairs = input.readUnsignedShort();
element_value_pairs = new ArrayList<>(num_element_value_pairs);
for (int i = 0; i < num_element_value_pairs; i++) {
element_value_pairs.add(new ElementValuePair(input, constant_pool));
}
}
/**
* @return the annotation type name
*/
public String getAnnotationType() {
final ConstantUtf8 c = (ConstantUtf8) constant_pool.getConstant(type_index, Const.CONSTANT_Utf8);
return c.getBytes();
}
/**
* @return the element value pairs in this annotation entry
*/
public List<ElementValuePair> getElementValuePairs() {
return element_value_pairs;
}
}

View File

@@ -0,0 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.util.bcel.classfile;
import java.io.DataInput;
import java.io.IOException;
/**
* base class for annotations
*/
public class Annotations {
private final AnnotationEntry[] annotation_table;
/**
* @param input Input stream
* @param constant_pool Array of constants
*/
Annotations(final DataInput input, final ConstantPool constant_pool) throws IOException {
final int annotation_table_length = input.readUnsignedShort();
annotation_table = new AnnotationEntry[annotation_table_length];
for (int i = 0; i < annotation_table_length; i++) {
annotation_table[i] = new AnnotationEntry(input, constant_pool);
}
}
/**
* @return the array of annotation entries in this annotation
*/
public AnnotationEntry[] getAnnotationEntries() {
return annotation_table;
}
}

View File

@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.util.bcel.classfile;
public class ArrayElementValue extends ElementValue
{
// For array types, this is the array
private final ElementValue[] evalues;
ArrayElementValue(final int type, final ElementValue[] datums, final ConstantPool cpool)
{
super(type, cpool);
if (type != ARRAY) {
throw new RuntimeException(
"Only element values of type array can be built with this ctor - type specified: " + type);
}
this.evalues = datums;
}
@Override
public String stringifyValue()
{
final StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < evalues.length; i++)
{
sb.append(evalues[i].stringifyValue());
if ((i + 1) < evalues.length) {
sb.append(",");
}
}
sb.append("]");
return sb.toString();
}
public ElementValue[] getElementValuesArray()
{
return evalues;
}
}

View File

@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.util.bcel.classfile;
import org.apache.tomcat.util.bcel.Const;
public class ClassElementValue extends ElementValue
{
// For primitive types and string type, this points to the value entry in
// the cpool
// For 'class' this points to the class entry in the cpool
private final int idx;
ClassElementValue(final int type, final int idx, final ConstantPool cpool) {
super(type, cpool);
this.idx = idx;
}
@Override
public String stringifyValue()
{
final ConstantUtf8 cu8 = (ConstantUtf8) super.getConstantPool().getConstant(idx,
Const.CONSTANT_Utf8);
return cu8.getBytes();
}
}

View File

@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.util.bcel.classfile;
/**
* Thrown when the BCEL attempts to read a class file and determines
* that the file is malformed or otherwise cannot be interpreted as a
* class file.
*/
public class ClassFormatException extends RuntimeException {
private static final long serialVersionUID = 3243149520175287759L;
public ClassFormatException() {
super();
}
public ClassFormatException(final String s) {
super(s);
}
}

View File

@@ -0,0 +1,247 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.util.bcel.classfile;
import java.io.BufferedInputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.tomcat.util.bcel.Const;
/**
* Wrapper class that parses a given Java .class file. The method <A
* href ="#parse">parse</A> returns a <A href ="JavaClass.html">
* JavaClass</A> object on success. When an I/O error or an
* inconsistency occurs an appropriate exception is propagated back to
* the caller.
*
* The structure and the names comply, except for a few conveniences,
* exactly with the <A href="http://docs.oracle.com/javase/specs/">
* JVM specification 1.0</a>. See this paper for
* further details about the structure of a bytecode file.
*/
public final class ClassParser {
private static final int MAGIC = 0xCAFEBABE;
private final DataInput dataInputStream;
private String class_name, superclass_name;
private int access_flags; // Access rights of parsed class
private String[] interface_names; // Names of implemented interfaces
private ConstantPool constant_pool; // collection of constants
private Annotations runtimeVisibleAnnotations; // "RuntimeVisibleAnnotations" attribute defined in the class
private static final int BUFSIZE = 8192;
private static final String[] INTERFACES_EMPTY_ARRAY = new String[0];
/**
* Parses class from the given stream.
*
* @param inputStream Input stream
*/
public ClassParser(final InputStream inputStream) {
this.dataInputStream = new DataInputStream(new BufferedInputStream(inputStream, BUFSIZE));
}
/**
* Parses the given Java class file and return an object that represents
* the contained data, i.e., constants, methods, fields and commands.
* A <em>ClassFormatException</em> is raised, if the file is not a valid
* .class file. (This does not include verification of the byte code as it
* is performed by the java interpreter).
*
* @return Class object representing the parsed class file
* @throws IOException If an I/O occurs reading the byte code
* @throws ClassFormatException If the byte code is invalid
*/
public JavaClass parse() throws IOException, ClassFormatException {
/****************** Read headers ********************************/
// Check magic tag of class file
readID();
// Get compiler version
readVersion();
/****************** Read constant pool and related **************/
// Read constant pool entries
readConstantPool();
// Get class information
readClassInfo();
// Get interface information, i.e., implemented interfaces
readInterfaces();
/****************** Read class fields and methods ***************/
// Read class fields, i.e., the variables of the class
readFields();
// Read class methods, i.e., the functions in the class
readMethods();
// Read class attributes
readAttributes();
// Return the information we have gathered in a new object
return new JavaClass(class_name, superclass_name,
access_flags, constant_pool, interface_names,
runtimeVisibleAnnotations);
}
/**
* Reads information about the attributes of the class.
* @throws IOException
* @throws ClassFormatException
*/
private void readAttributes() throws IOException, ClassFormatException {
final int attributes_count = dataInputStream.readUnsignedShort();
for (int i = 0; i < attributes_count; i++) {
ConstantUtf8 c;
String name;
int name_index;
int length;
// Get class name from constant pool via `name_index' indirection
name_index = dataInputStream.readUnsignedShort();
c = (ConstantUtf8) constant_pool.getConstant(name_index,
Const.CONSTANT_Utf8);
name = c.getBytes();
// Length of data in bytes
length = dataInputStream.readInt();
if (name.equals("RuntimeVisibleAnnotations")) {
if (runtimeVisibleAnnotations != null) {
throw new ClassFormatException(
"RuntimeVisibleAnnotations attribute is not allowed more than once in a class file");
}
runtimeVisibleAnnotations = new Annotations(dataInputStream, constant_pool);
} else {
// All other attributes are skipped
Utility.skipFully(dataInputStream, length);
}
}
}
/**
* Reads information about the class and its super class.
* @throws IOException
* @throws ClassFormatException
*/
private void readClassInfo() throws IOException, ClassFormatException {
access_flags = dataInputStream.readUnsignedShort();
/* Interfaces are implicitly abstract, the flag should be set
* according to the JVM specification.
*/
if ((access_flags & Const.ACC_INTERFACE) != 0) {
access_flags |= Const.ACC_ABSTRACT;
}
if (((access_flags & Const.ACC_ABSTRACT) != 0)
&& ((access_flags & Const.ACC_FINAL) != 0)) {
throw new ClassFormatException("Class can't be both final and abstract");
}
int class_name_index = dataInputStream.readUnsignedShort();
class_name = Utility.getClassName(constant_pool, class_name_index);
int superclass_name_index = dataInputStream.readUnsignedShort();
if (superclass_name_index > 0) {
// May be zero -> class is java.lang.Object
superclass_name = Utility.getClassName(constant_pool, superclass_name_index);
} else {
superclass_name = "java.lang.Object";
}
}
/**
* Reads constant pool entries.
* @throws IOException
* @throws ClassFormatException
*/
private void readConstantPool() throws IOException, ClassFormatException {
constant_pool = new ConstantPool(dataInputStream);
}
/**
* Reads information about the fields of the class, i.e., its variables.
* @throws IOException
* @throws ClassFormatException
*/
private void readFields() throws IOException, ClassFormatException {
final int fields_count = dataInputStream.readUnsignedShort();
for (int i = 0; i < fields_count; i++) {
Utility.swallowFieldOrMethod(dataInputStream);
}
}
/******************** Private utility methods **********************/
/**
* Checks whether the header of the file is ok.
* Of course, this has to be the first action on successive file reads.
* @throws IOException
* @throws ClassFormatException
*/
private void readID() throws IOException, ClassFormatException {
if (dataInputStream.readInt() != MAGIC) {
throw new ClassFormatException("It is not a Java .class file");
}
}
/**
* Reads information about the interfaces implemented by this class.
* @throws IOException
* @throws ClassFormatException
*/
private void readInterfaces() throws IOException, ClassFormatException {
final int interfaces_count = dataInputStream.readUnsignedShort();
if (interfaces_count > 0) {
interface_names = new String[interfaces_count];
for (int i = 0; i < interfaces_count; i++) {
int index = dataInputStream.readUnsignedShort();
interface_names[i] = Utility.getClassName(constant_pool, index);
}
} else {
interface_names = INTERFACES_EMPTY_ARRAY;
}
}
/**
* Reads information about the methods of the class.
* @throws IOException
* @throws ClassFormatException
*/
private void readMethods() throws IOException, ClassFormatException {
final int methods_count = dataInputStream.readUnsignedShort();
for (int i = 0; i < methods_count; i++) {
Utility.swallowFieldOrMethod(dataInputStream);
}
}
/**
* Reads major and minor version of compiler which created the file.
* @throws IOException
* @throws ClassFormatException
*/
private void readVersion() throws IOException, ClassFormatException {
// file.readUnsignedShort(); // Unused minor
// file.readUnsignedShort(); // Unused major
Utility.skipFully(dataInputStream, 4);
}
}

View File

@@ -0,0 +1,108 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.util.bcel.classfile;
import java.io.DataInput;
import java.io.IOException;
import org.apache.tomcat.util.bcel.Const;
/**
* Abstract superclass for classes to represent the different constant types
* in the constant pool of a class file. The classes keep closely to
* the JVM specification.
*
* @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
*/
public abstract class Constant {
/* In fact this tag is redundant since we can distinguish different
* `Constant' objects by their type, i.e., via `instanceof'. In some
* places we will use the tag for switch()es anyway.
*
* First, we want match the specification as closely as possible. Second we
* need the tag as an index to select the corresponding class name from the
* `CONSTANT_NAMES' array.
*/
protected final byte tag;
Constant(final byte tag) {
this.tag = tag;
}
/**
* @return Tag of constant, i.e., its type. No setTag() method to avoid
* confusion.
*/
public final byte getTag() {
return tag;
}
/**
* Read one constant from the given input, the type depends on a tag byte.
*
* @param dataInput Input stream
* @return Constant object
* @throws IOException if an I/O error occurs reading from the given {@code dataInput}.
* @throws ClassFormatException if the next byte is not recognized
*/
static Constant readConstant(final DataInput dataInput) throws IOException, ClassFormatException {
final byte b = dataInput.readByte(); // Read tag byte
int skipSize;
switch (b) {
case Const.CONSTANT_Class:
return new ConstantClass(dataInput);
case Const.CONSTANT_Integer:
return new ConstantInteger(dataInput);
case Const.CONSTANT_Float:
return new ConstantFloat(dataInput);
case Const.CONSTANT_Long:
return new ConstantLong(dataInput);
case Const.CONSTANT_Double:
return new ConstantDouble(dataInput);
case Const.CONSTANT_Utf8:
return ConstantUtf8.getInstance(dataInput);
case Const.CONSTANT_String:
case Const.CONSTANT_MethodType:
case Const.CONSTANT_Module:
case Const.CONSTANT_Package:
skipSize = 2; // unsigned short
break;
case Const.CONSTANT_MethodHandle:
skipSize = 3; // unsigned byte, unsigned short
break;
case Const.CONSTANT_Fieldref:
case Const.CONSTANT_Methodref:
case Const.CONSTANT_InterfaceMethodref:
case Const.CONSTANT_NameAndType:
case Const.CONSTANT_Dynamic:
case Const.CONSTANT_InvokeDynamic:
skipSize = 4; // unsigned short, unsigned short
break;
default:
throw new ClassFormatException("Invalid byte tag in constant pool: " + b);
}
Utility.skipFully(dataInput, skipSize);
return null;
}
@Override
public String toString() {
return "[" + tag + "]";
}
}

View File

@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.util.bcel.classfile;
import java.io.DataInput;
import java.io.IOException;
import org.apache.tomcat.util.bcel.Const;
/**
* This class is derived from the abstract {@link Constant}
* and represents a reference to a (external) class.
*
* @see Constant
*/
public final class ConstantClass extends Constant {
private final int name_index; // Identical to ConstantString except for the name
/**
* Constructs an instance from file data.
*
* @param dataInput Input stream
* @throws IOException if an I/O error occurs reading from the given {@code dataInput}.
*/
ConstantClass(final DataInput dataInput) throws IOException {
super(Const.CONSTANT_Class);
this.name_index = dataInput.readUnsignedShort();
}
/**
* @return Name index in constant pool of class name.
*/
public int getNameIndex() {
return name_index;
}
}

View File

@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.util.bcel.classfile;
import java.io.DataInput;
import java.io.IOException;
import org.apache.tomcat.util.bcel.Const;
/**
* This class is derived from the abstract {@link Constant}
* and represents a reference to a Double object.
*
* @see Constant
*/
public final class ConstantDouble extends Constant {
private final double bytes;
/**
* Initialize instance from file data.
*
* @param file Input stream
* @throws IOException
*/
ConstantDouble(final DataInput file) throws IOException {
super(Const.CONSTANT_Double);
this.bytes = file.readDouble();
}
/**
* @return data, i.e., 8 bytes.
*/
public double getBytes() {
return bytes;
}
}

View File

@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.util.bcel.classfile;
import java.io.DataInput;
import java.io.IOException;
import org.apache.tomcat.util.bcel.Const;
/**
* This class is derived from the abstract {@link Constant}
* and represents a reference to a float object.
*
* @see Constant
*/
public final class ConstantFloat extends Constant {
private final float bytes;
/**
* Initialize instance from file data.
*
* @param file Input stream
* @throws IOException
*/
ConstantFloat(final DataInput file) throws IOException {
super(Const.CONSTANT_Float);
this.bytes = file.readFloat();
}
/**
* @return data, i.e., 4 bytes.
*/
public float getBytes() {
return bytes;
}
}

View File

@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.util.bcel.classfile;
import java.io.DataInput;
import java.io.IOException;
import org.apache.tomcat.util.bcel.Const;
/**
* This class is derived from the abstract {@link Constant}
* and represents a reference to an int object.
*
* @see Constant
*/
public final class ConstantInteger extends Constant {
private final int bytes;
/**
* Initialize instance from file data.
*
* @param file Input stream
* @throws IOException
*/
ConstantInteger(final DataInput file) throws IOException {
super(Const.CONSTANT_Integer);
this.bytes = file.readInt();
}
/**
* @return data, i.e., 4 bytes.
*/
public int getBytes() {
return bytes;
}
}

View File

@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.util.bcel.classfile;
import java.io.DataInput;
import java.io.IOException;
import org.apache.tomcat.util.bcel.Const;
/**
* This class is derived from the abstract {@link Constant}
* and represents a reference to a long object.
*
* @see Constant
*/
public final class ConstantLong extends Constant {
private final long bytes;
/**
* Initialize instance from file data.
*
* @param input Input stream
* @throws IOException
*/
ConstantLong(final DataInput input) throws IOException {
super(Const.CONSTANT_Long);
this.bytes = input.readLong();
}
/**
* @return data, i.e., 8 bytes.
*/
public long getBytes() {
return bytes;
}
}

View File

@@ -0,0 +1,107 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.util.bcel.classfile;
import java.io.DataInput;
import java.io.IOException;
import org.apache.tomcat.util.bcel.Const;
/**
* This class represents the constant pool, i.e., a table of constants, of
* a parsed classfile. It may contain null references, due to the JVM
* specification that skips an entry after an 8-byte constant (double,
* long) entry. Those interested in generating constant pools
* programatically should see <a href="../generic/ConstantPoolGen.html">
* ConstantPoolGen</a>.
* @see Constant
*/
public class ConstantPool {
private final Constant[] constant_pool;
/**
* Reads constants from given input stream.
*
* @param input Input stream
* @throws IOException
* @throws ClassFormatException
*/
ConstantPool(final DataInput input) throws IOException, ClassFormatException {
final int constant_pool_count = input.readUnsignedShort();
constant_pool = new Constant[constant_pool_count];
/* constant_pool[0] is unused by the compiler and may be used freely
* by the implementation.
*/
for (int i = 1; i < constant_pool_count; i++) {
constant_pool[i] = Constant.readConstant(input);
/* Quote from the JVM specification:
* "All eight byte constants take up two spots in the constant pool.
* If this is the n'th byte in the constant pool, then the next item
* will be numbered n+2"
*
* Thus we have to increment the index counter.
*/
if (constant_pool[i] != null) {
byte tag = constant_pool[i].getTag();
if ((tag == Const.CONSTANT_Double) || (tag == Const.CONSTANT_Long)) {
i++;
}
}
}
}
/**
* Gets constant from constant pool.
*
* @param index Index in constant pool
* @return Constant value
* @see Constant
*/
public Constant getConstant( final int index ) {
if (index >= constant_pool.length || index < 0) {
throw new ClassFormatException("Invalid constant pool reference: " + index
+ ". Constant pool size is: " + constant_pool.length);
}
return constant_pool[index];
}
/**
* Gets constant from constant pool and check whether it has the
* expected type.
*
* @param index Index in constant pool
* @param tag Tag of expected constant, i.e., its type
* @return Constant value
* @see Constant
* @throws ClassFormatException If the constant is not of the expected type
*/
public Constant getConstant( final int index, final byte tag ) throws ClassFormatException {
Constant c;
c = getConstant(index);
if (c == null) {
throw new ClassFormatException("Constant pool at index " + index + " is null.");
}
if (c.getTag() != tag) {
throw new ClassFormatException("Expected class `" + Const.getConstantName(tag)
+ "' at index " + index + " and got " + c);
}
return c;
}
}

View File

@@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.util.bcel.classfile;
import java.io.DataInput;
import java.io.IOException;
import org.apache.tomcat.util.bcel.Const;
/**
* This class is derived from the abstract
* <A HREF="org.apache.tomcat.util.bcel.classfile.Constant.html">Constant</A> class
* and represents a reference to a Utf8 encoded string.
*
* @see Constant
*/
public final class ConstantUtf8 extends Constant {
private final String bytes;
static ConstantUtf8 getInstance(final DataInput input) throws IOException {
return new ConstantUtf8(input.readUTF());
}
/**
* @param bytes Data
*/
private ConstantUtf8(final String bytes) {
super(Const.CONSTANT_Utf8);
if (bytes == null) {
throw new IllegalArgumentException("bytes must not be null!");
}
this.bytes = bytes;
}
/**
* @return Data converted to string.
*/
public final String getBytes() {
return bytes;
}
}

View File

@@ -0,0 +1,99 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.util.bcel.classfile;
import java.io.DataInput;
import java.io.IOException;
public abstract class ElementValue
{
private final int type;
private final ConstantPool cpool;
ElementValue(final int type, final ConstantPool cpool) {
this.type = type;
this.cpool = cpool;
}
public abstract String stringifyValue();
public static final byte STRING = 's';
public static final byte ENUM_CONSTANT = 'e';
public static final byte CLASS = 'c';
public static final byte ANNOTATION = '@';
public static final byte ARRAY = '[';
public static final byte PRIMITIVE_INT = 'I';
public static final byte PRIMITIVE_BYTE = 'B';
public static final byte PRIMITIVE_CHAR = 'C';
public static final byte PRIMITIVE_DOUBLE = 'D';
public static final byte PRIMITIVE_FLOAT = 'F';
public static final byte PRIMITIVE_LONG = 'J';
public static final byte PRIMITIVE_SHORT = 'S';
public static final byte PRIMITIVE_BOOLEAN = 'Z';
public static ElementValue readElementValue(final DataInput input, final ConstantPool cpool) throws IOException
{
final byte type = input.readByte();
switch (type)
{
case PRIMITIVE_BYTE:
case PRIMITIVE_CHAR:
case PRIMITIVE_DOUBLE:
case PRIMITIVE_FLOAT:
case PRIMITIVE_INT:
case PRIMITIVE_LONG:
case PRIMITIVE_SHORT:
case PRIMITIVE_BOOLEAN:
case STRING:
return new SimpleElementValue(type, input.readUnsignedShort(), cpool);
case ENUM_CONSTANT:
input.readUnsignedShort(); // Unused type_index
return new EnumElementValue(ENUM_CONSTANT, input.readUnsignedShort(), cpool);
case CLASS:
return new ClassElementValue(CLASS, input.readUnsignedShort(), cpool);
case ANNOTATION:
// TODO isRuntimeVisible
return new AnnotationElementValue(ANNOTATION, new AnnotationEntry(input, cpool), cpool);
case ARRAY:
final int numArrayVals = input.readUnsignedShort();
final ElementValue[] evalues = new ElementValue[numArrayVals];
for (int j = 0; j < numArrayVals; j++)
{
evalues[j] = ElementValue.readElementValue(input, cpool);
}
return new ArrayElementValue(ARRAY, evalues, cpool);
default:
throw new ClassFormatException(
"Unexpected element value kind in annotation: " + type);
}
}
final ConstantPool getConstantPool() {
return cpool;
}
final int getType() {
return type;
}
}

View File

@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.util.bcel.classfile;
import java.io.DataInput;
import java.io.IOException;
import org.apache.tomcat.util.bcel.Const;
/**
* an annotation's element value pair
*
* @since 6.0
*/
public class ElementValuePair
{
private final ElementValue elementValue;
private final ConstantPool constantPool;
private final int elementNameIndex;
ElementValuePair(final DataInput file, final ConstantPool constantPool) throws IOException {
this.constantPool = constantPool;
this.elementNameIndex = file.readUnsignedShort();
this.elementValue = ElementValue.readElementValue(file, constantPool);
}
public String getNameString()
{
final ConstantUtf8 c = (ConstantUtf8) constantPool.getConstant(
elementNameIndex, Const.CONSTANT_Utf8);
return c.getBytes();
}
public final ElementValue getValue()
{
return elementValue;
}
}

View File

@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.util.bcel.classfile;
import org.apache.tomcat.util.bcel.Const;
public class EnumElementValue extends ElementValue
{
private final int valueIdx;
EnumElementValue(final int type, final int valueIdx, final ConstantPool cpool) {
super(type, cpool);
if (type != ENUM_CONSTANT)
throw new RuntimeException(
"Only element values of type enum can be built with this ctor - type specified: " + type);
this.valueIdx = valueIdx;
}
@Override
public String stringifyValue()
{
final ConstantUtf8 cu8 = (ConstantUtf8) super.getConstantPool().getConstant(valueIdx,
Const.CONSTANT_Utf8);
return cu8.getBytes();
}
}

View File

@@ -0,0 +1,102 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.util.bcel.classfile;
/**
* Represents a Java class, i.e., the data structures, constant pool,
* fields, methods and commands contained in a Java .class file.
* See <a href="http://docs.oracle.com/javase/specs/">JVM specification</a> for details.
* The intent of this class is to represent a parsed or otherwise existing
* class file. Those interested in programatically generating classes
* should see the <a href="../generic/ClassGen.html">ClassGen</a> class.
*/
public class JavaClass {
private final int access_flags;
private final String class_name;
private final String superclass_name;
private final String[] interface_names;
private final Annotations runtimeVisibleAnnotations; // "RuntimeVisibleAnnotations" attribute defined in the class
/**
* Constructor gets all contents as arguments.
*
* @param class_name Name of this class.
* @param superclass_name Name of this class's superclass.
* @param access_flags Access rights defined by bit flags
* @param constant_pool Array of constants
* @param interface_names Implemented interfaces
* @param runtimeVisibleAnnotations "RuntimeVisibleAnnotations" attribute defined on the Class, or null
*/
JavaClass(final String class_name, final String superclass_name,
final int access_flags, final ConstantPool constant_pool, final String[] interface_names,
final Annotations runtimeVisibleAnnotations) {
this.access_flags = access_flags;
this.runtimeVisibleAnnotations = runtimeVisibleAnnotations;
this.class_name = class_name;
this.superclass_name = superclass_name;
this.interface_names = interface_names;
}
/**
* @return Access flags of the object aka. "modifiers".
*/
public final int getAccessFlags() {
return access_flags;
}
/**
* Return annotations entries from "RuntimeVisibleAnnotations" attribute on
* the class, if there is any.
*
* @return An array of entries or {@code null}
*/
public AnnotationEntry[] getAnnotationEntries() {
if (runtimeVisibleAnnotations != null) {
return runtimeVisibleAnnotations.getAnnotationEntries();
}
return null;
}
/**
* @return Class name.
*/
public String getClassName() {
return class_name;
}
/**
* @return Names of implemented interfaces.
*/
public String[] getInterfaceNames() {
return interface_names;
}
/**
* returns the super class name of this class. In the case that this class is
* java.lang.Object, it will return itself (java.lang.Object). This is probably incorrect
* but isn't fixed at this time to not break existing clients.
*
* @return Superclass name.
*/
public String getSuperclassName() {
return superclass_name;
}
}

View File

@@ -0,0 +1,91 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.util.bcel.classfile;
import org.apache.tomcat.util.bcel.Const;
public class SimpleElementValue extends ElementValue
{
private final int index;
SimpleElementValue(final int type, final int index, final ConstantPool cpool) {
super(type, cpool);
this.index = index;
}
/**
* @return Value entry index in the cpool
*/
public int getIndex()
{
return index;
}
// Whatever kind of value it is, return it as a string
@Override
public String stringifyValue()
{
final ConstantPool cpool = super.getConstantPool();
final int _type = super.getType();
switch (_type)
{
case PRIMITIVE_INT:
final ConstantInteger c = (ConstantInteger) cpool.getConstant(getIndex(),
Const.CONSTANT_Integer);
return Integer.toString(c.getBytes());
case PRIMITIVE_LONG:
final ConstantLong j = (ConstantLong) cpool.getConstant(getIndex(),
Const.CONSTANT_Long);
return Long.toString(j.getBytes());
case PRIMITIVE_DOUBLE:
final ConstantDouble d = (ConstantDouble) cpool.getConstant(getIndex(),
Const.CONSTANT_Double);
return Double.toString(d.getBytes());
case PRIMITIVE_FLOAT:
final ConstantFloat f = (ConstantFloat) cpool.getConstant(getIndex(),
Const.CONSTANT_Float);
return Float.toString(f.getBytes());
case PRIMITIVE_SHORT:
final ConstantInteger s = (ConstantInteger) cpool.getConstant(getIndex(),
Const.CONSTANT_Integer);
return Integer.toString(s.getBytes());
case PRIMITIVE_BYTE:
final ConstantInteger b = (ConstantInteger) cpool.getConstant(getIndex(),
Const.CONSTANT_Integer);
return Integer.toString(b.getBytes());
case PRIMITIVE_CHAR:
final ConstantInteger ch = (ConstantInteger) cpool.getConstant(
getIndex(), Const.CONSTANT_Integer);
return String.valueOf((char)ch.getBytes());
case PRIMITIVE_BOOLEAN:
final ConstantInteger bo = (ConstantInteger) cpool.getConstant(
getIndex(), Const.CONSTANT_Integer);
if (bo.getBytes() == 0) {
return "false";
}
return "true";
case STRING:
final ConstantUtf8 cu8 = (ConstantUtf8) cpool.getConstant(getIndex(),
Const.CONSTANT_Utf8);
return cu8.getBytes();
default:
throw new RuntimeException("SimpleElementValue class does not know how to stringify type " + _type);
}
}
}

View File

@@ -0,0 +1,86 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.util.bcel.classfile;
import java.io.DataInput;
import java.io.EOFException;
import java.io.IOException;
import org.apache.tomcat.util.bcel.Const;
/**
* Utility functions that do not really belong to any class in particular.
*/
final class Utility {
private Utility() {
// Hide default constructor
}
/**
* Shorten long class name <em>str</em>, i.e., chop off the <em>prefix</em>,
* if the
* class name starts with this string and the flag <em>chopit</em> is true.
* Slashes <em>/</em> are converted to dots <em>.</em>.
*
* @param str The long class name
* @return Compacted class name
*/
static String compactClassName(final String str) {
return str.replace('/', '.'); // Is `/' on all systems, even DOS
}
static String getClassName(final ConstantPool constant_pool, final int index) {
Constant c = constant_pool.getConstant(index, Const.CONSTANT_Class);
int i = ((ConstantClass) c).getNameIndex();
// Finally get the string from the constant pool
c = constant_pool.getConstant(i, Const.CONSTANT_Utf8);
String name = ((ConstantUtf8) c).getBytes();
return compactClassName(name);
}
static void skipFully(final DataInput file, final int length) throws IOException {
int total = file.skipBytes(length);
if (total != length) {
throw new EOFException();
}
}
static void swallowFieldOrMethod(final DataInput file)
throws IOException {
// file.readUnsignedShort(); // Unused access flags
// file.readUnsignedShort(); // name index
// file.readUnsignedShort(); // signature index
skipFully(file, 6);
int attributes_count = file.readUnsignedShort();
for (int i = 0; i < attributes_count; i++) {
swallowAttribute(file);
}
}
static void swallowAttribute(final DataInput file)
throws IOException {
//file.readUnsignedShort(); // Unused name index
skipFully(file, 2);
// Length of data in bytes
int length = file.readInt();
skipFully(file, length);
}
}

View File

@@ -0,0 +1,27 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
</head>
<body bgcolor="white">
<p>
This package contains the classes that describe the structure of a
Java class file and a class file parser.
</p>
</body>
</html>

View File

@@ -0,0 +1,30 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
</head>
<body bgcolor="white">
<p>
This package contains basic classes for the
<a href="https://commons.apache.org/bcel/">Byte Code Engineering Library</a>
and constants defined by the
<a href="http://docs.oracle.com/javase/specs/">
JVM specification</a>.
</p>
</body>
</html>