aboutsummaryrefslogtreecommitdiffstats
path: root/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/ThrowsPattern.java
blob: 7b276516d660ba801c1d020099a2eaaf44afc4c2 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* *******************************************************************
 * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
 * All rights reserved.
 * This program and the accompanying materials are made available
 * under the terms of the Eclipse Public License v 2.0
 * which accompanies this distribution and is available at
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
 *
 * Contributors:
 *     PARC     initial implementation
 * ******************************************************************/

package org.aspectj.weaver.patterns;

import java.io.IOException;
import java.util.Map;

import org.aspectj.weaver.CompressingDataOutputStream;
import org.aspectj.weaver.ISourceContext;
import org.aspectj.weaver.ResolvedType;
import org.aspectj.weaver.UnresolvedType;
import org.aspectj.weaver.VersionedDataInputStream;
import org.aspectj.weaver.World;

public class ThrowsPattern extends PatternNode {
	private TypePatternList required;
	private TypePatternList forbidden;

	public static final ThrowsPattern ANY = new ThrowsPattern(TypePatternList.EMPTY, TypePatternList.EMPTY);

	public ThrowsPattern(TypePatternList required, TypePatternList forbidden) {
		this.required = required;
		this.forbidden = forbidden;
	}

	public TypePatternList getRequired() {
		return required;
	}

	public TypePatternList getForbidden() {
		return forbidden;
	}

	public String toString() {
		if (this == ANY) {
			return "";
		}

		String ret = "throws " + required.toString();
		if (forbidden.size() > 0) {
			ret = ret + " !(" + forbidden.toString() + ")";
		}
		return ret;
	}

	public boolean equals(Object other) {
		if (!(other instanceof ThrowsPattern)) {
			return false;
		}
		ThrowsPattern o = (ThrowsPattern) other;
		boolean ret = o.required.equals(this.required) && o.forbidden.equals(this.forbidden);
		return ret;
	}

	public int hashCode() {
		int result = 17;
		result = 37 * result + required.hashCode();
		result = 37 * result + forbidden.hashCode();
		return result;
	}

	public ThrowsPattern resolveBindings(IScope scope, Bindings bindings) {
		if (this == ANY) {
			return this;
		}
		required = required.resolveBindings(scope, bindings, false, false);
		forbidden = forbidden.resolveBindings(scope, bindings, false, false);
		return this;
	}

	public ThrowsPattern parameterizeWith(Map<String,UnresolvedType> typeVariableMap, World w) {
		ThrowsPattern ret = new ThrowsPattern(required.parameterizeWith(typeVariableMap, w), forbidden.parameterizeWith(
				typeVariableMap, w));
		ret.copyLocationFrom(this);
		return ret;
	}

	public boolean matches(UnresolvedType[] tys, World world) {
		if (this == ANY) {
			return true;
		}

		// System.out.println("matching: " + this + " with " + Arrays.asList(tys));

		ResolvedType[] types = world.resolve(tys);
		// int len = types.length;
		for (int j = 0, lenj = required.size(); j < lenj; j++) {
			if (!matchesAny(required.get(j), types)) {
				return false;
			}
		}
		for (int j = 0, lenj = forbidden.size(); j < lenj; j++) {
			if (matchesAny(forbidden.get(j), types)) {
				return false;
			}
		}
		return true;
	}

	private boolean matchesAny(TypePattern typePattern, ResolvedType[] types) {
		for (int i = types.length - 1; i >= 0; i--) {
			if (typePattern.matchesStatically(types[i])) {
				return true;
			}
		}
		return false;
	}

	public static ThrowsPattern read(VersionedDataInputStream s, ISourceContext context) throws IOException {
		TypePatternList required = TypePatternList.read(s, context);
		TypePatternList forbidden = TypePatternList.read(s, context);
		if (required.size() == 0 && forbidden.size() == 0) {
			return ANY;
		}
		ThrowsPattern ret = new ThrowsPattern(required, forbidden);
		// XXXret.readLocation(context, s);
		return ret;
	}

	public void write(CompressingDataOutputStream s) throws IOException {
		required.write(s);
		forbidden.write(s);
		// XXXwriteLocation(s);
	}

	public Object accept(PatternNodeVisitor visitor, Object data) {
		return visitor.visit(this, data);
	}

	public Object traverse(PatternNodeVisitor visitor, Object data) {
		Object ret = accept(visitor, data);
		forbidden.traverse(visitor, data);
		required.traverse(visitor, data);
		return ret;
	}
}