use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass in project intellij-community by JetBrains.
the class ExpressionGenerator method visitReferenceExpression.
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
final GrExpression qualifier = referenceExpression.getQualifier();
final GroovyResolveResult resolveResult = referenceExpression.advancedResolve();
final PsiElement resolved = resolveResult.getElement();
final String referenceName = referenceExpression.getReferenceName();
if (PsiUtil.isThisOrSuperRef(referenceExpression)) {
writeThisOrSuperRef(referenceExpression, qualifier, referenceName);
return;
}
if (ResolveUtil.isClassReference(referenceExpression)) {
// just delegate to qualifier
LOG.assertTrue(qualifier != null);
qualifier.accept(this);
return;
}
if (resolved instanceof PsiClass) {
builder.append(((PsiClass) resolved).getQualifiedName());
if (PsiUtil.isExpressionUsed(referenceExpression)) {
builder.append(".class");
}
return;
}
//don't try to resolve local vars that are provided my this generator (they are listed in myUsedVarNames)
if (resolved == null && qualifier == null && context.myUsedVarNames.contains(referenceName)) {
builder.append(referenceName);
return;
}
//all refs in script that are not resolved are saved in 'binding' of the script
if (qualifier == null && (resolved == null || resolved instanceof GrBindingVariable || resolved instanceof LightElement && !(resolved instanceof ClosureSyntheticParameter)) && (referenceExpression.getParent() instanceof GrIndexProperty || !(referenceExpression.getParent() instanceof GrCall)) && PsiUtil.getContextClass(referenceExpression) instanceof GroovyScriptClass) {
final GrExpression thisExpr = factory.createExpressionFromText("this", referenceExpression);
thisExpr.accept(this);
builder.append(".getBinding().getProperty(\"").append(referenceExpression.getReferenceName()).append("\")");
return;
}
final IElementType type = referenceExpression.getDotTokenType();
GrExpression qualifierToUse = qualifier;
if (type == GroovyTokenTypes.mMEMBER_POINTER) {
LOG.assertTrue(qualifier != null);
builder.append("new ").append(GroovyCommonClassNames.ORG_CODEHAUS_GROOVY_RUNTIME_METHOD_CLOSURE).append('(');
qualifier.accept(this);
builder.append(", \"").append(referenceName).append("\")");
return;
}
if (type == GroovyTokenTypes.mOPTIONAL_DOT) {
LOG.assertTrue(qualifier != null);
String qualifierName = createVarByInitializer(qualifier);
builder.append('(').append(qualifierName).append(" == null ? null : ");
qualifierToUse = factory.createReferenceExpressionFromText(qualifierName, referenceExpression);
}
if (resolveResult.isInvokedOnProperty()) {
//property-style access to accessor (e.g. qual.prop should be translated to qual.getProp())
LOG.assertTrue(resolved instanceof PsiMethod);
LOG.assertTrue(GroovyPropertyUtils.isSimplePropertyGetter((PsiMethod) resolved));
invokeMethodOn(((PsiMethod) resolved), qualifierToUse, GrExpression.EMPTY_ARRAY, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), referenceExpression);
} else {
if (qualifierToUse != null) {
qualifierToUse.accept(this);
builder.append('.');
}
if (resolved instanceof PsiNamedElement && !(resolved instanceof GrBindingVariable)) {
final String refName = ((PsiNamedElement) resolved).getName();
if (resolved instanceof GrVariable && context.analyzedVars.toWrap((GrVariable) resolved)) {
//this var should be wrapped by groovy.lang.Reference. so we add .get() tail.
builder.append(context.analyzedVars.toVarName((GrVariable) resolved));
if (!PsiUtil.isAccessedForWriting(referenceExpression)) {
builder.append(".get()");
}
} else {
builder.append(refName);
}
} else {
//unresolved reference
if (referenceName != null) {
if (PsiUtil.isAccessedForWriting(referenceExpression)) {
builder.append(referenceName);
} else {
PsiType stringType = PsiType.getJavaLangString(referenceExpression.getManager(), referenceExpression.getResolveScope());
PsiType qualifierType = PsiImplUtil.getQualifierType(referenceExpression);
GroovyResolveResult[] candidates = qualifierType != null ? ResolveUtil.getMethodCandidates(qualifierType, "getProperty", referenceExpression, stringType) : GroovyResolveResult.EMPTY_ARRAY;
final PsiElement method = PsiImplUtil.extractUniqueElement(candidates);
if (method != null) {
builder.append("getProperty(\"").append(referenceName).append("\")");
} else {
builder.append(referenceName);
}
}
} else {
final PsiElement nameElement = referenceExpression.getReferenceNameElement();
if (nameElement instanceof GrExpression) {
((GrExpression) nameElement).accept(this);
} else if (nameElement != null) {
builder.append(nameElement.toString());
}
}
}
}
if (type == GroovyTokenTypes.mOPTIONAL_DOT) {
builder.append(')');
}
}
use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass in project intellij-community by JetBrains.
the class GantMemberContributor method processDynamicElements.
@Override
public void processDynamicElements(@NotNull PsiType qualifierType, PsiClass aClass, @NotNull PsiScopeProcessor processor, @NotNull PsiElement place, @NotNull ResolveState state) {
if (aClass != null && ClassUtil.getSuperClassesWithCache(aClass).containsKey("groovy.util.AntBuilder")) {
processAntTasks(processor, place, state);
return;
}
if (!(place instanceof GrReferenceExpression) || ((GrReferenceExpression) place).isQualified()) {
return;
}
GrClosableBlock closure = PsiTreeUtil.getContextOfType(place, GrClosableBlock.class, true);
boolean antTasksProcessed = false;
while (closure != null) {
final PsiElement parent = closure.getParent();
if (parent instanceof GrMethodCall) {
final PsiMethod method = ((GrMethodCall) parent).resolveMethod();
if (method instanceof AntBuilderMethod) {
antTasksProcessed = true;
if (!processAntTasks(processor, place, state)) {
return;
}
if (!((AntBuilderMethod) method).processNestedElements(processor)) {
return;
}
break;
}
}
closure = PsiTreeUtil.getContextOfType(closure, GrClosableBlock.class, true);
}
// ------- gant-specific
PsiFile file = place.getContainingFile();
if (file == null || !GroovyScriptUtil.isSpecificScriptFile(file, GantScriptType.INSTANCE)) {
return;
}
if (aClass instanceof GroovyScriptClass) {
for (GrArgumentLabel label : GantUtils.getScriptTargets((GroovyFile) file)) {
final String targetName = label.getName();
if (targetName != null) {
final PsiNamedElement variable = new LightVariableBuilder(targetName, GroovyCommonClassNames.GROOVY_LANG_CLOSURE, label).setBaseIcon(JetgroovyIcons.Groovy.Gant_target);
if (!ResolveUtil.processElement(processor, variable, state)) {
return;
}
}
}
}
if (!antTasksProcessed) {
processAntTasks(processor, place, state);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass in project intellij-community by JetBrains.
the class FieldAnnotationChecker method checkScriptField.
private static void checkScriptField(AnnotationHolder holder, GrAnnotation annotation) {
final PsiAnnotationOwner owner = annotation.getOwner();
final GrMember container = PsiTreeUtil.getParentOfType(((PsiElement) owner), GrMember.class);
if (container != null) {
if (container.getContainingClass() instanceof GroovyScriptClass) {
holder.createErrorAnnotation(annotation, GroovyBundle.message("annotation.field.can.only.be.used.within.a.script.body"));
} else {
holder.createErrorAnnotation(annotation, GroovyBundle.message("annotation.field.can.only.be.used.within.a.script"));
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass in project intellij-community by JetBrains.
the class GrHighlightUtil method isScriptPropertyAccess.
private static boolean isScriptPropertyAccess(GrReferenceExpression refExpr) {
final GrExpression qualifier = refExpr.getQualifierExpression();
if (qualifier == null) {
final PsiClass clazz = PsiTreeUtil.getParentOfType(refExpr, PsiClass.class);
if (clazz == null) {
//script
return true;
}
//in class, a property should normally be defined, so it's not a declaration
return false;
}
final PsiType type = qualifier.getType();
if (type instanceof PsiClassType && !(qualifier instanceof GrReferenceExpression && ((GrReferenceExpression) qualifier).resolve() instanceof GroovyScriptClass)) {
final PsiClassType classType = (PsiClassType) type;
final PsiClass psiClass = classType.resolve();
if (psiClass instanceof GroovyScriptClass) {
return true;
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass in project intellij-community by JetBrains.
the class GroovyFileImpl method processDeclarationsNoGuess.
private boolean processDeclarationsNoGuess(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, @Nullable PsiElement lastParent, @NotNull PsiElement place) {
ElementClassHint classHint = processor.getHint(ElementClassHint.KEY);
if (myContext != null) {
if (ResolveUtil.shouldProcessProperties(classHint)) {
if (!processChildrenScopes(processor, state, lastParent, place))
return false;
}
return true;
}
boolean processClasses = ResolveUtil.shouldProcessClasses(classHint);
GrImportStatement[] importStatements = getImportStatements();
if (!processImports(processor, state, lastParent, place, importStatements, ImportKind.ALIAS, false))
return false;
GroovyScriptClass scriptClass = getScriptClass();
if (scriptClass != null && StringUtil.isJavaIdentifier(scriptClass.getName())) {
if (!(lastParent instanceof GrTypeDefinition)) {
if (!ResolveUtil.processClassDeclarations(scriptClass, processor, state, lastParent, place))
return false;
}
if (processClasses) {
if (!ResolveUtil.processElement(processor, scriptClass, state))
return false;
}
}
if (processClasses) {
for (GrTypeDefinition definition : getTypeDefinitions()) {
if (!ResolveUtil.processElement(processor, definition, state))
return false;
}
}
if (ResolveUtil.shouldProcessProperties(classHint)) {
if (!processChildrenScopes(processor, state, lastParent, place))
return false;
}
if (!processImports(processor, state, lastParent, place, importStatements, ImportKind.ALIAS, true))
return false;
if (!processImports(processor, state, lastParent, place, importStatements, ImportKind.SIMPLE, null))
return false;
if (!processDeclarationsInPackage(processor, state, lastParent, place))
return false;
if (!processImports(processor, state, lastParent, place, importStatements, ImportKind.ON_DEMAND, null))
return false;
if (!ImplicitImportsKt.processImplicitImports(processor, state, lastParent, place, this))
return false;
if (ResolveUtil.shouldProcessPackages(classHint)) {
NameHint nameHint = processor.getHint(NameHint.KEY);
String expectedName = nameHint != null ? nameHint.getName(state) : null;
final JavaPsiFacade facade = JavaPsiFacade.getInstance(getProject());
if (expectedName != null) {
final PsiPackage pkg = facade.findPackage(expectedName);
if (pkg != null && !processor.execute(pkg, state)) {
return false;
}
} else {
PsiPackage defaultPackage = facade.findPackage("");
if (defaultPackage != null) {
for (PsiPackage subPackage : defaultPackage.getSubPackages(getResolveScope())) {
if (!ResolveUtil.processElement(processor, subPackage, state))
return false;
}
}
}
}
if (ResolveUtil.shouldProcessProperties(classHint)) {
if (lastParent != null && !(lastParent instanceof GrTypeDefinition) && scriptClass != null) {
if (!ResolveUtil.processElement(processor, getSyntheticArgsParameter(), state))
return false;
}
}
return true;
}
Aggregations