use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass in project intellij-community by JetBrains.
the class ResolveUtil method findDuplicate.
public static PsiNamedElement findDuplicate(@NotNull GrVariable variable) {
if (isScriptField(variable)) {
final String name = variable.getName();
final GroovyScriptClass script = (GroovyScriptClass) ((GroovyFile) variable.getContainingFile()).getScriptClass();
assert script != null;
List<GrField> duplicates = ContainerUtil.filter(script.getFields(), (GrField f) -> {
if (!(f instanceof GrScriptField))
return false;
if (!name.equals(f.getName()))
return false;
if (((GrScriptField) f).getOriginalVariable() == variable)
return false;
return true;
});
return duplicates.size() > 0 ? duplicates.get(0) : null;
} else {
PsiNamedElement duplicate = resolveExistingElement(variable, new DuplicateVariablesProcessor(variable), GrVariable.class);
final PsiElement context1 = variable.getContext();
if (duplicate == null && variable instanceof GrParameter && context1 != null) {
final PsiElement context = context1.getContext();
if (context instanceof GrClosableBlock || context instanceof GrMethod && !(context.getParent() instanceof GroovyFile) || context instanceof GrTryCatchStatement) {
duplicate = resolveExistingElement(context.getParent(), new DuplicateVariablesProcessor(variable), GrVariable.class);
}
}
if (duplicate instanceof GrLightParameter && "args".equals(duplicate.getName())) {
return null;
} else {
return duplicate;
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass in project intellij-community by JetBrains.
the class BaseScriptTransformationSupport method applyTransformation.
@Override
public void applyTransformation(@NotNull TransformationContext context) {
if (!(context.getCodeClass() instanceof GroovyScriptClass))
return;
GroovyScriptClass scriptClass = (GroovyScriptClass) context.getCodeClass();
LightMethodBuilder mainMethod = new LightMethodBuilder(scriptClass.getManager(), GroovyLanguage.INSTANCE, "main").setMethodReturnType(PsiType.VOID).addParameter("args", new PsiArrayType(PsiType.getJavaLangString(scriptClass.getManager(), scriptClass.getResolveScope()))).addModifiers(PsiModifier.PUBLIC, PsiModifier.STATIC);
LightMethodBuilder runMethod = new LightMethodBuilder(scriptClass.getManager(), GroovyLanguage.INSTANCE, "run").setMethodReturnType(TypesUtil.getJavaLangObject(scriptClass)).addModifier(PsiModifier.PUBLIC);
context.addMethod(runMethod, true);
context.addMethod(mainMethod, true);
context.setSuperType(getBaseClassType(scriptClass));
}
use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass in project intellij-community by JetBrains.
the class GroovyScriptRunConfigurationProducer method createConfigurationByElement.
@Override
protected RunnerAndConfigurationSettings createConfigurationByElement(final Location location, final ConfigurationContext context) {
final PsiElement element = location.getPsiElement();
final PsiFile file = element.getContainingFile();
if (!(file instanceof GroovyFile)) {
return null;
}
GroovyFile groovyFile = (GroovyFile) file;
final PsiClass aClass = GroovyRunnerPsiUtil.getRunningClass(location.getPsiElement());
if (aClass instanceof GroovyScriptClass || GroovyRunnerPsiUtil.isRunnable(aClass)) {
final RunnerAndConfigurationSettings settings = createConfiguration(aClass);
if (settings != null) {
mySourceElement = element;
final GroovyScriptRunConfiguration configuration = (GroovyScriptRunConfiguration) settings.getConfiguration();
GroovyScriptUtil.getScriptType(groovyFile).tuneConfiguration(groovyFile, configuration, location);
return settings;
}
}
if (file.getText().contains("@Grab")) {
ApplicationConfigurationProducer producer = new ApplicationConfigurationProducer();
ConfigurationFromContext settings = producer.createConfigurationFromContext(context);
if (settings != null) {
PsiElement src = settings.getSourceElement();
mySourceElement = src;
return createConfiguration(src instanceof PsiMethod ? ((PsiMethod) src).getContainingClass() : (PsiClass) src);
}
return null;
} else {
return null;
}
}
use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass in project intellij-community by JetBrains.
the class ReachingDefinitionsCollector method filterNonlocals.
private static VariableInfo[] filterNonlocals(Map<String, VariableInfo> infos, GrStatement place) {
List<VariableInfo> result = new ArrayList<>();
for (Iterator<VariableInfo> iterator = infos.values().iterator(); iterator.hasNext(); ) {
VariableInfo info = iterator.next();
String name = info.getName();
GroovyPsiElement property = ResolveUtil.resolveProperty(place, name);
if (property instanceof GrVariable) {
iterator.remove();
} else if (property instanceof GrReferenceExpression) {
GrMember member = PsiTreeUtil.getParentOfType(property, GrMember.class);
if (member == null) {
continue;
} else if (!member.hasModifierProperty(PsiModifier.STATIC)) {
if (member.getContainingClass() instanceof GroovyScriptClass) {
//binding variable
continue;
}
}
}
if (ResolveUtil.resolveClass(place, name) == null) {
result.add(info);
}
}
return result.toArray(new VariableInfo[result.size()]);
}
use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass in project intellij-community by JetBrains.
the class GroovyFileImpl method getScriptClass.
@Override
public GroovyScriptClass getScriptClass() {
if (!isScript()) {
return null;
}
GroovyScriptClass aClass = myScriptClass;
if (aClass == null) {
aClass = new GroovyScriptClass(this);
myScriptClass = aClass;
}
return aClass;
}
Aggregations