aboutsummaryrefslogtreecommitdiffstats
path: root/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/Bindings.java
blob: 0de1c1056b5406c6eb1f7c8826bd84415f2c2e28 (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
/* *******************************************************************
 * 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 org.aspectj.bridge.IMessage;
import org.aspectj.weaver.BCException;
import org.aspectj.weaver.UnresolvedType;

public class Bindings {
	public static final Bindings NONE = new Bindings(0);

	private BindingPattern[] bindings;

	public Bindings(BindingPattern[] bindings) {
		this.bindings = bindings;
	}

	public Bindings(int count) {
		this(new BindingPattern[count]);
	}

	public void register(BindingPattern binding, IScope scope) {
		int index = binding.getFormalIndex();
		BindingPattern existingBinding = bindings[index];
		if (existingBinding != null) {
			scope.message(IMessage.ERROR, existingBinding, binding, "multiple bindings" + index + ", " + binding);
		}
		bindings[index] = binding;
	}

	public void mergeIn(Bindings other, IScope scope) {
		for (int i = 0, len = other.bindings.length; i < len; i++) {
			if (other.bindings[i] != null) {
				register(other.bindings[i], scope);
			}
		}
	}

	/**
	 * signals an error if one has a binding and other doesn't
	 */
	public void checkEquals(Bindings other, IScope scope) {
		BindingPattern[] b1 = this.bindings;
		BindingPattern[] b2 = other.bindings;
		int len = b1.length;
		if (len != b2.length) {
			throw new BCException("INSANE");
		}

		for (int i = 0; i < len; i++) {
			if (b1[i] == null && b2[i] != null) {
				scope.message(IMessage.ERROR, b2[i], "inconsistent binding");
				b1[i] = b2[i]; // done just to produce fewer error messages
			} else if (b2[i] == null && b1[i] != null) {
				scope.message(IMessage.ERROR, b1[i], "inconsistent binding");
				b2[i] = b1[i]; // done just to produce fewer error messages
			}
		}
	}

	public String toString() {
		StringBuilder buf = new StringBuilder("Bindings(");
		for (int i = 0, len = bindings.length; i < len; i++) {
			if (i > 0)
				buf.append(", ");
			buf.append(bindings[i]);
		}
		buf.append(")");
		return buf.toString();
	}

	public int[] getUsedFormals() {
		// System.out.println("used: " + this);
		int[] ret = new int[bindings.length];
		int index = 0;
		for (int i = 0, len = bindings.length; i < len; i++) {
			if (bindings[i] != null) {
				ret[index++] = i;
			}
		}
		int[] newRet = new int[index];
		System.arraycopy(ret, 0, newRet, 0, index);
		// System.out.println("ret: " + index);
		return newRet;
	}

	public UnresolvedType[] getUsedFormalTypes() {
		UnresolvedType[] ret = new UnresolvedType[bindings.length];
		int index = 0;
		for (BindingPattern binding : bindings) {
			if (binding != null) {
				ret[index++] = ((BindingTypePattern) binding).getExactType();
			}
		}
		UnresolvedType[] newRet = new UnresolvedType[index];
		System.arraycopy(ret, 0, newRet, 0, index);
		// System.out.println("ret: " + index);
		return newRet;
	}

	public Bindings copy() {
		return new Bindings(bindings.clone());
	}

	public void checkAllBound(IScope scope) {
		for (int i = 0, len = bindings.length; i < len; i++) {
			if (bindings[i] == null) {
				// ATAJ: avoid warnings for implicit bindings
				if (scope.getFormal(i) instanceof FormalBinding.ImplicitFormalBinding) {
					bindings[i] = new BindingTypePattern(scope.getFormal(i), false);
				} else {
					scope.message(IMessage.ERROR, scope.getFormal(i), "formal unbound in pointcut ");
				}
			}
		}

	}

	public int size() {
		return bindings.length;
	}

}