Java tutorial
/* * Copyright 2016 The Bazel Authors. All rights reserved. * * 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 com.google.idea.blaze.java.run; import com.google.common.collect.Iterables; import com.google.idea.blaze.base.ideinfo.RuleIdeInfo; import com.google.idea.blaze.base.ideinfo.TestIdeInfo; import com.google.idea.blaze.base.model.primitives.Label; import com.google.idea.blaze.base.run.TestRuleFinder; import com.google.idea.blaze.base.run.rulefinder.RuleFinder; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiFile; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.File; /** * Utility methods for finding rules and Android facets. */ public final class RunUtil { private RunUtil() { } /** * @return The Blaze test rule containing the target test class. In the case of multiple * containing rules, the first rule sorted alphabetically by label. */ @Nullable public static RuleIdeInfo ruleForTestClass(@NotNull Project project, @NotNull PsiClass testClass, @Nullable TestIdeInfo.TestSize testSize) { File testFile = getFileForClass(testClass); if (testFile == null) { return null; } TestRuleFinder testRuleFinder = TestRuleFinder.getInstance(project); // We don't expose multiple rule choices, just pick first Label testLabel = Iterables.getFirst(testRuleFinder.testTargetsForSourceFile(testFile, testSize), null); if (testLabel == null) { return null; } return RuleFinder.getInstance().ruleForTarget(project, testLabel); } /** * Returns an instance of {@link java.io.File} related to the containing file of the given class. * It returns {@code null} if the given class is not contained in a file and only exists in * memory. */ @Nullable private static File getFileForClass(@NotNull PsiClass aClass) { PsiFile containingFile = aClass.getContainingFile(); if (containingFile == null) { return null; } VirtualFile virtualFile = containingFile.getVirtualFile(); if (virtualFile == null) { return null; } return new File(virtualFile.getPath()); } }