aboutsummaryrefslogtreecommitdiffstats
path: root/org.aspectj.matcher/src/org/aspectj/weaver/patterns/SimpleScope.java
blob: 119103d84e949dedac6dac4185aa291f2da7510d (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* *******************************************************************
 * 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 v1.0 
 * which accompanies this distribution and is available at 
 * http://www.eclipse.org/legal/epl-v10.html 
 *  
 * Contributors: 
 *     PARC     initial implementation 
 * ******************************************************************/

package org.aspectj.weaver.patterns;

import org.aspectj.bridge.IMessage;
import org.aspectj.bridge.IMessageHandler;
import org.aspectj.bridge.ISourceLocation;
import org.aspectj.bridge.Message;
import org.aspectj.bridge.SourceLocation;
import org.aspectj.weaver.IHasPosition;
import org.aspectj.weaver.ResolvedType;
import org.aspectj.weaver.UnresolvedType;
import org.aspectj.weaver.World;

public class SimpleScope implements IScope {

	private static final String[] NoStrings = new String[0];
	private static final String[] javaLangPrefixArray = new String[] { "java.lang.", };

	private String[] importedPrefixes = javaLangPrefixArray;
	private String[] importedNames = NoStrings;
	private World world;
	private ResolvedType enclosingType;
	protected FormalBinding[] bindings;

	public SimpleScope(World world, FormalBinding[] bindings) {
		super();
		this.world = world;
		this.bindings = bindings;
	}

	public UnresolvedType lookupType(String name, IHasPosition location) {
		for (int i = 0; i < importedNames.length; i++) {
			String importedName = importedNames[i];
			// make sure we're matching against the type name rather than part of it
			// if (importedName.endsWith("." + name)) {
			if (importedName.endsWith(name)) {
				return world.resolve(importedName);
			}
		}

		// Check for a primitive
		if (name.length() < 8 && Character.isLowerCase(name.charAt(0))) {
			// could be a primitive
			int len = name.length();
			if (len == 3) {
				if (name.equals("int")) {
					return UnresolvedType.INT;
				}
			} else if (len == 4) {
				if (name.equals("void")) {
					return UnresolvedType.VOID;
				} else if (name.equals("byte")) {
					return UnresolvedType.BYTE;
				} else if (name.equals("char")) {
					return UnresolvedType.CHAR;
				} else if (name.equals("long")) {
					return UnresolvedType.LONG;
				}
			} else if (len == 5) {
				if (name.equals("float")) {
					return UnresolvedType.FLOAT;
				} else if (name.equals("short")) {
					return UnresolvedType.SHORT;
				}
			} else if (len == 6) {
				if (name.equals("double")) {
					return UnresolvedType.DOUBLE;
				}
			} else if (len == 7) {
				if (name.equals("boolean")) {
					return UnresolvedType.BOOLEAN;
				}
			}
		}

		// Is it fully qualified?
		if (name.indexOf('.') != -1) {
			return world.resolve(UnresolvedType.forName(name), true);
		}

		for (String importedPrefix : importedPrefixes) {
			ResolvedType tryType = world.resolve(UnresolvedType.forName(importedPrefix + name), true);
			if (!tryType.isMissing()) {
				return tryType;
			}
		}

		return world.resolve(UnresolvedType.forName(name), true);
	}

	public IMessageHandler getMessageHandler() {
		return world.getMessageHandler();
	}

	public FormalBinding lookupFormal(String name) {
		for (int i = 0, len = bindings.length; i < len; i++) {
			if (bindings[i].getName().equals(name)) {
				return bindings[i];
			}
		}
		return null;
	}

	public FormalBinding getFormal(int i) {
		return bindings[i];
	}

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

	public String[] getImportedNames() {
		return importedNames;
	}

	public String[] getImportedPrefixes() {
		return importedPrefixes;
	}

	public void setImportedNames(String[] importedNames) {
		this.importedNames = importedNames;
	}

	public void setImportedPrefixes(String[] importedPrefixes) {
		this.importedPrefixes = importedPrefixes;
	}

	public static FormalBinding[] makeFormalBindings(UnresolvedType[] types, String[] names) {
		int len = types.length;
		FormalBinding[] bindings = new FormalBinding[len];
		for (int i = 0; i < len; i++) {
			bindings[i] = new FormalBinding(types[i], names[i], i);
		}
		return bindings;
	}

	public ISourceLocation makeSourceLocation(IHasPosition location) {
		return new SourceLocation(ISourceLocation.NO_FILE, 0);
	}

	public void message(IMessage.Kind kind, IHasPosition location1, IHasPosition location2, String message) {
		message(kind, location1, message);
		message(kind, location2, message);
	}

	public void message(IMessage.Kind kind, IHasPosition location, String message) {
		getMessageHandler().handleMessage(new Message(message, kind, null, makeSourceLocation(location)));
	}

	public void message(IMessage aMessage) {
		getMessageHandler().handleMessage(aMessage);
	}

	public World getWorld() {
		return world;
	}

	public ResolvedType getEnclosingType() {
		return enclosingType;
	}

}