use of org.codehaus.groovy.GroovyBugError in project groovy by apache.
the class GenericsUtils method applyGenericsContextToPlaceHolders.
/**
* transforms generics types from an old context to a new context using the given spec. This method assumes
* all generics types will be placeholders. WARNING: The resulting generics types may or may not be placeholders
* after the transformation.
* @param genericsSpec the generics context information spec
* @param oldPlaceHolders the old placeholders
* @return the new generics types
*/
public static GenericsType[] applyGenericsContextToPlaceHolders(Map<String, ClassNode> genericsSpec, GenericsType[] oldPlaceHolders) {
if (oldPlaceHolders == null || oldPlaceHolders.length == 0)
return oldPlaceHolders;
if (genericsSpec.isEmpty())
return oldPlaceHolders;
GenericsType[] newTypes = new GenericsType[oldPlaceHolders.length];
for (int i = 0; i < oldPlaceHolders.length; i++) {
GenericsType old = oldPlaceHolders[i];
if (!old.isPlaceholder())
throw new GroovyBugError("Given generics type " + old + " must be a placeholder!");
ClassNode fromSpec = genericsSpec.get(old.getName());
if (fromSpec != null) {
if (fromSpec.isGenericsPlaceHolder()) {
ClassNode[] upper = new ClassNode[] { fromSpec.redirect() };
newTypes[i] = new GenericsType(fromSpec, upper, null);
} else {
newTypes[i] = new GenericsType(fromSpec);
}
} else {
ClassNode[] upper = old.getUpperBounds();
ClassNode[] newUpper = upper;
if (upper != null && upper.length > 0) {
ClassNode[] upperCorrected = new ClassNode[upper.length];
for (int j = 0; j < upper.length; j++) {
upperCorrected[i] = correctToGenericsSpecRecurse(genericsSpec, upper[j]);
}
upper = upperCorrected;
}
ClassNode lower = old.getLowerBound();
ClassNode newLower = correctToGenericsSpecRecurse(genericsSpec, lower);
if (lower == newLower && upper == newUpper) {
newTypes[i] = oldPlaceHolders[i];
} else {
ClassNode newPlaceHolder = ClassHelper.make(old.getName());
GenericsType gt = new GenericsType(newPlaceHolder, newUpper, newLower);
gt.setPlaceholder(true);
newTypes[i] = gt;
}
}
}
return newTypes;
}
use of org.codehaus.groovy.GroovyBugError in project groovy by apache.
the class AntlrParserPlugin method importDef.
protected void importDef(AST importNode) {
try {
// GROOVY-6094
output.putNodeMetaData(ImportNode.class, ImportNode.class);
boolean isStatic = importNode.getType() == STATIC_IMPORT;
List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
AST node = importNode.getFirstChild();
if (isType(ANNOTATIONS, node)) {
processAnnotations(annotations, node);
node = node.getNextSibling();
}
String alias = null;
if (isType(LITERAL_as, node)) {
//import is like "import Foo as Bar"
node = node.getFirstChild();
AST aliasNode = node.getNextSibling();
alias = identifier(aliasNode);
}
if (node.getNumberOfChildren() == 0) {
String name = identifier(node);
// import is like "import Foo"
ClassNode type = ClassHelper.make(name);
configureAST(type, importNode);
addImport(type, name, alias, annotations);
return;
}
AST packageNode = node.getFirstChild();
String packageName = qualifiedName(packageNode);
AST nameNode = packageNode.getNextSibling();
if (isType(STAR, nameNode)) {
if (isStatic) {
// import is like "import static foo.Bar.*"
// packageName is actually a className in this case
ClassNode type = ClassHelper.make(packageName);
configureAST(type, importNode);
addStaticStarImport(type, packageName, annotations);
} else {
// import is like "import foo.*"
addStarImport(packageName, annotations);
}
if (alias != null)
throw new GroovyBugError("imports like 'import foo.* as Bar' are not " + "supported and should be caught by the grammar");
} else {
String name = identifier(nameNode);
if (isStatic) {
// import is like "import static foo.Bar.method"
// packageName is really class name in this case
ClassNode type = ClassHelper.make(packageName);
configureAST(type, importNode);
addStaticImport(type, name, alias, annotations);
} else {
// import is like "import foo.Bar"
ClassNode type = ClassHelper.make(packageName + "." + name);
configureAST(type, importNode);
addImport(type, name, alias, annotations);
}
}
} finally {
// we're using node metadata here in order to fix GROOVY-6094
// without breaking external APIs
Object node = output.getNodeMetaData(ImportNode.class);
if (node != null && node != ImportNode.class) {
configureAST((ImportNode) node, importNode);
}
output.removeNodeMetaData(ImportNode.class);
}
}
use of org.codehaus.groovy.GroovyBugError in project groovy by apache.
the class Expression method transformExpressions.
/**
* Transforms the list of expressions, and checks that all transformed expressions have the given type.
*
* @return a new list of transformed expressions
*/
protected <T extends Expression> List<T> transformExpressions(List<? extends Expression> expressions, ExpressionTransformer transformer, Class<T> transformedType) {
List<T> list = new ArrayList<T>(expressions.size());
for (Expression expr : expressions) {
Expression transformed = transformer.transform(expr);
if (!transformedType.isInstance(transformed))
throw new GroovyBugError(String.format("Transformed expression should have type %s but has type %s", transformedType, transformed.getClass()));
list.add(transformedType.cast(transformed));
}
return list;
}
use of org.codehaus.groovy.GroovyBugError in project groovy by apache.
the class SourceUnit method parse.
//---------------------------------------------------------------------------
// PROCESSING
/**
* Parses the source to a CST. You can retrieve it with getCST().
*/
public void parse() throws CompilationFailedException {
if (this.phase > Phases.PARSING) {
throw new GroovyBugError("parsing is already complete");
}
if (this.phase == Phases.INITIALIZATION) {
nextPhase();
}
//
// Create a reader on the source and run the parser.
Reader reader = null;
try {
reader = source.getReader();
// let's recreate the parser each time as it tends to keep around state
parserPlugin = getConfiguration().getPluginFactory().createParserPlugin();
cst = parserPlugin.parseCST(this, reader);
reader.close();
} catch (IOException e) {
getErrorCollector().addFatalError(new SimpleMessage(e.getMessage(), this));
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// Ignore
}
}
}
}
use of org.codehaus.groovy.GroovyBugError in project groovy by apache.
the class ClassNodeResolver method findByClassLoading.
/**
* Search for classes using class loading
*/
private static LookupResult findByClassLoading(String name, CompilationUnit compilationUnit, GroovyClassLoader loader) {
Class cls;
try {
// NOTE: it's important to do no lookup against script files
// here since the GroovyClassLoader would create a new CompilationUnit
cls = loader.loadClass(name, false, true);
} catch (ClassNotFoundException cnfe) {
LookupResult lr = tryAsScript(name, compilationUnit, null);
return lr;
} catch (CompilationFailedException cfe) {
throw new GroovyBugError("The lookup for " + name + " caused a failed compilaton. There should not have been any compilation from this call.", cfe);
}
/*catch (NoClassDefFoundError ncdfe) {
cachedClasses.put(name,SCRIPT);
return false;
}*/
if (cls == null)
return null;
//NOTE: we might return false here even if we found a class,
// because we want to give a possible script a chance to
// recompile. This can only be done if the loader was not
// the instance defining the class.
ClassNode cn = ClassHelper.make(cls);
if (cls.getClassLoader() != loader) {
return tryAsScript(name, compilationUnit, cn);
}
return new LookupResult(null, cn);
}
Aggregations