Search in sources :

Example 1 with GrLightParameter

use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightParameter in project intellij-community by JetBrains.

the class GradleResolverUtil method createMethodWithClosure.

@Nullable
public static GrLightMethodBuilder createMethodWithClosure(@NotNull String name, @Nullable String returnType, @Nullable String closureTypeParameter, @NotNull PsiElement place) {
    PsiClassType closureType;
    PsiClass closureClass = JavaPsiFacade.getInstance(place.getProject()).findClass(GroovyCommonClassNames.GROOVY_LANG_CLOSURE, place.getResolveScope());
    if (closureClass == null)
        return null;
    if (closureClass.getTypeParameters().length != 1) {
        GradleLog.LOG.debug(String.format("Unexpected type parameters found for closureClass(%s) : (%s)", closureClass, Arrays.toString(closureClass.getTypeParameters())));
        return null;
    }
    PsiElementFactory factory = JavaPsiFacade.getElementFactory(place.getManager().getProject());
    if (closureTypeParameter != null) {
        PsiClassType closureClassTypeParameter = factory.createTypeByFQClassName(closureTypeParameter, place.getResolveScope());
        closureType = factory.createType(closureClass, closureClassTypeParameter);
    } else {
        PsiClassType closureClassTypeParameter = factory.createTypeByFQClassName(CommonClassNames.JAVA_LANG_OBJECT, place.getResolveScope());
        closureType = factory.createType(closureClass, closureClassTypeParameter);
    }
    GrLightMethodBuilder methodWithClosure = new GrLightMethodBuilder(place.getManager(), name);
    GrLightParameter closureParameter = new GrLightParameter("closure", closureType, methodWithClosure);
    methodWithClosure.addParameter(closureParameter);
    PsiClassType retType = factory.createTypeByFQClassName(returnType != null ? returnType : CommonClassNames.JAVA_LANG_OBJECT, place.getResolveScope());
    methodWithClosure.setReturnType(retType);
    methodWithClosure.setContainingClass(retType.resolve());
    return methodWithClosure;
}
Also used : GrLightParameter(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightParameter) GrLightMethodBuilder(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightMethodBuilder) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with GrLightParameter

use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightParameter in project intellij-community by JetBrains.

the class StreamingJsonBuilderContributor method addClosureParameter.

protected void addClosureParameter(GrLightMethodBuilder method) {
    GrLightParameter closureParam = method.addAndGetParameter("closure", GROOVY_LANG_CLOSURE);
    closureParam.putUserData(DELEGATES_TO_KEY, getDelegateClassName());
    closureParam.putUserData(DELEGATES_TO_STRATEGY_KEY, Closure.OWNER_FIRST);
}
Also used : GrLightParameter(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightParameter)

Example 3 with GrLightParameter

use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightParameter in project intellij-community by JetBrains.

the class GroovyFileImpl method getSyntheticArgsParameter.

private GrParameter getSyntheticArgsParameter() {
    GrParameter parameter = mySyntheticArgsParameter;
    if (parameter == null) {
        final PsiType psiType = JavaPsiFacade.getElementFactory(getProject()).createTypeFromText("java.lang.String[]", this);
        parameter = new GrLightParameter(SYNTHETIC_PARAMETER_NAME, psiType, this);
        mySyntheticArgsParameter = parameter;
    }
    return parameter;
}
Also used : GrLightParameter(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightParameter) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 4 with GrLightParameter

use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightParameter in project intellij-community by JetBrains.

the class ResolveUtil method findDuplicate.

public static PsiNamedElement findDuplicate(@NotNull GrVariable variable) {
    if (isScriptField(variable)) {
        final String name = variable.getName();
        final GroovyScriptClass script = (GroovyScriptClass) ((GroovyFile) variable.getContainingFile()).getScriptClass();
        assert script != null;
        List<GrField> duplicates = ContainerUtil.filter(script.getFields(), (GrField f) -> {
            if (!(f instanceof GrScriptField))
                return false;
            if (!name.equals(f.getName()))
                return false;
            if (((GrScriptField) f).getOriginalVariable() == variable)
                return false;
            return true;
        });
        return duplicates.size() > 0 ? duplicates.get(0) : null;
    } else {
        PsiNamedElement duplicate = resolveExistingElement(variable, new DuplicateVariablesProcessor(variable), GrVariable.class);
        final PsiElement context1 = variable.getContext();
        if (duplicate == null && variable instanceof GrParameter && context1 != null) {
            final PsiElement context = context1.getContext();
            if (context instanceof GrClosableBlock || context instanceof GrMethod && !(context.getParent() instanceof GroovyFile) || context instanceof GrTryCatchStatement) {
                duplicate = resolveExistingElement(context.getParent(), new DuplicateVariablesProcessor(variable), GrVariable.class);
            }
        }
        if (duplicate instanceof GrLightParameter && "args".equals(duplicate.getName())) {
            return null;
        } else {
            return duplicate;
        }
    }
}
Also used : GrLightParameter(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightParameter) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrScriptField(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrScriptField) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 5 with GrLightParameter

use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightParameter in project intellij-community by JetBrains.

the class GrInnerClassConstructorUtil method addEnclosingInstanceParam.

@NotNull
public static GrParameter[] addEnclosingInstanceParam(@NotNull GrMethod method, @NotNull PsiClass enclosingClass, @NotNull GrParameter[] originalParams, boolean isOptional) {
    final GrParameter[] parameters = new GrParameter[originalParams.length + 1];
    final PsiClassType enclosingClassType = JavaPsiFacade.getElementFactory(method.getProject()).createType(enclosingClass, PsiSubstitutor.EMPTY);
    final GrLightParameter enclosing = new GrLightParameter("enclosing", enclosingClassType, method);
    if (isOptional) {
        enclosing.setOptional(true);
        enclosing.setInitializerGroovy(GroovyPsiElementFactory.getInstance(method.getProject()).createExpressionFromText("null"));
    }
    parameters[0] = enclosing;
    System.arraycopy(originalParams, 0, parameters, 1, originalParams.length);
    return parameters;
}
Also used : GrLightParameter(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightParameter) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrLightParameter (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightParameter)6 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)3 Nullable (org.jetbrains.annotations.Nullable)2 GrLightMethodBuilder (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightMethodBuilder)2 NotNull (org.jetbrains.annotations.NotNull)1 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)1 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)1 GrScriptField (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrScriptField)1 GroovyScriptClass (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)1