use of org.codehaus.groovy.GroovyBugError in project groovy-core by groovy.
the class CompilationUnit method createClassVisitor.
protected ClassVisitor createClassVisitor() {
CompilerConfiguration config = getConfiguration();
int computeMaxStackAndFrames = ClassWriter.COMPUTE_MAXS;
if (CompilerConfiguration.isPostJDK7(config.getTargetBytecode()) || Boolean.TRUE.equals(config.getOptimizationOptions().get("indy"))) {
computeMaxStackAndFrames += ClassWriter.COMPUTE_FRAMES;
}
return new ClassWriter(computeMaxStackAndFrames) {
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;
// try class loader classes
try {
cn = ClassHelper.make(cu.getClassLoader().loadClass(name, false, true), false);
} catch (Exception e) {
throw new GroovyBugError(e);
}
return cn;
}
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.GroovyBugError in project groovy-core by groovy.
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-core by groovy.
the class GroovyTypeCheckingExtensionSupport method setup.
@Override
public void setup() {
CompilerConfiguration config = new CompilerConfiguration();
config.setScriptBaseClass("org.codehaus.groovy.transform.stc.GroovyTypeCheckingExtensionSupport.TypeCheckingDSL");
ImportCustomizer ic = new ImportCustomizer();
ic.addStarImports("org.codehaus.groovy.ast.expr");
ic.addStaticStars("org.codehaus.groovy.ast.ClassHelper");
ic.addStaticStars("org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport");
config.addCompilationCustomizers(ic);
final GroovyClassLoader transformLoader = compilationUnit != null ? compilationUnit.getTransformLoader() : typeCheckingVisitor.getSourceUnit().getClassLoader();
// since Groovy 2.2, it is possible to use FQCN for type checking extension scripts
TypeCheckingDSL script = null;
try {
Class<?> clazz = transformLoader.loadClass(scriptPath, false, true);
if (TypeCheckingDSL.class.isAssignableFrom(clazz)) {
script = (TypeCheckingDSL) clazz.newInstance();
} else if (TypeCheckingExtension.class.isAssignableFrom(clazz)) {
// since 2.4, we can also register precompiled type checking extensions which are not scripts
try {
Constructor<?> declaredConstructor = clazz.getDeclaredConstructor(StaticTypeCheckingVisitor.class);
TypeCheckingExtension extension = (TypeCheckingExtension) declaredConstructor.newInstance(typeCheckingVisitor);
typeCheckingVisitor.addTypeCheckingExtension(extension);
extension.setup();
return;
} catch (InstantiationException e) {
addLoadingError(config);
} catch (IllegalAccessException e) {
addLoadingError(config);
} catch (NoSuchMethodException e) {
context.getErrorCollector().addFatalError(new SimpleMessage("Static type checking extension '" + scriptPath + "' could not be loaded because it doesn't have a constructor accepting StaticTypeCheckingVisitor.", config.getDebug(), typeCheckingVisitor.getSourceUnit()));
} catch (InvocationTargetException e) {
addLoadingError(config);
}
}
} catch (ClassNotFoundException e) {
// silent
} catch (InstantiationException e) {
addLoadingError(config);
} catch (IllegalAccessException e) {
addLoadingError(config);
}
if (script == null) {
ClassLoader cl = typeCheckingVisitor.getSourceUnit().getClassLoader();
// cast to prevent incorrect @since 1.7 warning
InputStream is = ((ClassLoader) transformLoader).getResourceAsStream(scriptPath);
if (is == null) {
// fallback to the source unit classloader
is = cl.getResourceAsStream(scriptPath);
}
if (is == null) {
// fallback to the compiler classloader
cl = GroovyTypeCheckingExtensionSupport.class.getClassLoader();
is = cl.getResourceAsStream(scriptPath);
}
if (is == null) {
// if the input stream is still null, we've not found the extension
context.getErrorCollector().addFatalError(new SimpleMessage("Static type checking extension '" + scriptPath + "' was not found on the classpath.", config.getDebug(), typeCheckingVisitor.getSourceUnit()));
}
try {
GroovyShell shell = new GroovyShell(transformLoader, new Binding(), config);
script = (TypeCheckingDSL) shell.parse(new InputStreamReader(is, typeCheckingVisitor.getSourceUnit().getConfiguration().getSourceEncoding()));
} catch (CompilationFailedException e) {
throw new GroovyBugError("An unexpected error was thrown during custom type checking", e);
} catch (UnsupportedEncodingException e) {
throw new GroovyBugError("Unsupported encoding found in compiler configuration", e);
}
}
if (script != null) {
script.extension = this;
script.run();
List<Closure> list = eventHandlers.get("setup");
if (list != null) {
for (Closure closure : list) {
safeCall(closure);
}
}
}
}
use of org.codehaus.groovy.GroovyBugError in project groovy-core by groovy.
the class GroovyTypeCheckingExtensionSupport method handleMissingMethod.
@Override
@SuppressWarnings("unchecked")
public List<MethodNode> handleMissingMethod(final ClassNode receiver, final String name, final ArgumentListExpression argumentList, final ClassNode[] argumentTypes, final MethodCall call) {
List<Closure> onMethodSelection = eventHandlers.get("handleMissingMethod");
List<MethodNode> methodList = new LinkedList<MethodNode>();
if (onMethodSelection != null) {
for (Closure closure : onMethodSelection) {
Object result = safeCall(closure, receiver, name, argumentList, argumentTypes, call);
if (result != null) {
if (result instanceof MethodNode) {
methodList.add((MethodNode) result);
} else if (result instanceof Collection) {
methodList.addAll((Collection<? extends MethodNode>) result);
} else {
throw new GroovyBugError("Type checking extension returned unexpected method list: " + result);
}
}
}
}
return methodList;
}
use of org.codehaus.groovy.GroovyBugError in project groovy-core by groovy.
the class StaticTypeCheckingSupport method applyGenericsConnections.
static void applyGenericsConnections(Map<String, GenericsType> connections, Map<String, GenericsType> resolvedPlaceholders) {
if (connections == null)
return;
int count = 0;
while (count < 10000) {
count++;
boolean checkForMorePlaceHolders = false;
for (Map.Entry<String, GenericsType> entry : resolvedPlaceholders.entrySet()) {
String name = entry.getKey();
GenericsType replacement = connections.get(name);
if (replacement == null) {
GenericsType value = entry.getValue();
GenericsType newValue = applyGenericsContext(connections, value);
entry.setValue(newValue);
checkForMorePlaceHolders = checkForMorePlaceHolders || !equalIncludingGenerics(value, newValue);
continue;
}
GenericsType original = entry.getValue();
if (!original.isWildcard() && !original.isPlaceholder()) {
continue;
} else if (!replacement.isPlaceholder()) {
entry.setValue(replacement);
continue;
}
GenericsType connectedType = resolvedPlaceholders.get(replacement.getName());
if (replacement == connectedType)
continue;
entry.setValue(replacement);
checkForMorePlaceHolders = checkForMorePlaceHolders || !equalIncludingGenerics(original, replacement);
}
if (!checkForMorePlaceHolders)
break;
}
if (count >= 10000)
throw new GroovyBugError("unable to handle generics in " + resolvedPlaceholders + " with connections " + connections);
}
Aggregations