Search in sources :

Example 1 with CompilationFailureException

use of org.neo4j.codegen.CompilationFailureException in project neo4j by neo4j.

the class ByteCodeVerifier method compilationFailure.

private static CompilationFailureException compilationFailure(List<Failure> failures) {
    List<Diagnostic<?>> diagnostics = new ArrayList<>(failures.size());
    for (Failure failure : failures) {
        diagnostics.add(new BytecodeDiagnostic(failure.message));
    }
    CompilationFailureException exception = new CompilationFailureException(diagnostics);
    for (Failure failure : failures) {
        exception.addSuppressed(failure.cause);
    }
    return exception;
}
Also used : ArrayList(java.util.ArrayList) Diagnostic(javax.tools.Diagnostic) CompilationFailureException(org.neo4j.codegen.CompilationFailureException)

Example 2 with CompilationFailureException

use of org.neo4j.codegen.CompilationFailureException in project neo4j by neo4j.

the class ByteCodeVerifier method check.

/**
     * Check the bytecode from one round of bytecode generation.
     *
     * @param classpathLoader
     *         the ClassLoader to use for loading classes from the classpath.
     * @param byteCodes
     *         the bytecodes generated in this round.
     * @throws CompilationFailureException
     *         if any issue is discovered in the verification.
     */
@Override
public void check(ClassLoader classpathLoader, Collection<ByteCodes> byteCodes) throws CompilationFailureException {
    List<ClassNode> classes = new ArrayList<>(byteCodes.size());
    List<Failure> failures = new ArrayList<>();
    // load (and verify) the structure of the generated classes
    for (ByteCodes byteCode : byteCodes) {
        try {
            classes.add(classNode(byteCode.bytes()));
        } catch (Exception e) {
            failures.add(new Failure(e, e.toString()));
        }
    }
    // we are not going to be able to verify their methods
    if (!failures.isEmpty()) {
        throw compilationFailure(failures);
    }
    // continue with verifying the methods of the classes
    AssignmentChecker check = new AssignmentChecker(classpathLoader, classes);
    for (ClassNode clazz : classes) {
        verify(check, clazz, failures);
    }
    if (!failures.isEmpty()) {
        throw compilationFailure(failures);
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ArrayList(java.util.ArrayList) ByteCodes(org.neo4j.codegen.ByteCodes) AnalyzerException(org.objectweb.asm.tree.analysis.AnalyzerException) CompilationFailureException(org.neo4j.codegen.CompilationFailureException)

Example 3 with CompilationFailureException

use of org.neo4j.codegen.CompilationFailureException in project neo4j by neo4j.

the class ByteCodeVerifierTest method shouldVerifyBytecode.

@Test
public void shouldVerifyBytecode() throws Throwable {
    // given
    CodeGenerator generator = generateCode(BYTECODE, VERIFY_GENERATED_BYTECODE);
    ClassHandle handle;
    try (ClassGenerator clazz = generator.generateClass(PACKAGE, "SimpleClass");
        CodeBlock code = clazz.generateMethod(Integer.class, "box", param(int.class, "value"))) {
        handle = clazz.handle();
        code.returns(code.load("value"));
    }
    // when
    try {
        handle.loadClass();
        fail("Should have thrown exception");
    }// then
     catch (CompilationFailureException expected) {
        assertThat(expected.toString(), containsString("box(I)"));
    }
}
Also used : ClassGenerator(org.neo4j.codegen.ClassGenerator) CodeBlock(org.neo4j.codegen.CodeBlock) CodeGenerator(org.neo4j.codegen.CodeGenerator) ClassHandle(org.neo4j.codegen.ClassHandle) CompilationFailureException(org.neo4j.codegen.CompilationFailureException) Test(org.junit.Test)

Aggregations

CompilationFailureException (org.neo4j.codegen.CompilationFailureException)3 ArrayList (java.util.ArrayList)2 Diagnostic (javax.tools.Diagnostic)1 Test (org.junit.Test)1 ByteCodes (org.neo4j.codegen.ByteCodes)1 ClassGenerator (org.neo4j.codegen.ClassGenerator)1 ClassHandle (org.neo4j.codegen.ClassHandle)1 CodeBlock (org.neo4j.codegen.CodeBlock)1 CodeGenerator (org.neo4j.codegen.CodeGenerator)1 ClassNode (org.objectweb.asm.tree.ClassNode)1 AnalyzerException (org.objectweb.asm.tree.analysis.AnalyzerException)1