use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class GroovyPsiElementFactoryImpl method createTypeElement.
@Override
@NotNull
public GrTypeElement createTypeElement(@NotNull String typeText, @Nullable final PsiElement context) throws IncorrectOperationException {
final GroovyFile file = createGroovyFileChecked("def " + typeText + " someVar", false, context);
GrTopStatement[] topStatements = file.getTopStatements();
if (topStatements == null || topStatements.length == 0)
throw new IncorrectOperationException("can't create type element from:" + typeText);
GrTopStatement statement = topStatements[0];
if (!(statement instanceof GrVariableDeclaration))
throw new IncorrectOperationException("can't create type element from:" + typeText);
GrVariableDeclaration decl = (GrVariableDeclaration) statement;
final GrTypeElement element = decl.getTypeElementGroovy();
if (element == null)
throw new IncorrectOperationException("can't create type element from:" + typeText);
return element;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class PsiImplUtil method isMainMethod.
public static boolean isMainMethod(GrMethod method) {
if (!method.getName().equals(MAIN_METHOD))
return false;
else if (!method.hasModifierProperty(PsiModifier.STATIC))
return false;
final GrParameter[] parameters = method.getParameters();
if (parameters.length == 0)
return false;
if (parameters.length == 1 && parameters[0].getTypeElementGroovy() == null)
return true;
int args_count = 0;
int optional_count = 0;
for (GrParameter p : parameters) {
final GrTypeElement declaredType = p.getTypeElementGroovy();
if ((declaredType == null || declaredType.getType().equalsToText(CommonClassNames.JAVA_LANG_STRING + "[]")) && p.getInitializerGroovy() == null) {
args_count++;
}
if (p.getInitializerGroovy() != null)
optional_count++;
}
return optional_count == parameters.length - 1 && args_count == 1;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class GrReferenceElementImpl method getTypeArguments.
@Override
@NotNull
public PsiType[] getTypeArguments() {
final GrTypeArgumentList typeArgsList = getTypeArgumentList();
if (typeArgsList == null)
return PsiType.EMPTY_ARRAY;
final GrTypeElement[] args = typeArgsList.getTypeArgumentElements();
if (args.length == 0)
return PsiType.EMPTY_ARRAY;
PsiType[] result = PsiType.createArray(args.length);
for (int i = 0; i < result.length; i++) {
result[i] = args[i].getType();
}
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class GrSplitDeclarationIntention method createVarDeclaration.
private static GrStatement createVarDeclaration(Project project, GrVariable variable, String modifiers, boolean isTuple) {
StringBuilder builder = new StringBuilder();
builder.append(modifiers).append(' ');
GrTypeElement typeElement = variable.getTypeElementGroovy();
if (typeElement != null) {
builder.append(typeElement.getText()).append(' ');
}
builder.append(variable.getName());
GrExpression initializer = variable.getInitializerGroovy();
if (initializer != null) {
builder.append('=').append(initializer.getText());
}
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
GrVariableDeclaration decl = (GrVariableDeclaration) factory.createStatementFromText(builder);
if (isTuple && (variable.getDeclaredType() != null || decl.getModifierList().getModifiers().length > 1)) {
decl.getModifierList().setModifierProperty(GrModifier.DEF, false);
}
return decl;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class GrConvertTypeCastToSafeCastIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
if (!(element instanceof GrTypeCastExpression))
return;
GrExpression operand = ((GrTypeCastExpression) element).getOperand();
GrTypeElement type = ((GrTypeCastExpression) element).getCastTypeElement();
if (type == null)
return;
if (operand == null)
return;
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
GrExpression safeCast = factory.createExpressionFromText(operand.getText() + " as " + type.getText());
((GrTypeCastExpression) element).replaceWithExpression(safeCast, true);
}
Aggregations