Search in sources :

Example 26 with JavaPsiFacade

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

the class OnClickEnumSupport method getAllValues.

@Override
@NotNull
public List<ValueWithDisplayString> getAllValues() {
    Module module = myProperty.getModel().getModule();
    Configuration configuration = myProperty.getModel().getConfiguration();
    String activityClassName = configuration.getActivity();
    JavaPsiFacade facade = JavaPsiFacade.getInstance(module.getProject());
    Collection<PsiClass> classes;
    if (activityClassName != null) {
        if (activityClassName.startsWith(".")) {
            MergedManifest manifest = MergedManifest.get(module);
            String pkg = StringUtil.notNullize(manifest.getPackage());
            activityClassName = pkg + activityClassName;
        }
        PsiClass activityClass = facade.findClass(activityClassName, module.getModuleScope());
        if (activityClass != null) {
            classes = Collections.singletonList(activityClass);
        } else {
            classes = Collections.emptyList();
        }
    } else {
        GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module, false);
        PsiClass activityClass = facade.findClass(CLASS_ACTIVITY, scope);
        if (activityClass != null) {
            classes = ClassInheritorsSearch.search(activityClass, scope, true).findAll();
        } else {
            classes = Collections.emptyList();
        }
    }
    List<ValueWithDisplayString> values = new ArrayList<>();
    Set<String> found = new HashSet<>();
    for (PsiClass psiClass : classes) {
        for (PsiMethod method : psiClass.getAllMethods()) {
            if (OnClickConverter.CONVERTER_FOR_LAYOUT.checkSignature(method) && found.add(method.getName()) && method instanceof PsiMethodImpl) {
                values.add(new ValueWithDisplayString(method.getName(), method.getName(), psiClass.getName()));
            }
        }
    }
    return values;
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) Configuration(com.android.tools.idea.configurations.Configuration) MergedManifest(com.android.tools.idea.model.MergedManifest) PsiMethod(com.intellij.psi.PsiMethod) PsiMethodImpl(com.intellij.psi.impl.source.PsiMethodImpl) PsiClass(com.intellij.psi.PsiClass) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Example 27 with JavaPsiFacade

use of com.intellij.psi.JavaPsiFacade in project oxy-template-support-plugin by mutant-industries.

the class JavaContext method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    Module module;
    PsiElement element = parameters.getOriginalFile().getViewProvider().findElementAt(parameters.getOffset() - 1, OxyTemplateInnerJs.INSTANCE);
    if (element == null || element.getNode().getElementType() != JSTokenTypes.DOT && (element = element.getPrevSibling()) == null) {
        return;
    }
    if (element.getNode().getElementType() == TokenType.WHITE_SPACE) {
        element = element.getPrevSibling();
    }
    while (element != null && element.getNode().getElementType() != JSTokenTypes.DOT) {
        element = element.getLastChild();
        if (element instanceof PsiErrorElement) {
            element = element.getPrevSibling();
        }
    }
    if (element == null || element.getNode().getElementType() != JSTokenTypes.DOT || (element = element.getPrevSibling()) == null) {
        return;
    }
    if (element.getNode().getElementType() == TokenType.WHITE_SPACE) {
        element = element.getPrevSibling();
    }
    if (!(element instanceof JSExpression) || (module = ModuleUtilCore.findModuleForPsiElement(parameters.getOriginalFile())) == null) {
        return;
    }
    JSSimpleTypeProcessor typeProcessor = new JSSimpleTypeProcessor();
    JSTypeEvaluator.evaluateTypes((JSExpression) element, parameters.getOriginalFile().getViewProvider().getPsi(OxyTemplateInnerJs.INSTANCE), typeProcessor);
    JSType type = typeProcessor.getType();
    List<String> possibleJavaTypes = new LinkedList<>();
    if (type != null) {
        if (type instanceof JSCompositeTypeImpl) {
            possibleJavaTypes.addAll(((JSCompositeTypeImpl) type).getTypes().stream().filter(jsType -> jsType instanceof JSTypeImpl).map(JSType::getTypeText).collect(Collectors.toList()));
        } else if (type instanceof JSTypeImpl) {
            possibleJavaTypes.add(type.getTypeText());
        }
    }
    JavaPsiFacade facade = JavaPsiFacade.getInstance(parameters.getOriginalFile().getProject());
    GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module);
    List<PsiClass> possibleInstancesOf = new LinkedList<>();
    boolean suggestionsFound = false;
    PsiClass aClass;
    for (String possibleType : possibleJavaTypes) {
        if ((aClass = facade.findClass(possibleType, scope)) != null) {
            possibleInstancesOf.add(aClass);
            possibleInstancesOf.addAll(ExtenderProvider.getExtenders(aClass));
        }
    }
    for (PsiClass psiClass : possibleInstancesOf) {
        List<String> alreadySuggested = new LinkedList<>();
        for (PsiMethod method : psiClass.getAllMethods()) {
            if (method.getContainingClass() == null || Object.class.getName().equals(method.getContainingClass().getQualifiedName()) || method.getReturnType() == null || shippedBaseExtenderMethods.contains(method.getName()) || !method.getModifierList().hasModifierProperty(PsiModifier.PUBLIC) || InheritanceUtil.isInheritor(method.getContainingClass(), EXTENDER_INTERFACE_FQN) && !InheritanceUtil.isInheritor(method.getContainingClass(), EXTENDER_BASE_CLASS_FQN)) {
                continue;
            }
            String insertText = method.getName();
            String presentableText = method.getName();
            if (insertText.matches("((^is)|(^get)|(^set))[A-Z].*")) {
                insertText = presentableText = StringUtils.decapitalize(insertText.replaceFirst("(^is)|(^get)|(^set)", ""));
            } else {
                presentableText = method.getPresentation() == null ? presentableText + "()" : method.getPresentation().getPresentableText();
                insertText += "()";
            }
            if (alreadySuggested.contains(insertText)) {
                continue;
            }
            result.consume(LookupElementBuilder.create(method, insertText).withIcon(method.getIcon(0)).withTypeText(method.getReturnType().getPresentableText(), true).withTailText(" (" + psiClass.getContainingFile().getName() + ")", true).withPresentableText(presentableText).withInsertHandler(new TrailingPatternConsumer(INSERT_CONSUME)).withAutoCompletionPolicy(AutoCompletionPolicy.GIVE_CHANCE_TO_OVERWRITE));
            alreadySuggested.add(insertText);
            suggestionsFound = true;
        }
    }
    if (suggestionsFound) {
        result.stopHere();
    }
}
Also used : Arrays(java.util.Arrays) PsiErrorElement(com.intellij.psi.PsiErrorElement) JSTypeEvaluator(com.intellij.lang.javascript.psi.resolve.JSTypeEvaluator) StringUtils(com.sun.xml.internal.ws.util.StringUtils) JavaPsiFacade(com.intellij.psi.JavaPsiFacade) CompletionParameters(com.intellij.codeInsight.completion.CompletionParameters) JSCompositeTypeImpl(com.intellij.lang.javascript.psi.types.JSCompositeTypeImpl) Extender(ool.web.model.Extender) TokenType(com.intellij.psi.TokenType) PsiModifier(com.intellij.psi.PsiModifier) CompletionResultSet(com.intellij.codeInsight.completion.CompletionResultSet) CompletionContributor(com.intellij.codeInsight.completion.CompletionContributor) PsiClass(com.intellij.psi.PsiClass) JSTokenTypes(com.intellij.lang.javascript.JSTokenTypes) PsiElement(com.intellij.psi.PsiElement) InheritanceUtil(com.intellij.psi.util.InheritanceUtil) JSExpression(com.intellij.lang.javascript.psi.JSExpression) JSType(com.intellij.lang.javascript.psi.JSType) LinkedList(java.util.LinkedList) Module(com.intellij.openapi.module.Module) OxyTemplateInnerJs(ool.intellij.plugin.lang.OxyTemplateInnerJs) JSSimpleTypeProcessor(com.intellij.lang.javascript.psi.resolve.JSSimpleTypeProcessor) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) ModuleUtilCore(com.intellij.openapi.module.ModuleUtilCore) PsiMethod(com.intellij.psi.PsiMethod) ExtenderProvider(ool.intellij.plugin.psi.reference.innerjs.ExtenderProvider) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Collectors(java.util.stream.Collectors) AutoCompletionPolicy(com.intellij.codeInsight.lookup.AutoCompletionPolicy) List(java.util.List) TrailingPatternConsumer(ool.intellij.plugin.editor.completion.handler.TrailingPatternConsumer) Pattern(java.util.regex.Pattern) BaseExtender(ool.web.model.BaseExtender) NotNull(org.jetbrains.annotations.NotNull) JSTypeImpl(com.intellij.lang.javascript.psi.types.JSTypeImpl) JavaPsiFacade(com.intellij.psi.JavaPsiFacade) JSType(com.intellij.lang.javascript.psi.JSType) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) JSExpression(com.intellij.lang.javascript.psi.JSExpression) LinkedList(java.util.LinkedList) PsiErrorElement(com.intellij.psi.PsiErrorElement) JSTypeImpl(com.intellij.lang.javascript.psi.types.JSTypeImpl) JSCompositeTypeImpl(com.intellij.lang.javascript.psi.types.JSCompositeTypeImpl) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) TrailingPatternConsumer(ool.intellij.plugin.editor.completion.handler.TrailingPatternConsumer) JSSimpleTypeProcessor(com.intellij.lang.javascript.psi.resolve.JSSimpleTypeProcessor) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement)

Example 28 with JavaPsiFacade

use of com.intellij.psi.JavaPsiFacade in project moe-ide-integration by multi-os-engine.

the class MOEJUnitUtil method checkConfiguration.

public static void checkConfiguration(Module module, int TESTING_TYPE, String PACKAGE_NAME, String CLASS_NAME) throws RuntimeConfigurationException {
    JavaPsiFacade facade = JavaPsiFacade.getInstance(module.getProject());
    switch(TESTING_TYPE) {
        case TEST_ALL_IN_PACKAGE:
            if (PACKAGE_NAME == null || PACKAGE_NAME.isEmpty()) {
                throw new RuntimeConfigurationWarning("No test package specified");
            }
            final PsiPackage testPackage = facade.findPackage(PACKAGE_NAME);
            if (testPackage == null) {
                throw new RuntimeConfigurationWarning(ExecutionBundle.message("package.does.not.exist.error.message", PACKAGE_NAME));
            }
            Vector<PsiClass> packageTestClasses = MOEJUnitUtil.getTestClasses(module, PACKAGE_NAME);
            if (packageTestClasses == null || packageTestClasses.size() == 0) {
                throw new RuntimeConfigurationWarning("Not found test class in package: " + PACKAGE_NAME);
            }
            break;
        case TEST_CLASS:
            JavaRunConfigurationModule configurationModule = new JavaRunConfigurationModule(module.getProject(), false);
            configurationModule.setModule(module);
            final PsiClass testClass = configurationModule.checkModuleAndClassName(CLASS_NAME, ExecutionBundle.message("no.test.class.specified.error.text"));
            if (!JUnitUtil.isTestClass(testClass)) {
                throw new RuntimeConfigurationWarning(ExecutionBundle.message("class.isnt.test.class.error.message", CLASS_NAME));
            }
            break;
        case TEST_METHOD:
            // Already not used
            break;
    }
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) RuntimeConfigurationWarning(com.intellij.execution.configurations.RuntimeConfigurationWarning) PsiClass(com.intellij.psi.PsiClass) PsiPackage(com.intellij.psi.PsiPackage) JavaRunConfigurationModule(com.intellij.execution.configurations.JavaRunConfigurationModule)

Example 29 with JavaPsiFacade

use of com.intellij.psi.JavaPsiFacade in project moe-ide-integration by multi-os-engine.

the class MOETestClassBrowser method showDialog.

@Override
protected String showDialog() {
    Project project = getProject();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
    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) PsiModifierList(com.intellij.psi.PsiModifierList)

Example 30 with JavaPsiFacade

use of com.intellij.psi.JavaPsiFacade in project component-runtime by Talend.

the class SuggestionServiceImpl method computeKeySuggestions.

@Override
public List<LookupElement> computeKeySuggestions(final Project project, final Module module, final String packageName, final List<String> containerElements, final String query) {
    final JavaPsiFacade javaPsiFacade = JavaPsiFacade.getInstance(project);
    final PsiPackage pkg = javaPsiFacade.findPackage(packageName);
    if (pkg == null) {
        return Collections.emptyList();
    }
    final String defaultFamily = getFamilyFromPackageInfo(pkg, module);
    return Stream.concat(of(pkg.getClasses()).flatMap(this::unwrapInnerClasses).filter(c -> AnnotationUtil.findAnnotation(c, PARTITION_MAPPER, PROCESSOR, EMITTER) != null).flatMap(clazz -> fromComponent(clazz, defaultFamily)), of(pkg.getClasses()).flatMap(this::unwrapInnerClasses).filter(c -> of(c.getAllFields()).anyMatch(f -> AnnotationUtil.findAnnotation(f, OPTION) != null)).flatMap(c -> fromConfiguration(defaultFamily, c.getName(), c))).filter(s -> containerElements.isEmpty() || !containerElements.contains(s.getKey())).filter(s -> query == null || query.isEmpty() || s.getKey().startsWith(query)).map(s -> s.newLookupElement(withPriority(s.getType()))).collect(toList());
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) PsiPackage(com.intellij.psi.PsiPackage) PsiType(com.intellij.psi.PsiType) JavaPsiFacade(com.intellij.psi.JavaPsiFacade) FilenameIndex(com.intellij.psi.search.FilenameIndex) DISPLAY_NAME(org.talend.sdk.component.intellij.completion.properties.Suggestion.DISPLAY_NAME) CompletionParameters(com.intellij.codeInsight.completion.CompletionParameters) Collections.singletonList(java.util.Collections.singletonList) PsiClass(com.intellij.psi.PsiClass) PsiEnumConstant(com.intellij.psi.PsiEnumConstant) PsiClassReferenceType(com.intellij.psi.impl.source.PsiClassReferenceType) Project(com.intellij.openapi.project.Project) JavaRecursiveElementWalkingVisitor(com.intellij.psi.JavaRecursiveElementWalkingVisitor) ROOT(java.util.Locale.ROOT) Module(com.intellij.openapi.module.Module) PLACEHOLDER(org.talend.sdk.component.intellij.completion.properties.Suggestion.PLACEHOLDER) Suggestion(org.talend.sdk.component.intellij.completion.properties.Suggestion) LookupElement(com.intellij.codeInsight.lookup.LookupElement) Collections.emptyList(java.util.Collections.emptyList) Optional.ofNullable(java.util.Optional.ofNullable) Stream.of(java.util.stream.Stream.of) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiJavaFile(com.intellij.psi.PsiJavaFile) Objects(java.util.Objects) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Stream(java.util.stream.Stream) AnnotationUtil(com.intellij.codeInsight.AnnotationUtil) PsiAnnotation(com.intellij.psi.PsiAnnotation) JvmModifier(com.intellij.lang.jvm.JvmModifier) Collections(java.util.Collections) PsiAnnotationMemberValue(com.intellij.psi.PsiAnnotationMemberValue) PsiPackage(com.intellij.psi.PsiPackage)

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