aboutsummaryrefslogtreecommitdiffstats
path: root/bcel-builder/src/main/java/org/aspectj/apache/bcel/classfile/AttributeUtils.java
blob: d28a4f9f4b2f938c85f3a5dd99d81e72aff46975 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package org.aspectj.apache.bcel.classfile;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.aspectj.apache.bcel.Constants;

public class AttributeUtils {

	public static Attribute[] readAttributes(DataInputStream dataInputstream, ConstantPool cpool) {
		try {
			int length = dataInputstream.readUnsignedShort();
			if (length == 0) {
				return Attribute.NoAttributes;
			}
			Attribute[] attrs = new Attribute[length];
			for (int i = 0; i < length; i++) {
				attrs[i] = Attribute.readAttribute(dataInputstream, cpool);
			}
			return attrs;
		} catch (IOException e) {
			throw new ClassFormatException("IOException whilst reading set of attributes: " + e.toString());
		}
	}

	/** Write (serialize) a set of attributes into a specified output stream */
	public static void writeAttributes(Attribute[] attributes, DataOutputStream file) throws IOException {
		if (attributes == null) {
			file.writeShort(0);
		} else {
			file.writeShort(attributes.length);
			for (Attribute attribute : attributes) {
				attribute.dump(file);
			}
		}
	}

	public static Signature getSignatureAttribute(Attribute[] attributes) {
		for (Attribute attribute : attributes) {
			if (attribute.tag == Constants.ATTR_SIGNATURE) {
				return (Signature) attribute;
			}
		}
		return null;
	}

	public static Code getCodeAttribute(Attribute[] attributes) {
		for (Attribute attribute : attributes) {
			if (attribute.tag == Constants.ATTR_CODE) {
				return (Code) attribute;
			}
		}
		return null;
	}

	public static ExceptionTable getExceptionTableAttribute(Attribute[] attributes) {
		for (Attribute attribute : attributes) {
			if (attribute.tag == Constants.ATTR_EXCEPTIONS) {
				return (ExceptionTable) attribute;
			}
		}
		return null;
	}

	public static ConstantValue getConstantValueAttribute(Attribute[] attributes) {
		for (Attribute attribute : attributes) {
			if (attribute.getTag() == Constants.ATTR_CONSTANT_VALUE) {
				return (ConstantValue) attribute;
			}
		}
		return null;
	}

	public static void accept(Attribute[] attributes, ClassVisitor visitor) {
		for (Attribute attribute : attributes) {
			attribute.accept(visitor);
		}
	}

	public static boolean hasSyntheticAttribute(Attribute[] attributes) {
		for (Attribute attribute : attributes) {
			if (attribute.tag == Constants.ATTR_SYNTHETIC) {
				return true;
			}
		}
		return false;
	}

	public static SourceFile getSourceFileAttribute(Attribute[] attributes) {
		for (Attribute attribute : attributes) {
			if (attribute.tag == Constants.ATTR_SOURCE_FILE) {
				return (SourceFile) attribute;
			}
		}
		return null;
	}

}