use of org.codehaus.groovy.ast.CompileUnit in project groovy by apache.
the class CompilationUnit method addPhaseOperations.
private void addPhaseOperations() {
addPhaseOperation(SourceUnit::parse, Phases.PARSING);
addPhaseOperation(source -> {
source.convert();
// add module to compile unit
getAST().addModule(source.getAST());
Optional.ofNullable(getProgressCallback()).ifPresent(callback -> callback.call(source, getPhase()));
}, Phases.CONVERSION);
addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
GroovyClassVisitor visitor = new EnumVisitor(this, source);
visitor.visitClass(classNode);
}, Phases.CONVERSION);
addPhaseOperation(source -> {
for (ClassNode classNode : source.getAST().getClasses()) {
resolveVisitor.setClassNodeResolver(classNodeResolver);
resolveVisitor.startResolving(classNode, source);
}
}, Phases.SEMANTIC_ANALYSIS);
addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
GroovyClassVisitor visitor = new StaticImportVisitor(classNode, source);
visitor.visitClass(classNode);
}, Phases.SEMANTIC_ANALYSIS);
addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
GroovyClassVisitor visitor = new InnerClassVisitor(this, source);
visitor.visitClass(classNode);
}, Phases.SEMANTIC_ANALYSIS);
addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
if (!classNode.isSynthetic()) {
GroovyClassVisitor visitor = new GenericsVisitor(source);
visitor.visitClass(classNode);
}
}, Phases.SEMANTIC_ANALYSIS);
addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
TraitComposer.doExtendTraits(classNode, source, this);
}, Phases.CANONICALIZATION);
addPhaseOperation(source -> {
List<ClassNode> classes = source.getAST().getClasses();
for (ClassNode node : classes) {
CompileUnit cu = node.getCompileUnit();
for (Iterator<String> it = cu.getClassesToCompile().keySet().iterator(); it.hasNext(); ) {
String name = it.next();
StringBuilder message = new StringBuilder();
message.append("Compilation incomplete: expected to find the class ").append(name).append(" in ").append(source.getName());
if (classes.isEmpty()) {
message.append(", but the file seems not to contain any classes");
} else {
message.append(", but the file contains the classes: ");
boolean first = true;
for (ClassNode cn : classes) {
if (first) {
first = false;
} else {
message.append(", ");
}
message.append(cn.getName());
}
}
getErrorCollector().addErrorAndContinue(Message.create(message.toString(), this));
it.remove();
}
}
}, Phases.CANONICALIZATION);
addPhaseOperation(classgen, Phases.CLASS_GENERATION);
addPhaseOperation(groovyClass -> {
String name = groovyClass.getName().replace('.', File.separatorChar) + ".class";
File path = new File(getConfiguration().getTargetDirectory(), name);
// ensure the path is ready for the file
File directory = path.getParentFile();
if (directory != null && !directory.exists()) {
directory.mkdirs();
}
// create the file and write out the data
try (FileOutputStream stream = new FileOutputStream(path)) {
stream.write(groovyClass.getBytes());
} catch (IOException e) {
getErrorCollector().addError(Message.create(e.getMessage(), this));
}
});
addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
AnnotationCollectorTransform.ClassChanger xformer = new AnnotationCollectorTransform.ClassChanger();
xformer.transformClass(classNode);
}, Phases.SEMANTIC_ANALYSIS);
ASTTransformationVisitor.addPhaseOperations(this);
// post-transform operations:
addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
StaticVerifier verifier = new StaticVerifier();
verifier.visitClass(classNode, source);
}, Phases.SEMANTIC_ANALYSIS);
addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
GroovyClassVisitor visitor = new InnerClassCompletionVisitor(this, source);
visitor.visitClass(classNode);
visitor = new EnumCompletionVisitor(this, source);
visitor.visitClass(classNode);
}, Phases.CANONICALIZATION);
addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
Object callback = classNode.getNodeMetaData(DYNAMIC_OUTER_NODE_CALLBACK);
if (callback instanceof IPrimaryClassNodeOperation) {
((IPrimaryClassNodeOperation) callback).call(source, context, classNode);
classNode.removeNodeMetaData(DYNAMIC_OUTER_NODE_CALLBACK);
}
}, Phases.INSTRUCTION_SELECTION);
addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
// TODO: Can this be moved into org.codehaus.groovy.transform.sc.transformers.VariableExpressionTransformer?
GroovyClassVisitor visitor = new ClassCodeExpressionTransformer() {
@Override
protected SourceUnit getSourceUnit() {
return source;
}
@Override
public Expression transform(final Expression expression) {
if (expression instanceof VariableExpression) {
// check for "switch(enumType) { case CONST: ... }"
ClassNode enumType = expression.getNodeMetaData(SWITCH_CONDITION_EXPRESSION_TYPE);
if (enumType != null) {
// replace "CONST" variable expression with "EnumType.CONST" property expression
Expression propertyExpression = propX(classX(enumType), expression.getText());
setSourcePosition(propertyExpression, expression);
return propertyExpression;
}
}
return expression;
}
};
visitor.visitClass(classNode);
}, Phases.INSTRUCTION_SELECTION);
}
use of org.codehaus.groovy.ast.CompileUnit in project groovy by apache.
the class ResolveVisitor method resolveFromCompileUnit.
protected boolean resolveFromCompileUnit(final ClassNode type) {
// look into the compile unit if there is a class with that name
CompileUnit compileUnit = currentClass.getCompileUnit();
if (compileUnit == null)
return false;
ClassNode cuClass = compileUnit.getClass(type.getName());
if (cuClass != null) {
if (type != cuClass)
type.setRedirect(cuClass);
return true;
}
return false;
}
use of org.codehaus.groovy.ast.CompileUnit in project groovy by apache.
the class BytecodeHelper method isSameCompilationUnit.
/**
* Returns true if the two classes share the same compilation unit.
*
* @param a class a
* @param b class b
* @return true if both classes share the same compilation unit
*/
public static boolean isSameCompilationUnit(ClassNode a, ClassNode b) {
CompileUnit cu1 = a.getCompileUnit();
CompileUnit cu2 = b.getCompileUnit();
return cu1 != null && cu1 == cu2;
}
use of org.codehaus.groovy.ast.CompileUnit in project groovy by apache.
the class CompilationUnit method createClassVisitor.
protected ClassVisitor createClassVisitor() {
return new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES) {
private ClassNode getClassNode(String name) {
// try classes under compilation
CompileUnit cu = getAST();
ClassNode cn = cu.getClass(name);
if (cn != null)
return cn;
// try inner classes
cn = cu.getGeneratedInnerClass(name);
if (cn != null)
return cn;
ClassNodeResolver.LookupResult lookupResult = getClassNodeResolver().resolveName(name, CompilationUnit.this);
return lookupResult == null ? null : lookupResult.getClassNode();
}
private ClassNode getCommonSuperClassNode(ClassNode c, ClassNode d) {
// adapted from ClassWriter code
if (c.isDerivedFrom(d))
return d;
if (d.isDerivedFrom(c))
return c;
if (c.isInterface() || d.isInterface())
return ClassHelper.OBJECT_TYPE;
do {
c = c.getSuperClass();
} while (c != null && !d.isDerivedFrom(c));
if (c == null)
return ClassHelper.OBJECT_TYPE;
return c;
}
@Override
protected String getCommonSuperClass(String arg1, String arg2) {
ClassNode a = getClassNode(arg1.replace('/', '.'));
ClassNode b = getClassNode(arg2.replace('/', '.'));
return getCommonSuperClassNode(a, b).getName().replace('.', '/');
}
};
}
use of org.codehaus.groovy.ast.CompileUnit in project gradle by gradle.
the class GradleResolveVisitor method resolveFromCompileUnit.
protected boolean resolveFromCompileUnit(ClassNode type) {
// look into the compile unit if there is a class with that name
CompileUnit compileUnit = currentClass.getCompileUnit();
if (compileUnit == null) {
return false;
}
ClassNode cuClass = compileUnit.getClass(type.getName());
if (cuClass != null) {
if (type != cuClass) {
type.setRedirect(cuClass);
}
return true;
}
return false;
}
Aggregations