Search in sources :

Example 1 with PsiParameter

use of com.intellij.psi.PsiParameter in project Conductor by bluelinelabs.

the class ControllerIssueDetector method checkClass.

@Override
public void checkClass(JavaContext context, PsiClass declaration) {
    final JavaEvaluator evaluator = context.getEvaluator();
    if (evaluator.isAbstract(declaration)) {
        return;
    }
    if (!evaluator.isPublic(declaration)) {
        String message = String.format("This Controller class should be public (%1$s)", declaration.getQualifiedName());
        context.report(ISSUE, declaration, context.getLocation(declaration), message);
        return;
    }
    if (declaration.getContainingClass() != null && !evaluator.isStatic(declaration)) {
        String message = String.format("This Controller inner class should be static (%1$s)", declaration.getQualifiedName());
        context.report(ISSUE, declaration, context.getLocation(declaration), message);
        return;
    }
    boolean hasDefaultConstructor = false;
    boolean hasBundleConstructor = false;
    PsiMethod[] constructors = declaration.getConstructors();
    for (PsiMethod constructor : constructors) {
        if (evaluator.isPublic(constructor)) {
            PsiParameter[] parameters = constructor.getParameterList().getParameters();
            if (parameters.length == 0) {
                hasDefaultConstructor = true;
                break;
            } else if (parameters.length == 1 && parameters[0].getType().equalsToText(SdkConstants.CLASS_BUNDLE) || parameters[0].getType().equalsToText("Bundle")) {
                hasBundleConstructor = true;
                break;
            }
        }
    }
    if (constructors.length > 0 && !hasDefaultConstructor && !hasBundleConstructor) {
        String message = String.format("This Controller needs to have either a public default constructor or a" + " public single-argument constructor that takes a Bundle. (`%1$s`)", declaration.getQualifiedName());
        context.report(ISSUE, declaration, context.getLocation(declaration), message);
    }
}
Also used : PsiParameter(com.intellij.psi.PsiParameter) PsiMethod(com.intellij.psi.PsiMethod) JavaEvaluator(com.android.tools.lint.client.api.JavaEvaluator)

Example 2 with PsiParameter

use of com.intellij.psi.PsiParameter in project intellij-community by JetBrains.

the class GroovyGeneratePropertyMissingHandler method chooseOriginalMembers.

@Nullable
@Override
protected ClassMember[] chooseOriginalMembers(PsiClass aClass, Project project) {
    final PsiMethod[] missings = aClass.findMethodsByName("propertyMissing", true);
    PsiMethod getter = null;
    PsiMethod setter = null;
    for (PsiMethod missing : missings) {
        final PsiParameter[] parameters = missing.getParameterList().getParameters();
        if (parameters.length == 1) {
            if (isNameParam(parameters[0])) {
                getter = missing;
            }
        } else if (parameters.length == 2) {
            if (isNameParam(parameters[0])) {
                setter = missing;
            }
        }
    }
    if (setter != null && getter != null) {
        String text = GroovyCodeInsightBundle.message("generate.property.missing.already.defined.warning");
        if (Messages.showYesNoDialog(project, text, GroovyCodeInsightBundle.message("generate.property.missing.already.defined.title"), Messages.getQuestionIcon()) == Messages.YES) {
            final PsiMethod finalGetter = getter;
            final PsiMethod finalSetter = setter;
            if (!ApplicationManager.getApplication().runWriteAction(new Computable<Boolean>() {

                @Override
                public Boolean compute() {
                    try {
                        finalSetter.delete();
                        finalGetter.delete();
                        return Boolean.TRUE;
                    } catch (IncorrectOperationException e) {
                        LOG.error(e);
                        return Boolean.FALSE;
                    }
                }
            }).booleanValue()) {
                return null;
            }
        } else {
            return null;
        }
    }
    return new ClassMember[1];
}
Also used : PsiParameter(com.intellij.psi.PsiParameter) PsiMethod(com.intellij.psi.PsiMethod) IncorrectOperationException(com.intellij.util.IncorrectOperationException) ClassMember(com.intellij.codeInsight.generation.ClassMember) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with PsiParameter

use of com.intellij.psi.PsiParameter in project intellij-community by JetBrains.

the class GrInplaceParameterIntroducer method updateTitle.

@Override
protected void updateTitle(@Nullable GrVariable variable, String value) {
    if (getPreviewEditor() == null || variable == null)
        return;
    final PsiElement declarationScope = ((PsiParameter) variable).getDeclarationScope();
    if (declarationScope instanceof PsiMethod) {
        final PsiMethod psiMethod = (PsiMethod) declarationScope;
        final StringBuilder buf = new StringBuilder();
        buf.append(psiMethod.getName()).append(" (");
        boolean frst = true;
        final List<TextRange> ranges2Remove = new ArrayList<>();
        TextRange addedRange = null;
        int i = 0;
        for (PsiParameter parameter : psiMethod.getParameterList().getParameters()) {
            if (frst) {
                frst = false;
            } else {
                buf.append(", ");
            }
            int startOffset = buf.length();
            /*if (myMustBeFinal || myPanel.isGenerateFinal()) {
          buf.append("final ");
        }*/
            buf.append(parameter.getType().getPresentableText()).append(" ").append(variable == parameter ? value : parameter.getName());
            int endOffset = buf.length();
            if (variable == parameter) {
                addedRange = new TextRange(startOffset, endOffset);
            } else if (myParametersToRemove.contains(i)) {
                ranges2Remove.add(new TextRange(startOffset, endOffset));
            }
            i++;
        }
        assert addedRange != null;
        buf.append(")");
        setPreviewText(buf.toString());
        final MarkupModel markupModel = DocumentMarkupModel.forDocument(getPreviewEditor().getDocument(), myProject, true);
        markupModel.removeAllHighlighters();
        for (TextRange textRange : ranges2Remove) {
            markupModel.addRangeHighlighter(textRange.getStartOffset(), textRange.getEndOffset(), 0, getTestAttributesForRemoval(), HighlighterTargetArea.EXACT_RANGE);
        }
        markupModel.addRangeHighlighter(addedRange.getStartOffset(), addedRange.getEndOffset(), 0, getTextAttributesForAdd(), HighlighterTargetArea.EXACT_RANGE);
    //revalidate();
    }
}
Also used : PsiParameter(com.intellij.psi.PsiParameter) PsiMethod(com.intellij.psi.PsiMethod) ArrayList(java.util.ArrayList) TIntArrayList(gnu.trove.TIntArrayList) TextRange(com.intellij.openapi.util.TextRange) DocumentMarkupModel(com.intellij.openapi.editor.impl.DocumentMarkupModel) MarkupModel(com.intellij.openapi.editor.markup.MarkupModel) PsiElement(com.intellij.psi.PsiElement)

Example 4 with PsiParameter

use of com.intellij.psi.PsiParameter in project intellij-community by JetBrains.

the class LocalVarAnalyzer method searchForVarsToWrap.

public static Result searchForVarsToWrap(GroovyPsiElement root, Result analyzedVars, ExpressionContext context) {
    LocalVarAnalyzer visitor = new LocalVarAnalyzer();
    root.accept(visitor);
    Map<PsiVariable, String> varToName = analyzedVars == null ? new HashMap<>() : analyzedVars.varToName;
    Set<PsiVariable> toWrap = analyzedVars == null ? new HashSet<>() : analyzedVars.toWrap;
    Set<PsiVariable> toMakeFinal = analyzedVars == null ? new HashSet<>() : analyzedVars.toMakeFinal;
    for (PsiVariable v : visitor.touched) {
        if (visitor.rewritten.contains(v)) {
            toWrap.add(v);
            if (v instanceof PsiParameter) {
                varToName.put(v, GenerationUtil.suggestVarName(v.getType(), root, context));
            } else {
                varToName.put(v, v.getName());
            }
        } else {
            toMakeFinal.add(v);
            varToName.put(v, v.getName());
        }
    }
    return analyzedVars == null ? new Result(toMakeFinal, toWrap, varToName) : analyzedVars;
}
Also used : PsiParameter(com.intellij.psi.PsiParameter) PsiVariable(com.intellij.psi.PsiVariable)

Example 5 with PsiParameter

use of com.intellij.psi.PsiParameter in project intellij-community by JetBrains.

the class ChainCompletionLookupElementUtil method fillMethodParameters.

public static String fillMethodParameters(final PsiMethod method, @Nullable final TIntObjectHashMap<SubLookupElement> replaceElements) {
    final TIntObjectHashMap<SubLookupElement> notNullReplaceElements = replaceElements == null ? new TIntObjectHashMap<>(0) : replaceElements;
    final PsiParameter[] parameters = method.getParameterList().getParameters();
    final StringBuilder sb = new StringBuilder();
    for (int i = 0; i < parameters.length; i++) {
        if (i != 0) {
            sb.append(", ");
        }
        final PsiParameter parameter = parameters[i];
        final SubLookupElement replaceElement = notNullReplaceElements.get(i);
        if (replaceElement != null) {
            sb.append(replaceElement.getInsertString());
        } else {
            sb.append(parameter.getName());
        }
    }
    return sb.toString();
}
Also used : PsiParameter(com.intellij.psi.PsiParameter) SubLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.sub.SubLookupElement)

Aggregations

PsiParameter (com.intellij.psi.PsiParameter)26 PsiMethod (com.intellij.psi.PsiMethod)6 PsiElement (com.intellij.psi.PsiElement)5 PsiType (com.intellij.psi.PsiType)5 ArrayList (java.util.ArrayList)5 PsiParameterList (com.intellij.psi.PsiParameterList)4 Nullable (org.jetbrains.annotations.Nullable)3 ClassMember (com.intellij.codeInsight.generation.ClassMember)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiField (com.intellij.psi.PsiField)2 PsiPrimitiveType (com.intellij.psi.PsiPrimitiveType)2 PsiVariable (com.intellij.psi.PsiVariable)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (com.android.annotations.Nullable)1 ResourceUrl (com.android.ide.common.resources.ResourceUrl)1 ResourceType (com.android.resources.ResourceType)1 Location (com.android.tools.klint.detector.api.Location)1 JavaEvaluator (com.android.tools.lint.client.api.JavaEvaluator)1 AddAnnotationPsiFix (com.intellij.codeInsight.intention.AddAnnotationPsiFix)1