use of org.codehaus.groovy.control.SourceUnit in project groovy by apache.
the class ClassCodeVisitorSupport method addError.
public void addError(String msg, ASTNode expr) {
SourceUnit source = getSourceUnit();
source.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage(new SyntaxException(msg + '\n', expr.getLineNumber(), expr.getColumnNumber(), expr.getLastLineNumber(), expr.getLastColumnNumber()), source));
}
use of org.codehaus.groovy.control.SourceUnit in project groovy by apache.
the class Compiler method compile.
/**
* Compiles a string of code.
*/
public void compile(String name, String code) throws CompilationFailedException {
CompilationUnit unit = new CompilationUnit(configuration);
unit.addSource(new SourceUnit(name, code, configuration, unit.getClassLoader(), unit.getErrorCollector()));
unit.compile();
}
use of org.codehaus.groovy.control.SourceUnit in project groovy by apache.
the class TraitASTTransformation method registerASTTranformations.
private void registerASTTranformations(final ClassNode helper) {
ASTTransformationCollectorCodeVisitor collector = new ASTTransformationCollectorCodeVisitor(unit, compilationUnit.getTransformLoader());
collector.visitClass(helper);
// Perform an additional phase which has to be done *after* type checking
compilationUnit.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() {
@Override
public void call(final SourceUnit source, final GeneratorContext context, final ClassNode classNode) throws CompilationFailedException {
if (classNode == helper) {
PostTypeCheckingExpressionReplacer replacer = new PostTypeCheckingExpressionReplacer(source);
replacer.visitClass(helper);
}
}
}, CompilePhase.INSTRUCTION_SELECTION.getPhaseNumber());
}
use of org.codehaus.groovy.control.SourceUnit in project groovy by apache.
the class ProxyGeneratorAdapter method adjustSuperClass.
private Class adjustSuperClass(Class superClass, final Class[] interfaces) {
boolean isSuperClassAnInterface = superClass.isInterface();
if (!isSuperClassAnInterface) {
return superClass;
}
Class result = Object.class;
Set<ClassNode> traits = new LinkedHashSet<ClassNode>();
// check if it's a trait
collectTraits(superClass, traits);
if (interfaces != null) {
for (Class anInterface : interfaces) {
collectTraits(anInterface, traits);
}
}
if (!traits.isEmpty()) {
String name = superClass.getName() + "$TraitAdapter";
ClassNode cn = new ClassNode(name, ACC_PUBLIC | ACC_ABSTRACT, ClassHelper.OBJECT_TYPE, traits.toArray(new ClassNode[traits.size()]), null);
CompilationUnit cu = new CompilationUnit(loader);
CompilerConfiguration config = new CompilerConfiguration();
SourceUnit su = new SourceUnit(name + "wrapper", "", config, loader, new ErrorCollector(config));
cu.addSource(su);
cu.compile(Phases.CONVERSION);
su.getAST().addClass(cn);
cu.compile(Phases.CLASS_GENERATION);
@SuppressWarnings("unchecked") List<GroovyClass> classes = (List<GroovyClass>) cu.getClasses();
for (GroovyClass groovyClass : classes) {
if (groovyClass.getName().equals(name)) {
return loader.defineClass(name, groovyClass.getBytes());
}
}
}
return result;
}
use of org.codehaus.groovy.control.SourceUnit in project groovy by apache.
the class ASTTest method getAST.
public ModuleNode getAST(String source, int untilPhase) {
SourceUnit unit = SourceUnit.create("Test", source);
CompilationUnit compUnit = new CompilationUnit();
compUnit.addSource(unit);
compUnit.compile(untilPhase);
return unit.getAST();
}
Aggregations