org.jetbrains.kotlin.resolve.scopes.JetScopeSelectorUtil Maven / Gradle / Ivy
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.resolve.scopes;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.name.Name;
import java.util.*;
public class JetScopeSelectorUtil {
private JetScopeSelectorUtil() {
}
public interface ScopeByNameSelector {
@Nullable
D get(@NotNull JetScope scope, @NotNull Name name);
}
public interface ScopeByNameMultiSelector {
@NotNull
Collection get(JetScope scope, Name name);
}
@NotNull
public static Collection collect(Collection scopes, ScopeByNameMultiSelector selector, Name name) {
Set descriptors = new HashSet();
for (JetScope scope : scopes) {
descriptors.addAll(selector.get(scope, name));
}
return descriptors;
}
public static final ScopeByNameSelector CLASSIFIER_DESCRIPTOR_SCOPE_SELECTOR =
new ScopeByNameSelector() {
@Nullable
@Override
public ClassifierDescriptor get(@NotNull JetScope scope, @NotNull Name name) {
return scope.getClassifier(name);
}
};
public static final ScopeByNameSelector PACKAGE_SCOPE_SELECTOR =
new ScopeByNameSelector() {
@Nullable
@Override
public PackageViewDescriptor get(@NotNull JetScope scope, @NotNull Name name) {
return scope.getPackage(name);
}
};
public static final ScopeByNameSelector VARIABLE_DESCRIPTOR_SCOPE_SELECTOR =
new ScopeByNameSelector() {
@Nullable
@Override
public VariableDescriptor get(@NotNull JetScope scope, @NotNull Name name) {
return scope.getLocalVariable(name);
}
};
public static final ScopeByNameMultiSelector NAMED_FUNCTION_SCOPE_SELECTOR =
new ScopeByNameMultiSelector() {
@NotNull
@Override
public Collection get(@NotNull JetScope scope, @NotNull Name name) {
return scope.getFunctions(name);
}
};
public static final ScopeByNameMultiSelector NAMED_PROPERTIES_SCOPE_SELECTOR =
new ScopeByNameMultiSelector() {
@NotNull
@Override
public Collection get(@NotNull JetScope scope, @NotNull Name name) {
return scope.getProperties(name);
}
};
@Nullable
public static D getFirstMatch(
@NotNull JetScope[] scopes,
@NotNull Name name,
@NotNull ScopeByNameSelector descriptorSelector
) {
for (JetScope scope : scopes) {
D descriptor = descriptorSelector.get(scope, name);
if (descriptor != null) {
return descriptor;
}
}
return null;
}
@NotNull
public static Set getFromAllScopes(
@NotNull JetScope[] scopes,
@NotNull Name name,
@NotNull ScopeByNameMultiSelector descriptorsSelector
) {
if (scopes.length == 0) return Collections.emptySet();
Set descriptors = new LinkedHashSet();
for (JetScope jetScope : scopes) {
descriptors.addAll(descriptorsSelector.get(jetScope, name));
}
return descriptors;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy