use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrReferenceExpressionImpl method bindToElementViaStaticImport.
@Override
public GrReferenceExpression bindToElementViaStaticImport(@NotNull PsiMember member) {
if (getQualifier() != null) {
throw new IncorrectOperationException("Reference has qualifier");
}
if (StringUtil.isEmpty(getReferenceName())) {
throw new IncorrectOperationException("Reference has empty name");
}
PsiClass containingClass = member.getContainingClass();
if (containingClass == null) {
throw new IncorrectOperationException("Member has no containing class");
}
final PsiFile file = getContainingFile();
if (file instanceof GroovyFile) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
String text = "import static " + containingClass.getQualifiedName() + "." + member.getName();
final GrImportStatement statement = factory.createImportStatementFromText(text);
((GroovyFile) file).addImport(statement);
}
return this;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrEnumConstantImpl method addNamedArgument.
@Override
public GrNamedArgument addNamedArgument(final GrNamedArgument namedArgument) throws IncorrectOperationException {
GrArgumentList list = getArgumentList();
assert list != null;
if (list.getText().trim().isEmpty()) {
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
final GrArgumentList newList = factory.createArgumentList();
list = (GrArgumentList) list.replace(newList);
}
return list.addNamedArgument(namedArgument);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrClassReferenceTypePointer method calcType.
@Nullable
@Override
protected GrClassReferenceType calcType() {
final GrReferenceElement reference = mySmartPsiElementPointer.getElement();
if (reference != null) {
return new GrClassReferenceType(reference);
}
try {
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject);
GrTypeElement typeElement = factory.createTypeElement(myReferenceText, null);
return (GrClassReferenceType) typeElement.getType();
} catch (IncorrectOperationException e) {
LOG.error(e);
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GroovyPropertyUtils method generateSetterPrototype.
public static GrMethod generateSetterPrototype(PsiField field) {
Project project = field.getProject();
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
String name = field.getName();
boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC);
VariableKind kind = codeStyleManager.getVariableKind(field);
String propertyName = codeStyleManager.variableNameToPropertyName(name, kind);
String setName = getSetterName(field.getName());
final PsiClass containingClass = field.getContainingClass();
try {
GrMethod setMethod = factory.createMethod(setName, PsiType.VOID);
String parameterName = codeStyleManager.propertyNameToVariableName(propertyName, VariableKind.PARAMETER);
final PsiType type = field instanceof GrField ? ((GrField) field).getDeclaredType() : field.getType();
GrParameter param = factory.createParameter(parameterName, type);
annotateWithNullableStuff(field, param);
setMethod.getParameterList().add(param);
PsiUtil.setModifierProperty(setMethod, PsiModifier.STATIC, isStatic);
@NonNls StringBuilder builder = new StringBuilder();
if (name.equals(parameterName)) {
if (!isStatic) {
builder.append("this.");
} else {
String className = containingClass.getName();
if (className != null) {
builder.append(className);
builder.append(".");
}
}
}
builder.append(name);
builder.append("=");
builder.append(parameterName);
builder.append("\n");
GrCodeBlock body = factory.createMethodBodyFromText(builder.toString());
setMethod.getBlock().replace(body);
return setMethod;
} catch (IncorrectOperationException e) {
LOG.error(e);
return null;
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrLightAnnotation method addAttribute.
public void addAttribute(PsiNameValuePair pair) {
if (pair instanceof GrAnnotationNameValuePair) {
myAnnotationArgList.addAttribute((GrAnnotationNameValuePair) pair);
} else {
GrAnnotationMemberValue newValue = new AnnotationArgConverter().convert(pair.getValue());
if (newValue == null)
return;
String name = pair.getName();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(pair.getProject());
String annotationText;
annotationText = name != null ? "@A(" + name + "=" + newValue.getText() + ")" : "@A(" + newValue.getText() + ")";
GrAnnotation annotation = factory.createAnnotationFromText(annotationText);
myAnnotationArgList.addAttribute(annotation.getParameterList().getAttributes()[0]);
}
}
Aggregations