Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 125fa61877ceaca17a47e82861e43fe4bf9d0755 (plain) (blame)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*******************************************************************************
 * Copyright (c) 2000, 2017 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.internal.compiler.batch;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;

import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.env.AccessRestriction;
import org.eclipse.jdt.internal.compiler.env.AccessRuleSet;
import org.eclipse.jdt.internal.compiler.env.IModule;
import org.eclipse.jdt.internal.compiler.env.IModule.IModuleReference;
import org.eclipse.jdt.internal.compiler.lookup.ModuleBinding;
import org.eclipse.jdt.internal.compiler.util.SuffixConstants;

public abstract class ClasspathLocation implements FileSystem.Classpath,
		SuffixConstants {

	public static final int SOURCE = 1;
	public static final int BINARY = 2;

	String path;
	char[] normalizedPath;
	public AccessRuleSet accessRuleSet;
	IModule module;

	public String destinationPath;
		// destination path for compilation units that are reached through this
		// classpath location; the coding is consistent with the one of
		// Main.destinationPath:
		// == null: unspecified, use whatever value is set by the enclosing
		//          context, id est Main;
		// == Main.NONE: absorbent element, do not output class files;
		// else: use as the path of the directory into which class files must
		//       be written.
		// potentially carried by any entry that contains to be compiled files

	protected ClasspathLocation(AccessRuleSet accessRuleSet,
			String destinationPath) {
		this.accessRuleSet = accessRuleSet;
		this.destinationPath = destinationPath;
	}

	/**
	 * Return the first access rule which is violated when accessing a given
	 * type, or null if no 'non accessible' access rule applies.
	 *
	 * @param qualifiedBinaryFileName
	 *            tested type specification, formed as:
	 *            "org/eclipse/jdt/core/JavaCore.class"; on systems that
	 *            use \ as File.separator, the
	 *            "org\eclipse\jdt\core\JavaCore.class" is accepted as well
	 * @return the first access rule which is violated when accessing a given
	 *         type, or null if none applies
	 */
	protected AccessRestriction fetchAccessRestriction(String qualifiedBinaryFileName) {
		if (this.accessRuleSet == null)
			return null;
		char [] qualifiedTypeName = qualifiedBinaryFileName.
			substring(0, qualifiedBinaryFileName.length() - SUFFIX_CLASS.length)
			.toCharArray();
		if (File.separatorChar == '\\') {
			CharOperation.replace(qualifiedTypeName, File.separatorChar, '/');
		}
		return this.accessRuleSet.getViolatedRestriction(qualifiedTypeName);
	}
	
	public int getMode() {
		return SOURCE | BINARY;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + this.getMode();
		result = prime * result + ((this.path == null) ? 0 : this.path.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ClasspathLocation other = (ClasspathLocation) obj;
		String localPath = this.getPath();
		String otherPath = other.getPath();
		if (localPath == null) {
			if (otherPath != null)
				return false;
		} else if (!localPath.equals(otherPath))
			return false;
		if (this.getMode() != other.getMode())
			return false;
		return true;
	}
	@Override
	public String getPath() {
		return this.path;
	}
	@Override
	public String getDestinationPath() {
		return this.destinationPath;
	}
	
	@Override
	public void acceptModule(IModule mod) {
		this.module = mod;
	}
	@Override
	public boolean isAutomaticModule() {
		return this.module == null ? false : this.module.isAutomatic();
	}
	@Override
	public Collection<String> getModuleNames(Collection<String> limitModules) {
		return getModuleNames(limitModules, m -> getModule(m.toCharArray()));
	}
	@Override
	public Collection<String> getModuleNames(Collection<String> limitModules, Function<String,IModule> getModule) {
		if (this.module != null) {
			String name = String.valueOf(this.module.name());
			return selectModules(Collections.singleton(name), limitModules, getModule);
		}
		return Collections.emptyList();
	}
	protected Collection<String> selectModules(Set<String> modules, Collection<String> limitModules, Function<String,IModule> getModule) {
		Collection<String> rootModules;
		if (limitModules != null) {
			Set<String> result = new HashSet<>(modules);
			result.retainAll(limitModules);
			rootModules = result;
		} else {
			rootModules = allModules(modules, s -> s, m -> getModule(m.toCharArray()));
		}
		Set<String> allModules = new HashSet<>(rootModules);
		for (String mod : rootModules)
			addRequired(mod, allModules, getModule);
		return allModules;
	}

	private void addRequired(String mod, Set<String> allModules, Function<String,IModule> getModule) {
		IModule iMod = getModule(mod.toCharArray());
		if (iMod != null) {
			for (IModuleReference requiredRef : iMod.requires()) {
				IModule reqMod = getModule.apply(new String(requiredRef.name()));
				if (reqMod != null) {
					String reqModName = String.valueOf(reqMod.name());
					if (allModules.add(reqModName))
						addRequired(reqModName, allModules, getModule);
				}
			}
		}
	}
	protected <T> List<String> allModules(Iterable<T> allSystemModules, Function<T,String> getModuleName, Function<T,IModule> getModule) {
		List<String> result = new ArrayList<>();
		for (T mod : allSystemModules) {
			String moduleName = getModuleName.apply(mod);
			result.add(moduleName);
		}
		return result;
	}

	@Override
	public boolean isPackage(String qualifiedPackageName, String moduleName) {
		return getModulesDeclaringPackage(qualifiedPackageName, moduleName) != null;
	}

	protected char[][] singletonModuleNameIf(boolean condition) {
		if (!condition)
			return null;
		if (this.module != null)
			return new char[][] { this.module.name() };
		return new char[][] { ModuleBinding.UNNAMED };
	}
	
	@Override
	public void reset() {
		this.module = null;	
	}
}

Back to the top