@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)); }
@Nullable private static PsiClassType getSuperClassTypeFromBaseScriptAnnotatedVariable( GroovyScriptClass scriptClass) { return CachedValuesManager.getCachedValue( scriptClass, () -> CachedValueProvider.Result.create( doGetSuperClassType(scriptClass), scriptClass.getContainingFile())); }
@NotNull private static PsiClassType getBaseClassType(@NotNull GroovyScriptClass scriptClass) { PsiClassType type = getSuperClassTypeFromBaseScriptAnnotatedVariable(scriptClass); if (type != null) return type; final PsiClassType superClassFromDSL = GroovyDslFileIndex.processScriptSuperClasses(scriptClass.getContainingFile()); if (superClassFromDSL != null) return superClassFromDSL; return TypesUtil.createTypeByFQClassName( GroovyCommonClassNames.GROOVY_LANG_SCRIPT, scriptClass); }
private static PsiClassType doGetSuperClassType(GroovyScriptClass scriptClass) { GrVariableDeclaration declaration = findDeclaration(scriptClass.getContainingFile()); if (declaration != null) { GrModifierList modifierList = declaration.getModifierList(); if (modifierList.findAnnotation(GroovyCommonClassNames.GROOVY_TRANSFORM_BASE_SCRIPT) != null) { GrTypeElement typeElement = declaration.getTypeElementGroovy(); if (typeElement != null) { PsiType type = typeElement.getType(); if (type instanceof PsiClassType) { return (PsiClassType) type; } } } } return null; }