use of com.intellij.openapi.projectRoots.SdkModificator in project android by JetBrains.
the class AndroidSdkUtils method refreshLibrariesIn.
/**
* Refresh the library {@link VirtualFile}s in the given {@link Sdk}.
*
* After changes to installed Android SDK components, the contents of the {@link Sdk}s do not automatically get refreshed.
* The referenced {@link VirtualFile}s can be obsolete, new files may be created, or files may be deleted. The result is that
* references to Android classes may not be found in editors.
* Removing and adding the libraries effectively refreshes the contents of the IDEA SDK, and references in editors work again.
*/
public static void refreshLibrariesIn(@NotNull Sdk sdk) {
VirtualFile[] libraries = sdk.getRootProvider().getFiles(CLASSES);
SdkModificator sdkModificator = sdk.getSdkModificator();
sdkModificator.removeRoots(CLASSES);
sdkModificator.commitChanges();
sdkModificator = sdk.getSdkModificator();
for (VirtualFile library : libraries) {
sdkModificator.addRoot(library, CLASSES);
}
sdkModificator.commitChanges();
}
use of com.intellij.openapi.projectRoots.SdkModificator in project android by JetBrains.
the class AndroidTestBase method createLatestAndroidSdk.
public static Sdk createLatestAndroidSdk() {
String sdkPath = TestUtils.getSdk().toString();
String platformDir = TestUtils.getLatestAndroidPlatform();
Sdk sdk = ProjectJdkTable.getInstance().createSdk("android_test_sdk", AndroidSdkType.getInstance());
SdkModificator sdkModificator = sdk.getSdkModificator();
sdkModificator.setHomePath(sdkPath);
VirtualFile androidJar = JarFileSystem.getInstance().findFileByPath(sdkPath + "/platforms/" + platformDir + "/android.jar!/");
sdkModificator.addRoot(androidJar, OrderRootType.CLASSES);
VirtualFile resFolder = LocalFileSystem.getInstance().findFileByPath(sdkPath + "/platforms/" + platformDir + "/data/res");
sdkModificator.addRoot(resFolder, OrderRootType.CLASSES);
VirtualFile docsFolder = LocalFileSystem.getInstance().findFileByPath(sdkPath + "/docs/reference");
if (docsFolder != null) {
sdkModificator.addRoot(docsFolder, JavadocOrderRootType.getInstance());
}
AndroidSdkAdditionalData data = new AndroidSdkAdditionalData(sdk);
AndroidSdkData sdkData = AndroidSdkData.getSdkData(sdkPath);
assertNotNull(sdkData);
IAndroidTarget target = null;
IAndroidTarget[] targets = sdkData.getTargets();
for (IAndroidTarget t : targets) {
if (t.getLocation().contains(platformDir)) {
target = t;
break;
}
}
assertNotNull(target);
data.setBuildTarget(target);
sdkModificator.setSdkAdditionalData(data);
ExternalAnnotationsSupport.attachJdkAnnotations(sdkModificator);
sdkModificator.commitChanges();
return sdk;
}
use of com.intellij.openapi.projectRoots.SdkModificator in project android by JetBrains.
the class AndroidFacet method addFilesToSdkIfNecessary.
private static void addFilesToSdkIfNecessary(@NotNull Sdk sdk, @NotNull Collection<VirtualFile> files) {
List<VirtualFile> newFiles = Lists.newArrayList(files);
newFiles.removeAll(Arrays.asList(sdk.getRootProvider().getFiles(OrderRootType.CLASSES)));
if (newFiles.size() > 0) {
SdkModificator modificator = sdk.getSdkModificator();
for (VirtualFile file : newFiles) {
modificator.addRoot(file, OrderRootType.CLASSES);
}
modificator.commitChanges();
}
}
use of com.intellij.openapi.projectRoots.SdkModificator in project android by JetBrains.
the class AndroidSdkSourceAttachTest method testRefreshSdkSource.
@Ignore("failed in http://go/aj/job/studio-ui-test/417 and from IDEA")
@Test
public void testRefreshSdkSource() throws IOException {
assumeTrue("Android Sdk Source for '" + mySdk.getName() + "' must be installed before running 'testRefreshSdkSource'", mySdkSourcePath.isDirectory());
SdkModificator sdkModificator = mySdk.getSdkModificator();
sdkModificator.removeRoots(OrderRootType.SOURCES);
sdkModificator.commitChanges();
guiTest.importSimpleApplication();
final EditorFixture editor = guiTest.ideFrame().getEditor();
final VirtualFile classFile = findActivityClassFile();
editor.open(classFile, EditorFixture.Tab.EDITOR);
acceptLegalNoticeIfNeeded();
// Refresh the source.
editor.awaitNotification("Sources for '" + mySdk.getName() + "' not found.").performAction("Refresh (if already downloaded)");
Wait.seconds(1).expecting("source file to be opened").until(() -> !classFile.equals(editor.getCurrentFile()));
assertIsActivityJavaFile(editor.getCurrentFile());
}
use of com.intellij.openapi.projectRoots.SdkModificator in project android by JetBrains.
the class ExternalAnnotationsSupport method checkAnnotationsJarAttached.
// Based on similar code in MagicConstantInspection
@SuppressWarnings("ALL")
private static void checkAnnotationsJarAttached(@NotNull PsiFile file, @NotNull ProblemsHolder holder) {
// Not yet used
if (false) {
final Project project = file.getProject();
PsiClass actionBar = JavaPsiFacade.getInstance(project).findClass("android.app.ActionBar", GlobalSearchScope.allScope(project));
if (actionBar == null) {
// no sdk to attach
return;
}
PsiMethod[] methods = actionBar.findMethodsByName("getNavigationMode", false);
if (methods.length != 1) {
// no sdk to attach
return;
}
PsiMethod getModifiers = methods[0];
ExternalAnnotationsManager annotationsManager = ExternalAnnotationsManager.getInstance(project);
PsiAnnotation annotation = annotationsManager.findExternalAnnotation(getModifiers, MagicConstant.class.getName());
if (annotation != null) {
return;
}
final VirtualFile virtualFile = PsiUtilCore.getVirtualFile(getModifiers);
if (virtualFile == null) {
// no sdk to attach
return;
}
final List<OrderEntry> entries = ProjectRootManager.getInstance(project).getFileIndex().getOrderEntriesForFile(virtualFile);
Sdk sdk = null;
for (OrderEntry orderEntry : entries) {
if (orderEntry instanceof JdkOrderEntry) {
sdk = ((JdkOrderEntry) orderEntry).getJdk();
if (sdk != null) {
break;
}
}
}
if (sdk == null) {
// no sdk to attach
return;
}
final Sdk finalSdk = sdk;
String path = finalSdk.getHomePath();
String text = "No IDEA annotations attached to the Android SDK " + finalSdk.getName() + (path == null ? "" : " (" + FileUtil.toSystemDependentName(path) + ")") + ", some issues will not be found";
holder.registerProblem(file, text, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new LocalQuickFix() {
@NotNull
@Override
public String getName() {
return "Attach annotations";
}
@NotNull
@Override
public String getFamilyName() {
return getName();
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
SdkModificator modifier = finalSdk.getSdkModificator();
attachJdkAnnotations(modifier);
modifier.commitChanges();
}
});
}
});
}
}
Aggregations