Search in sources :

Example 1 with ClassFileDescriptor

use of com.buschmais.jqassistant.plugin.java.api.model.ClassFileDescriptor in project jqa-java-plugin by buschmais.

the class InvalidClassFileIT method validClass.

@Test
public void validClass() throws IOException {
    scanClasses(InvalidClassFileIT.class);
    store.beginTransaction();
    List<FileDescriptor> fileDescriptors = query("MATCH (c:Class:File) RETURN c").getColumn("c");
    assertThat(fileDescriptors.size(), equalTo(1));
    FileDescriptor fileDescriptor = fileDescriptors.get(0);
    assertThat(fileDescriptor, instanceOf(ClassFileDescriptor.class));
    ClassFileDescriptor classFileDescriptor = (ClassFileDescriptor) fileDescriptor;
    assertThat(classFileDescriptor.isValid(), equalTo(true));
    store.commitTransaction();
}
Also used : ClassFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.ClassFileDescriptor) JavaArtifactFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.JavaArtifactFileDescriptor) FileDescriptor(com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor) ClassFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.ClassFileDescriptor) Test(org.junit.Test)

Example 2 with ClassFileDescriptor

use of com.buschmais.jqassistant.plugin.java.api.model.ClassFileDescriptor in project jqa-java-plugin by buschmais.

the class ClassFileScannerPlugin method scan.

@Override
public ClassFileDescriptor scan(FileResource file, String path, Scope scope, final Scanner scanner) throws IOException {
    final FileDescriptor fileDescriptor = scanner.getContext().getCurrentDescriptor();
    VisitorHelper visitorHelper = new VisitorHelper(scanner.getContext(), classModelConfiguration);
    final ClassVisitor visitor = new ClassVisitor(fileDescriptor, visitorHelper);
    ClassFileDescriptor classFileDescriptor;
    try (InputStream stream = file.createStream()) {
        classFileDescriptor = MD5DigestDelegate.getInstance().digest(stream, new MD5DigestDelegate.DigestOperation<ClassFileDescriptor>() {

            @Override
            public ClassFileDescriptor execute(InputStream inputStream) throws IOException {
                new ClassReader(inputStream).accept(visitor, 0);
                return visitor.getTypeDescriptor();
            }
        });
        classFileDescriptor.setValid(true);
    } catch (RuntimeException e) {
        LOGGER.warn("Cannot scan class '" + path + "'.", e);
        classFileDescriptor = visitor.getTypeDescriptor();
        if (classFileDescriptor == null) {
            classFileDescriptor = scanner.getContext().getStore().addDescriptorType(fileDescriptor, ClassFileDescriptor.class);
        }
        classFileDescriptor.setValid(false);
    }
    return classFileDescriptor;
}
Also used : InputStream(java.io.InputStream) ClassReader(org.objectweb.asm.ClassReader) VisitorHelper(com.buschmais.jqassistant.plugin.java.impl.scanner.visitor.VisitorHelper) ClassVisitor(com.buschmais.jqassistant.plugin.java.impl.scanner.visitor.ClassVisitor) ClassFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.ClassFileDescriptor) FileDescriptor(com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor) ClassFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.ClassFileDescriptor)

Example 3 with ClassFileDescriptor

use of com.buschmais.jqassistant.plugin.java.api.model.ClassFileDescriptor in project jqa-java-plugin by buschmais.

the class ByteCodeVersionIT method byteCodeVersion.

@Test
public void byteCodeVersion() throws IOException {
    scanClasses(Pojo.class);
    store.beginTransaction();
    List<ClassFileDescriptor> types = query("MATCH (t:Type) WHERE t.name='Pojo' RETURN t").getColumn("t");
    assertThat(types.size(), equalTo(1));
    ClassFileDescriptor pojo = types.get(0);
    // Java 8
    assertThat(pojo.getByteCodeVersion(), equalTo(52));
    store.commitTransaction();
}
Also used : ClassFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.ClassFileDescriptor) Test(org.junit.Test)

Example 4 with ClassFileDescriptor

use of com.buschmais.jqassistant.plugin.java.api.model.ClassFileDescriptor in project jqa-java-plugin by buschmais.

the class InvalidClassFileIT method classFileWithHeaderOnly.

@Test
public void classFileWithHeaderOnly() throws IOException {
    final String path = "/com.buschmais.Test.class";
    final FileResource fileResource = new AbstractFileResource() {

        @Override
        public InputStream createStream() throws IOException {
            return new ByteArrayInputStream(ClassFileScannerPlugin.CAFEBABE);
        }
    };
    List<? extends FileDescriptor> fileDescriptors = execute(ARTIFACT_ID, new ScanClassPathOperation() {

        @Override
        public List<FileDescriptor> scan(JavaArtifactFileDescriptor artifact, Scanner scanner) {
            FileDescriptor fileDescriptor = scanner.scan(fileResource, path, JavaScope.CLASSPATH);
            return Collections.singletonList(fileDescriptor);
        }
    });
    store.beginTransaction();
    assertThat(fileDescriptors.size(), equalTo(1));
    FileDescriptor fileDescriptor = fileDescriptors.get(0);
    assertThat(fileDescriptor, instanceOf(ClassFileDescriptor.class));
    ClassFileDescriptor classFileDescriptor = (ClassFileDescriptor) fileDescriptor;
    assertThat(classFileDescriptor.getFileName(), equalTo(path));
    assertThat(classFileDescriptor.isValid(), equalTo(false));
    store.commitTransaction();
}
Also used : Scanner(com.buschmais.jqassistant.core.scanner.api.Scanner) ByteArrayInputStream(java.io.ByteArrayInputStream) FileResource(com.buschmais.jqassistant.plugin.common.api.scanner.filesystem.FileResource) AbstractFileResource(com.buschmais.jqassistant.plugin.common.api.scanner.filesystem.AbstractFileResource) List(java.util.List) AbstractFileResource(com.buschmais.jqassistant.plugin.common.api.scanner.filesystem.AbstractFileResource) JavaArtifactFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.JavaArtifactFileDescriptor) ClassFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.ClassFileDescriptor) JavaArtifactFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.JavaArtifactFileDescriptor) FileDescriptor(com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor) ClassFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.ClassFileDescriptor) Test(org.junit.Test)

Example 5 with ClassFileDescriptor

use of com.buschmais.jqassistant.plugin.java.api.model.ClassFileDescriptor in project jqa-java-plugin by buschmais.

the class PojoIT method attributes.

@Test
public void attributes() throws IOException {
    scanClasses(Pojo.class);
    store.beginTransaction();
    TestResult testResult = query("MATCH (t:Type:Class) WHERE t.fqn =~ '.*Pojo' RETURN t as types");
    assertThat(testResult.getRows().size(), equalTo(1));
    ClassFileDescriptor typeDescriptor = (ClassFileDescriptor) testResult.getRows().get(0).get("types");
    assertThat(typeDescriptor, is(typeDescriptor(Pojo.class)));
    assertThat(typeDescriptor.getFileName(), equalTo("/" + Pojo.class.getName().replace('.', '/') + ".class"));
    assertThat(typeDescriptor.getSourceFileName(), equalTo(Pojo.class.getSimpleName() + ".java"));
    assertThat(query("MATCH (t:Type:Class) WHERE t.fqn =~ '.*Pojo' RETURN t.name as name").getColumn("name"), hasItem(equalTo("Pojo")));
    testResult = query("MATCH (t:Type:Class)-[:DECLARES]->(f:Field) RETURN f.signature as signature, f.name as name");
    assertThat(testResult.getColumn("signature"), allOf(hasItem(equalTo("java.lang.String stringValue")), hasItem(equalTo("int intValue"))));
    assertThat(testResult.getColumn("name"), allOf(hasItem(equalTo("stringValue")), hasItem(equalTo("intValue"))));
    testResult = query("MATCH (t:Type:Class)-[:DECLARES]->(m:Method) RETURN m.signature as signature, m.name as name");
    assertThat(testResult.getColumn("signature"), allOf(hasItem(equalTo("java.lang.String getStringValue()")), hasItem(equalTo("void setStringValue(java.lang.String)")), hasItem(equalTo("int getIntValue()")), hasItem(equalTo("void setIntValue(int)"))));
    assertThat(testResult.getColumn("name"), allOf(hasItem(equalTo("getStringValue")), hasItem(equalTo("setStringValue")), hasItem(equalTo("getIntValue")), hasItem(equalTo("setIntValue"))));
    store.commitTransaction();
}
Also used : Pojo(com.buschmais.jqassistant.plugin.java.test.set.scanner.pojo.Pojo) ClassFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.ClassFileDescriptor) Test(org.junit.Test)

Aggregations

ClassFileDescriptor (com.buschmais.jqassistant.plugin.java.api.model.ClassFileDescriptor)6 Test (org.junit.Test)5 FileDescriptor (com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor)3 JavaArtifactFileDescriptor (com.buschmais.jqassistant.plugin.java.api.model.JavaArtifactFileDescriptor)2 Pojo (com.buschmais.jqassistant.plugin.java.test.set.scanner.pojo.Pojo)2 Scanner (com.buschmais.jqassistant.core.scanner.api.Scanner)1 AbstractFileResource (com.buschmais.jqassistant.plugin.common.api.scanner.filesystem.AbstractFileResource)1 FileResource (com.buschmais.jqassistant.plugin.common.api.scanner.filesystem.FileResource)1 ClassVisitor (com.buschmais.jqassistant.plugin.java.impl.scanner.visitor.ClassVisitor)1 VisitorHelper (com.buschmais.jqassistant.plugin.java.impl.scanner.visitor.VisitorHelper)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 List (java.util.List)1 ClassReader (org.objectweb.asm.ClassReader)1