use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class ExpressionGenerator method visitNewExpression.
@Override
public void visitNewExpression(@NotNull GrNewExpression newExpression) {
boolean hasFieldInitialization = hasFieldInitialization(newExpression);
StringBuilder builder;
final PsiType type = newExpression.getType();
final String varName;
if (hasFieldInitialization) {
builder = new StringBuilder();
varName = GenerationUtil.suggestVarName(type, newExpression, context);
TypeWriter.writeType(builder, type, newExpression);
builder.append(' ').append(varName).append(" = ");
} else {
varName = null;
builder = this.builder;
}
final GrTypeElement typeElement = newExpression.getTypeElement();
final GrArrayDeclaration arrayDeclaration = newExpression.getArrayDeclaration();
final GrCodeReferenceElement referenceElement = newExpression.getReferenceElement();
builder.append("new ");
if (typeElement != null) {
final PsiType builtIn = typeElement.getType();
LOG.assertTrue(builtIn instanceof PsiPrimitiveType);
final PsiType boxed = TypesUtil.boxPrimitiveType(builtIn, newExpression.getManager(), newExpression.getResolveScope());
TypeWriter.writeTypeForNew(builder, boxed, newExpression);
} else if (referenceElement != null) {
GenerationUtil.writeCodeReferenceElement(builder, referenceElement);
}
final GrArgumentList argList = newExpression.getArgumentList();
if (argList != null) {
GrClosureSignature signature = null;
final GroovyResolveResult resolveResult = newExpression.advancedResolve();
final PsiElement constructor = resolveResult.getElement();
if (constructor instanceof PsiMethod) {
signature = GrClosureSignatureUtil.createSignature((PsiMethod) constructor, resolveResult.getSubstitutor());
} else if (referenceElement != null) {
final GroovyResolveResult clazzResult = referenceElement.advancedResolve();
final PsiElement clazz = clazzResult.getElement();
if (clazz instanceof PsiClass && ((PsiClass) clazz).getConstructors().length == 0) {
signature = GrClosureSignatureUtil.createSignature(PsiParameter.EMPTY_ARRAY, null);
}
}
final GrNamedArgument[] namedArgs = hasFieldInitialization ? GrNamedArgument.EMPTY_ARRAY : argList.getNamedArguments();
new ArgumentListGenerator(builder, context).generate(signature, argList.getExpressionArguments(), namedArgs, GrClosableBlock.EMPTY_ARRAY, newExpression);
}
final GrAnonymousClassDefinition anonymous = newExpression.getAnonymousClassDefinition();
if (anonymous != null) {
writeTypeBody(builder, anonymous);
}
if (arrayDeclaration != null) {
final GrExpression[] boundExpressions = arrayDeclaration.getBoundExpressions();
for (GrExpression boundExpression : boundExpressions) {
builder.append('[');
boundExpression.accept(this);
builder.append(']');
}
if (boundExpressions.length == 0) {
builder.append("[]");
}
}
if (hasFieldInitialization) {
builder.append(';');
context.myStatements.add(builder.toString());
final GrNamedArgument[] namedArguments = argList.getNamedArguments();
for (GrNamedArgument namedArgument : namedArguments) {
final String fieldName = namedArgument.getLabelName();
if (fieldName == null) {
//todo try to initialize field
final GrArgumentLabel label = namedArgument.getLabel();
LOG.info("cannot initialize field " + (label == null ? "<null>" : label.getText()));
} else {
final GroovyResolveResult resolveResult = referenceElement.advancedResolve();
final PsiElement resolved = resolveResult.getElement();
LOG.assertTrue(resolved instanceof PsiClass);
initializeField(varName, type, ((PsiClass) resolved), resolveResult.getSubstitutor(), fieldName, namedArgument.getExpression());
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class AnnotationGenerator method visitAnnotation.
@Override
public void visitAnnotation(@NotNull GrAnnotation annotation) {
builder.append('@');
GrCodeReferenceElement classReference = annotation.getClassReference();
GenerationUtil.writeCodeReferenceElement(builder, classReference);
GrAnnotationArgumentList parameterList = annotation.getParameterList();
GrAnnotationNameValuePair[] attributes = parameterList.getAttributes();
if (attributes.length == 0)
return;
builder.append('(');
for (GrAnnotationNameValuePair attribute : attributes) {
String name = attribute.getName();
if (name != null) {
builder.append(name);
builder.append(" = ");
}
GrAnnotationMemberValue value = attribute.getValue();
if (value != null) {
value.accept(this);
}
builder.append(", ");
}
builder.delete(builder.length() - 2, builder.length());
//builder.removeFromTheEnd(2);
builder.append(')');
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class GenerationUtil method writeThisReference.
public static void writeThisReference(@Nullable PsiClass targetClass, StringBuilder buffer, ExpressionContext context) {
if (targetClass != null && !(targetClass instanceof PsiAnonymousClass)) {
final GrCodeReferenceElement ref = GroovyPsiElementFactory.getInstance(context.project).createCodeReferenceElementFromClass(targetClass);
writeCodeReferenceElement(buffer, ref);
buffer.append('.');
}
buffer.append("this");
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class GroovyTypeCheckVisitor method visitNewExpression.
@Override
public void visitNewExpression(@NotNull GrNewExpression newExpression) {
super.visitNewExpression(newExpression);
if (newExpression.getArrayCount() > 0)
return;
GrCodeReferenceElement refElement = newExpression.getReferenceElement();
if (refElement == null)
return;
GrNewExpressionInfo info = new GrNewExpressionInfo(newExpression);
processConstructorCall(info);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class GrRefactoringConflictsUtil method analyzeModuleConflicts.
public static void analyzeModuleConflicts(final Project project, final Collection<? extends GroovyPsiElement> scopes, final UsageInfo[] usages, final VirtualFile vFile, final MultiMap<PsiElement, String> conflicts) {
if (scopes == null)
return;
for (final PsiElement scope : scopes) {
if (scope instanceof PsiPackage)
return;
}
final Module targetModule = ModuleUtilCore.findModuleForFile(vFile, project);
if (targetModule == null)
return;
final GlobalSearchScope resolveScope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(targetModule);
final HashSet<PsiElement> reported = new HashSet<>();
for (final GroovyPsiElement scope : scopes) {
scope.accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitCodeReferenceElement(@NotNull GrCodeReferenceElement refElement) {
super.visitCodeReferenceElement(refElement);
visit(refElement);
}
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression reference) {
super.visitReferenceExpression(reference);
visit(reference);
}
private void visit(GrReferenceElement<? extends GroovyPsiElement> reference) {
final PsiElement resolved = reference.resolve();
if (resolved != null && !reported.contains(resolved) && !CommonRefactoringUtil.isAncestor(resolved, scopes) && !PsiSearchScopeUtil.isInScope(resolveScope, resolved) && !(resolved instanceof LightElement)) {
final String scopeDescription = RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(reference), true);
final String message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.in.module.2", RefactoringUIUtil.getDescription(resolved, true), scopeDescription, CommonRefactoringUtil.htmlEmphasize(targetModule.getName()));
conflicts.putValue(resolved, CommonRefactoringUtil.capitalize(message));
reported.add(resolved);
}
}
});
}
boolean isInTestSources = ModuleRootManager.getInstance(targetModule).getFileIndex().isInTestSourceContent(vFile);
NextUsage: for (UsageInfo usage : usages) {
final PsiElement element = usage.getElement();
if (element != null && PsiTreeUtil.getParentOfType(element, GrImportStatement.class, false) == null) {
for (PsiElement scope : scopes) {
if (PsiTreeUtil.isAncestor(scope, element, false))
continue NextUsage;
}
final GlobalSearchScope resolveScope1 = element.getResolveScope();
if (!resolveScope1.isSearchInModuleContent(targetModule, isInTestSources)) {
final PsiFile usageFile = element.getContainingFile();
PsiElement container;
if (usageFile instanceof PsiJavaFile) {
container = ConflictsUtil.getContainer(element);
} else {
container = usageFile;
}
final String scopeDescription = RefactoringUIUtil.getDescription(container, true);
final VirtualFile usageVFile = usageFile.getVirtualFile();
if (usageVFile != null) {
Module module = ProjectRootManager.getInstance(project).getFileIndex().getModuleForFile(usageVFile);
if (module != null) {
final String message;
final PsiElement referencedElement;
if (usage instanceof MoveRenameUsageInfo) {
referencedElement = ((MoveRenameUsageInfo) usage).getReferencedElement();
} else {
referencedElement = usage.getElement();
}
assert referencedElement != null : usage;
if (module == targetModule && isInTestSources) {
message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.from.production.of.module.2", RefactoringUIUtil.getDescription(referencedElement, true), scopeDescription, CommonRefactoringUtil.htmlEmphasize(module.getName()));
} else {
message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.from.module.2", RefactoringUIUtil.getDescription(referencedElement, true), scopeDescription, CommonRefactoringUtil.htmlEmphasize(module.getName()));
}
conflicts.putValue(referencedElement, CommonRefactoringUtil.capitalize(message));
}
}
}
}
}
}
Aggregations