Search in sources :

Example 31 with JavaPsiFacade

use of com.intellij.psi.JavaPsiFacade in project android by JetBrains.

the class ModuleClassLoader method isSourceModified.

/**
   * Determines whether the class specified by the given qualified name has a source file in the IDE that
   * has been edited more recently than its corresponding class file.
   * <p/>This method requires the indexing to have finished
   * <p/><b>Note that this method can only answer queries for classes that this class loader has previously
   * loaded!</b>
   *
   * @param fqcn the fully qualified class name
   * @param myCredential a render sandbox credential
   * @return true if the source file has been modified, or false if not (or if the source file cannot be found)
   */
public boolean isSourceModified(@NotNull final String fqcn, @Nullable final Object myCredential) {
    final Module module = myModuleReference.get();
    if (module == null) {
        return false;
    }
    VirtualFile classFile = getClassFile(fqcn);
    // Make sure the class file is up to date and if not, log an error
    if (classFile != null) {
        // Allow creating class loaders during rendering; may be prevented by the RenderSecurityManager
        boolean token = RenderSecurityManager.enterSafeRegion(myCredential);
        try {
            long classFileModified = classFile.getTimeStamp();
            if (classFileModified > 0L) {
                VirtualFile virtualFile = ApplicationManager.getApplication().runReadAction(new Computable<VirtualFile>() {

                    @Nullable
                    @Override
                    public VirtualFile compute() {
                        Project project = module.getProject();
                        GlobalSearchScope scope = module.getModuleWithDependenciesScope();
                        PsiManager psiManager = PsiManager.getInstance(project);
                        JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(psiManager.getProject());
                        PsiClass source = psiFacade.findClass(fqcn, scope);
                        if (source != null) {
                            PsiFile containingFile = source.getContainingFile();
                            if (containingFile != null) {
                                return containingFile.getVirtualFile();
                            }
                        }
                        return null;
                    }
                });
                if (virtualFile != null && !FN_RESOURCE_CLASS.equals(virtualFile.getName())) {
                    // Don't flag R.java edits; not done by user
                    // Edited but not yet saved?
                    boolean modified = FileDocumentManager.getInstance().isFileModified(virtualFile);
                    if (!modified) {
                        // Check timestamp
                        File sourceFile = VfsUtilCore.virtualToIoFile(virtualFile);
                        long sourceFileModified = sourceFile.lastModified();
                        AndroidFacet facet = AndroidFacet.getInstance(module);
                        // User modifications on the source file might not always result on a new .class file.
                        // We use the project modification time instead to display the warning more reliably.
                        // Also, some build systems may use a constant last modified timestamp for .class files,
                        // for deterministic builds, so the project modification time is more reliable.
                        long lastBuildTimestamp = classFileModified;
                        if (facet != null && facet.requiresAndroidModel() && facet.getAndroidModel() != null) {
                            Long projectBuildTimestamp = facet.getAndroidModel().getLastBuildTimestamp(module.getProject());
                            if (projectBuildTimestamp != null) {
                                lastBuildTimestamp = projectBuildTimestamp;
                            }
                        }
                        if (sourceFileModified > lastBuildTimestamp && lastBuildTimestamp > 0L) {
                            modified = true;
                        }
                    }
                    return modified;
                }
            }
        } finally {
            RenderSecurityManager.exitSafeRegion(token);
        }
    }
    return false;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) JavaPsiFacade(com.intellij.psi.JavaPsiFacade) PsiClass(com.intellij.psi.PsiClass) PsiManager(com.intellij.psi.PsiManager) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Example 32 with JavaPsiFacade

use of com.intellij.psi.JavaPsiFacade in project android by JetBrains.

the class SpecificActivityLocator method validate.

@Override
public void validate() throws ActivityLocatorException {
    if (myActivityName == null || myActivityName.length() == 0) {
        throw new ActivityLocatorException(AndroidBundle.message("activity.class.not.specified.error"));
    }
    if (doesPackageContainMavenProperty(myFacet)) {
        return;
    }
    Module module = myFacet.getModule();
    Project project = module.getProject();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
    PsiClass activityClass = facade.findClass(AndroidUtils.ACTIVITY_BASE_CLASS_NAME, ProjectScope.getAllScope(project));
    if (activityClass == null) {
        throw new ActivityLocatorException(AndroidBundle.message("cant.find.activity.class.error"));
    }
    PsiClass c = JavaExecutionUtil.findMainClass(project, myActivityName, GlobalSearchScope.projectScope(project));
    Element element;
    if (c == null || !c.isInheritor(activityClass, true)) {
        element = MergedManifest.get(module).findActivityAlias(myActivityName);
        if (element == null) {
            throw new ActivityLocatorException(AndroidBundle.message("not.activity.subclass.error", myActivityName));
        }
    } else {
        // check whether activity is declared in the manifest
        element = MergedManifest.get(module).findActivity(ActivityLocatorUtils.getQualifiedActivityName(c));
        if (element == null) {
            throw new ActivityLocatorException(AndroidBundle.message("activity.not.declared.in.manifest", c.getName()));
        }
    }
    DefaultActivityLocator.ActivityWrapper activity = DefaultActivityLocator.ActivityWrapper.get(element);
    Boolean exported = activity.getExported();
    // if the activity is not explicitly exported, and it doesn't have an intent filter, then it cannot be launched
    if (!Boolean.TRUE.equals(exported) && !activity.hasIntentFilter()) {
        throw new ActivityLocatorException(AndroidBundle.message("specific.activity.not.launchable.error"));
    }
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) Project(com.intellij.openapi.project.Project) Element(org.w3c.dom.Element) PsiClass(com.intellij.psi.PsiClass) Module(com.intellij.openapi.module.Module)

Example 33 with JavaPsiFacade

use of com.intellij.psi.JavaPsiFacade in project android by JetBrains.

the class AndroidClassBrowserBase method showDialog.

@Override
protected String showDialog() {
    Project project = getProject();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
    Module module = myModuleSelector.getModule();
    if (module == null) {
        Messages.showErrorDialog(project, ExecutionBundle.message("module.not.specified.error.text"), CommonBundle.getErrorTitle());
        return null;
    }
    GlobalSearchScope scope = myIncludeLibraryClasses ? module.getModuleWithDependenciesAndLibrariesScope(true) : module.getModuleWithDependenciesScope();
    PsiClass initialSelection = facade.findClass(getText(), scope);
    TreeClassChooser chooser = createTreeClassChooser(project, scope, initialSelection, new ClassFilter() {

        @Override
        public boolean isAccepted(PsiClass aClass) {
            if (aClass.isInterface())
                return false;
            final PsiModifierList modifierList = aClass.getModifierList();
            return modifierList == null || !modifierList.hasModifierProperty(PsiModifier.ABSTRACT);
        }
    });
    if (chooser == null)
        return null;
    chooser.showDialog();
    PsiClass selClass = chooser.getSelected();
    return selClass != null ? selClass.getQualifiedName() : null;
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) Project(com.intellij.openapi.project.Project) TreeClassChooser(com.intellij.ide.util.TreeClassChooser) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ClassFilter(com.intellij.ide.util.ClassFilter) PsiClass(com.intellij.psi.PsiClass) Module(com.intellij.openapi.module.Module) PsiModifierList(com.intellij.psi.PsiModifierList)

Example 34 with JavaPsiFacade

use of com.intellij.psi.JavaPsiFacade in project android by JetBrains.

the class AndroidPsiElementFinderTest method testResourceClasses.

public void testResourceClasses() throws Exception {
    myFixture.copyFileToProject(BASE_PATH + "values.xml", "res/values/values.xml");
    myFixture.copyFileToProject("R.java", "src/p1/p2/R.java");
    final Project project = getProject();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
    assertNotNull(facade.findClass("p1.p2.R.drawable", GlobalSearchScope.projectScope(project)));
    assertEquals(1, facade.findClasses("p1.p2.R.drawable", GlobalSearchScope.projectScope(project)).length);
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) Project(com.intellij.openapi.project.Project)

Example 35 with JavaPsiFacade

use of com.intellij.psi.JavaPsiFacade in project android by JetBrains.

the class AndroidSdkSourcesBrowsingTest method testFindClass_CanFindInternalSdkReferences.

public void testFindClass_CanFindInternalSdkReferences() throws Exception {
    JavaPsiFacade facade = JavaPsiFacade.getInstance(getProject());
    PsiClass activity = facade.findClass("android.app.Activity", GlobalSearchScope.allScope(getProject()));
    GlobalSearchScope scope = activity.getNavigationElement().getResolveScope();
    assertInstanceOf(scope, AndroidSdkResolveScopeProvider.MyJdkScope.class);
    assertNotNull(facade.findClass(AndroidPsiElementFinder.INTERNAL_R_CLASS_QNAME, scope));
    assertNotNull(facade.findClass(AndroidPsiElementFinder.INTERNAL_R_CLASS_QNAME + ".string", scope));
    PsiClass[] classes = facade.findClasses(AndroidPsiElementFinder.INTERNAL_R_CLASS_QNAME, scope);
    assertEquals(1, classes.length);
    classes = facade.findClasses(AndroidPsiElementFinder.INTERNAL_R_CLASS_QNAME + ".string", scope);
    assertEquals(1, classes.length);
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) AndroidSdkResolveScopeProvider(org.jetbrains.android.AndroidSdkResolveScopeProvider) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiClass(com.intellij.psi.PsiClass)

Aggregations

JavaPsiFacade (com.intellij.psi.JavaPsiFacade)52 PsiClass (com.intellij.psi.PsiClass)30 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)20 Module (com.intellij.openapi.module.Module)15 Project (com.intellij.openapi.project.Project)12 PsiPackage (com.intellij.psi.PsiPackage)8 LanguageLevel (com.intellij.pom.java.LanguageLevel)7 PsiElement (com.intellij.psi.PsiElement)7 Nullable (org.jetbrains.annotations.Nullable)6 NotNull (org.jetbrains.annotations.NotNull)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 PsiMethod (com.intellij.psi.PsiMethod)4 PsiType (com.intellij.psi.PsiType)4 File (java.io.File)3 List (java.util.List)3 Configuration (com.android.tools.idea.configurations.Configuration)2 MergedManifest (com.android.tools.idea.model.MergedManifest)2 CompletionParameters (com.intellij.codeInsight.completion.CompletionParameters)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 ClassFilter (com.intellij.ide.util.ClassFilter)2