use of groovy.lang.GroovyRuntimeException in project groovy by apache.
the class DummyClassGenerator method visitClass.
// GroovyClassVisitor interface
//-------------------------------------------------------------------------
public void visitClass(ClassNode classNode) {
try {
this.classNode = classNode;
this.internalClassName = BytecodeHelper.getClassInternalName(classNode);
//System.out.println("Generating class: " + classNode.getName());
this.internalBaseClassName = BytecodeHelper.getClassInternalName(classNode.getSuperClass());
cv.visit(Opcodes.V1_3, classNode.getModifiers(), internalClassName, (String) null, internalBaseClassName, BytecodeHelper.getClassInternalNames(classNode.getInterfaces()));
classNode.visitContents(this);
for (Iterator iter = innerClasses.iterator(); iter.hasNext(); ) {
ClassNode innerClass = (ClassNode) iter.next();
ClassNode innerClassType = innerClass;
String innerClassInternalName = BytecodeHelper.getClassInternalName(innerClassType);
// default for inner classes
String outerClassName = internalClassName;
MethodNode enclosingMethod = innerClass.getEnclosingMethod();
if (enclosingMethod != null) {
// local inner classes do not specify the outer class name
outerClassName = null;
}
cv.visitInnerClass(innerClassInternalName, outerClassName, innerClassType.getName(), innerClass.getModifiers());
}
cv.visitEnd();
} catch (GroovyRuntimeException e) {
e.setModule(classNode.getModule());
throw e;
}
}
use of groovy.lang.GroovyRuntimeException in project groovy by apache.
the class AsmClassGenerator method visitConstructorOrMethod.
protected void visitConstructorOrMethod(MethodNode node, boolean isConstructor) {
controller.resetLineNumber();
Parameter[] parameters = node.getParameters();
String methodType = BytecodeHelper.getMethodDescriptor(node.getReturnType(), parameters);
String signature = BytecodeHelper.getGenericsMethodSignature(node);
int modifiers = node.getModifiers();
if (isVargs(node.getParameters()))
modifiers |= Opcodes.ACC_VARARGS;
MethodVisitor mv = cv.visitMethod(modifiers, node.getName(), methodType, signature, buildExceptions(node.getExceptions()));
controller.setMethodVisitor(mv);
visitAnnotations(node, mv);
for (int i = 0; i < parameters.length; i++) {
visitParameterAnnotations(parameters[i], i, mv);
}
// Add parameter names to the MethodVisitor (jdk8+ only)
if (getCompileUnit().getConfig().getParameters()) {
for (int i = 0; i < parameters.length; i++) {
// TODO handle ACC_SYNTHETIC for enum method parameters?
mv.visitParameter(parameters[i].getName(), 0);
}
}
if (controller.getClassNode().isAnnotationDefinition() && !node.isStaticConstructor()) {
visitAnnotationDefault(node, mv);
} else if (!node.isAbstract()) {
Statement code = node.getCode();
mv.visitCode();
// fast path for getter/setters etc.
if (code instanceof BytecodeSequence && ((BytecodeSequence) code).getInstructions().size() == 1 && ((BytecodeSequence) code).getInstructions().get(0) instanceof BytecodeInstruction) {
((BytecodeInstruction) ((BytecodeSequence) code).getInstructions().get(0)).visit(mv);
} else {
visitStdMethod(node, isConstructor, parameters, code);
}
//mv.visitInsn(NOP);
try {
mv.visitMaxs(0, 0);
} catch (Exception e) {
throw new GroovyRuntimeException("ASM reporting processing error for " + controller.getClassNode() + "#" + node.getName() + " with signature " + node.getTypeDescriptor() + " in " + sourceFile + ":" + node.getLineNumber(), e);
}
}
mv.visitEnd();
}
use of groovy.lang.GroovyRuntimeException in project groovy by apache.
the class MethodFailureTest method testMethodWhichCallsTheFailingMethodInsideAClosure.
public void testMethodWhichCallsTheFailingMethodInsideAClosure() {
MockGroovyObject object = new MockGroovyObject();
try {
object.invokeMethod("callClosure", new Closure(this) {
protected Object doCall(GroovyObject object) {
return object.invokeMethod("nonExistentMethod", "hello");
}
});
fail("Should have thrown an exception");
} catch (GroovyRuntimeException e) {
// expected
}
}
use of groovy.lang.GroovyRuntimeException in project groovy by apache.
the class ConversionHandler method invoke.
/**
* This method is a default implementation for the invoke method given in
* InvocationHandler. Any call to a method with a declaring class that is
* not Object, excluding toString() and default methods is redirected to invokeCustom.
* <p>
* Methods like equals and hashcode are called on the class itself instead
* of the delegate because they are considered fundamental methods that should
* not be overwritten. The toString() method gets special treatment as it is
* deemed to be a method that you might wish to override when called from Groovy.
* Interface default methods from Java 8 on the other hand are considered being
* default implementations you don't normally want to change. So they are called
* directly too
* </p><p>
* In many scenarios, it is better to overwrite the invokeCustom method where
* the core Object related methods are filtered out.
*</p>
* @param proxy the proxy
* @param method the method
* @param args the arguments
* @return the result of the invocation by method or delegate
* @throws Throwable if caused by the delegate or the method
* @see #invokeCustom(Object, Method, Object[])
* @see InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (handleCache != null && isDefaultMethod(method)) {
VMPlugin plugin = VMPluginFactory.getPlugin();
Object handle = handleCache.get(method);
if (handle == null) {
handle = plugin.getInvokeSpecialHandle(method, proxy);
handleCache.put(method, handle);
}
return plugin.invokeHandle(handle, args);
}
if (!checkMethod(method)) {
try {
if (method.getDeclaringClass() == GroovyObject.class) {
if ("getMetaClass".equals(method.getName())) {
return getMetaClass(proxy);
} else if ("setMetaClass".equals(method.getName())) {
return setMetaClass((MetaClass) args[0]);
}
}
return invokeCustom(proxy, method, args);
} catch (GroovyRuntimeException gre) {
throw ScriptBytecodeAdapter.unwrap(gre);
}
}
try {
return method.invoke(this, args);
} catch (InvocationTargetException ite) {
throw ite.getTargetException();
}
}
use of groovy.lang.GroovyRuntimeException in project groovy by apache.
the class DefaultTypeTransformation method continueCastOnSAM.
private static Object continueCastOnSAM(Object object, Class type) {
if (object instanceof Closure) {
Method m = CachedSAMClass.getSAMMethod(type);
if (m != null) {
return CachedSAMClass.coerceToSAM((Closure) object, m, type, type.isInterface());
}
}
Object[] args = null;
if (object instanceof Collection) {
// let's try invoke the constructor with the list as arguments
// such as for creating a Dimension, Point, Color etc.
Collection collection = (Collection) object;
args = collection.toArray();
} else if (object instanceof Object[]) {
args = (Object[]) object;
} else if (object instanceof Map) {
// emulate named params constructor
args = new Object[1];
args[0] = object;
}
Exception nested = null;
if (args != null) {
try {
return InvokerHelper.invokeConstructorOf(type, args);
} catch (InvokerInvocationException iie) {
throw iie;
} catch (GroovyRuntimeException e) {
if (e.getMessage().contains("Could not find matching constructor for")) {
try {
return InvokerHelper.invokeConstructorOf(type, object);
} catch (InvokerInvocationException iie) {
throw iie;
} catch (Exception ex) {
// let's ignore exception and return the original object
// as the caller has more context to be able to throw a more
// meaningful exception (but stash to get message later)
nested = e;
}
} else {
nested = e;
}
} catch (Exception e) {
// let's ignore exception and return the original object
// as the caller has more context to be able to throw a more
// meaningful exception (but stash to get message later)
nested = e;
}
}
GroovyCastException gce;
if (nested != null) {
gce = new GroovyCastException(object, type, nested);
} else {
gce = new GroovyCastException(object, type);
}
throw gce;
}
Aggregations