use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class GroovyGenerateConstructorHandler method chooseOriginalMembers.
@Override
@Nullable
protected ClassMember[] chooseOriginalMembers(PsiClass aClass, Project project) {
final ClassMember[] classMembers = chooseOriginalMembersImpl(aClass, project);
if (classMembers == null)
return null;
List<ClassMember> res = new ArrayList<>();
final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
for (ClassMember classMember : classMembers) {
if (classMember instanceof PsiMethodMember) {
final PsiMethod method = ((PsiMethodMember) classMember).getElement();
PsiMethod copy = factory.createMethodFromText(GroovyToJavaGenerator.generateMethodStub(method), method);
if (method instanceof GrMethod) {
GrParameter[] parameters = ((GrMethod) method).getParameterList().getParameters();
PsiParameter[] copyParameters = copy.getParameterList().getParameters();
for (int i = 0; i < parameters.length; i++) {
if (parameters[i].getTypeElementGroovy() == null) {
copyParameters[i].setName(DEF_PSEUDO_ANNO + parameters[i].getName());
}
}
}
res.add(new PsiMethodMember(copy));
} else if (classMember instanceof PsiFieldMember) {
final PsiField field = ((PsiFieldMember) classMember).getElement();
String prefix = field instanceof GrField && ((GrField) field).getTypeElementGroovy() == null ? DEF_PSEUDO_ANNO : "";
res.add(new PsiFieldMember(factory.createFieldFromText(field.getType().getCanonicalText() + " " + prefix + field.getName(), aClass)));
}
}
return res.toArray(new ClassMember[res.size()]);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class GroovyValidationUtil method validateNewParameterName.
public static boolean validateNewParameterName(GrParameter variable, MultiMap<PsiElement, String> conflicts, @NotNull String varName) {
GrParameterList list = PsiTreeUtil.getParentOfType(variable, GrParameterList.class);
GrParametersOwner owner = PsiTreeUtil.getParentOfType(variable, GrParametersOwner.class);
assert owner != null;
for (GrParameter parameter : list.getParameters()) {
if (parameter.equals(variable))
continue;
validateVariableOccurrencesDownImpl(parameter, conflicts, varName);
}
validateVariableOccurrencesDown(owner, list, conflicts, varName);
PsiElement parent = owner.getParent();
validateVariableOccurrencesUp(parent, owner, conflicts, varName, parent instanceof GroovyFile);
return conflicts.isEmpty();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class GroovyValidationUtil method validateVariableOccurrencesUp.
private static void validateVariableOccurrencesUp(PsiElement parent, PsiElement lastParent, MultiMap<PsiElement, String> conflicts, @NotNull String varName, final boolean containerIsFile) {
if (!containerIsFile && (parent instanceof PsiFile) || parent == null)
return;
PsiElement child = parent.getFirstChild();
while (child != null && child != lastParent) {
// Upper variable declarations
if (child instanceof GrVariableDeclaration) {
for (GrVariable variable : ((GrVariableDeclaration) child).getVariables()) {
if (varName.equals(variable.getName())) {
addConflict(varName, variable, conflicts);
}
}
}
child = child.getNextSibling();
}
if (parent instanceof GrParametersOwner) {
//method or closure parameters
GrParametersOwner owner = (GrParametersOwner) parent;
for (GrParameter parameter : owner.getParameters()) {
if (varName.equals(parameter.getName())) {
addConflict(varName, parameter, conflicts);
}
}
} else if (parent instanceof GrForStatement) {
// For statement binding
GrForStatement statement = (GrForStatement) parent;
GrForClause clause = statement.getClause();
if (clause != null) {
final GrVariable variable = clause.getDeclaredVariable();
if (variable != null && varName.equals(variable.getName())) {
addConflict(varName, variable, conflicts);
}
}
}
if (parent instanceof PsiFile)
return;
validateVariableOccurrencesUp(parent.getParent(), parent, conflicts, varName, false);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class GrChangeSignatureConflictSearcher method addMethodConflicts.
private void addMethodConflicts(MultiMap<PsiElement, String> conflicts) {
try {
GrMethod prototype;
final PsiMethod method = myChangeInfo.getMethod();
if (!(method instanceof GrMethod))
return;
PsiManager manager = method.getManager();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(manager.getProject());
final CanonicalTypes.Type returnType = myChangeInfo.getNewReturnType();
String newMethodName = myChangeInfo.getNewName();
if (method.isConstructor()) {
prototype = factory.createConstructorFromText("foo", ArrayUtil.EMPTY_STRING_ARRAY, ArrayUtil.EMPTY_STRING_ARRAY, "{}", method);
} else {
prototype = factory.createMethodFromText("", "foo", returnType != null ? returnType.getTypeText() : null, ArrayUtil.EMPTY_STRING_ARRAY, method);
}
prototype.setName(newMethodName);
JavaParameterInfo[] parameters = myChangeInfo.getNewParameters();
for (JavaParameterInfo info : parameters) {
GrParameter param;
if (info instanceof GrParameterInfo) {
param = factory.createParameter(info.getName(), info.getTypeText(), ((GrParameterInfo) info).getDefaultInitializer(), (GroovyPsiElement) method);
} else {
param = factory.createParameter(info.getName(), info.getTypeText(), (GroovyPsiElement) method);
}
prototype.getParameterList().add(param);
}
ConflictsUtil.checkMethodConflicts(method.getContainingClass(), method, prototype, conflicts);
GrMethodConflictUtil.checkMethodConflicts(method.getContainingClass(), prototype, ((GrMethod) method), conflicts, true);
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class GroovyOverrideImplementUtil method setupTraitMethodBody.
private static void setupTraitMethodBody(Project project, GrMethod resultMethod, GrTraitMethod traitMethod) {
PsiClass traitClass = traitMethod.getPrototype().getContainingClass();
StringBuilder builder = new StringBuilder();
builder.append("\nreturn ");
builder.append(traitClass.getQualifiedName());
builder.append(".super.");
builder.append(traitMethod.getName());
builder.append("(");
GrParameter[] parameters = resultMethod.getParameters();
for (GrParameter parameter : parameters) {
builder.append(parameter.getName()).append(",");
}
if (parameters.length > 0) {
builder.replace(builder.length() - 1, builder.length(), ")\n");
} else {
builder.append(")\n");
}
GroovyFile file = GroovyPsiElementFactory.getInstance(project).createGroovyFile(builder, false, null);
GrOpenBlock block = resultMethod.getBlock();
block.getNode().addChildren(file.getFirstChild().getNode(), null, block.getRBrace().getNode());
}
Aggregations