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;
}
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"));
}
}
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;
}
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);
}
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);
}
Aggregations