Search in sources :

Example 1 with FileDescriptor

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

the class AbstractJavaPluginIT method execute.

/**
     * Executes the given scan operation.
     * 
     * @param artifactId
     *            The artifact id of the artifact to push on the context.
     * @param operation
     *            The operation.
     */
protected List<? extends FileDescriptor> execute(String artifactId, ScanClassPathOperation operation) {
    Scanner scanner = getScanner();
    ScannerContext context = scanner.getContext();
    store.beginTransaction();
    JavaArtifactFileDescriptor artifact = getArtifactDescriptor(artifactId);
    artifact.setFullQualifiedName(artifactId);
    context.push(JavaArtifactFileDescriptor.class, artifact);
    ContainerFileResolver fileResolverStrategy = new ContainerFileResolver(artifact);
    context.push(FileResolver.class, fileResolverStrategy);
    List<? extends FileDescriptor> descriptors = execute(artifact, operation, scanner);
    for (FileDescriptor descriptor : descriptors) {
        fileResolverStrategy.put(descriptor.getFileName(), descriptor);
    }
    context.pop(JavaArtifactFileDescriptor.class);
    context.pop(FileResolver.class);
    fileResolverStrategy.flush();
    store.commitTransaction();
    return descriptors;
}
Also used : Scanner(com.buschmais.jqassistant.core.scanner.api.Scanner) ContainerFileResolver(com.buschmais.jqassistant.plugin.common.api.scanner.ContainerFileResolver) JavaArtifactFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.JavaArtifactFileDescriptor) ScannerContext(com.buschmais.jqassistant.core.scanner.api.ScannerContext) JavaArtifactFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.JavaArtifactFileDescriptor) FileDescriptor(com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor)

Example 2 with FileDescriptor

use of com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor 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 FileDescriptor

use of com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor 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 4 with FileDescriptor

use of com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor 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 5 with FileDescriptor

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

the class TypeResolverIT method duplicateTypeInSameArtifact.

/**
     * @throws IOException
     *             If the test fails.
     */
@Test
public void duplicateTypeInSameArtifact() throws IOException {
    File directory = getClassesDirectory(A.class);
    final String resource = "/" + A.class.getName().replace(".", "/") + ".class";
    final File file = new File(directory, resource);
    scanClasses(B.class);
    List<? extends FileDescriptor> descriptors = execute("a1", new ScanClassPathOperation() {

        @Override
        public List<FileDescriptor> scan(JavaArtifactFileDescriptor artifact, Scanner scanner) {
            List<FileDescriptor> result = new ArrayList<>();
            FileDescriptor fileDescriptor1 = scanner.scan(file, "/1.0" + resource, JavaScope.CLASSPATH);
            FileDescriptor fileDescriptor2 = scanner.scan(file, resource, JavaScope.CLASSPATH);
            result.add(fileDescriptor1);
            result.add(fileDescriptor2);
            return result;
        }
    });
    store.beginTransaction();
    assertThat(descriptors.size(), equalTo(2));
    FileDescriptor fileDescriptor1 = descriptors.get(0);
    assertThat(fileDescriptor1.getFileName(), equalTo("/1.0" + resource));
    assertThat(fileDescriptor1, instanceOf(TypeDescriptor.class));
    assertThat(((TypeDescriptor) fileDescriptor1).getFullQualifiedName(), equalTo(A.class.getName()));
    FileDescriptor fileDescriptor2 = descriptors.get(1);
    assertThat(fileDescriptor2.getFileName(), equalTo(resource));
    assertThat(fileDescriptor2, instanceOf(TypeDescriptor.class));
    assertThat(((TypeDescriptor) fileDescriptor2).getFullQualifiedName(), equalTo(A.class.getName()));
    store.commitTransaction();
}
Also used : A(com.buschmais.jqassistant.plugin.java.test.set.scanner.resolver.A) Scanner(com.buschmais.jqassistant.core.scanner.api.Scanner) TypeDescriptor(com.buschmais.jqassistant.plugin.java.api.model.TypeDescriptor) ArrayList(java.util.ArrayList) List(java.util.List) JavaArtifactFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.JavaArtifactFileDescriptor) File(java.io.File) JavaArtifactFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.JavaArtifactFileDescriptor) ArtifactFileDescriptor(com.buschmais.jqassistant.plugin.common.api.model.ArtifactFileDescriptor) FileDescriptor(com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor) Test(org.junit.Test)

Aggregations

FileDescriptor (com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor)11 JavaArtifactFileDescriptor (com.buschmais.jqassistant.plugin.java.api.model.JavaArtifactFileDescriptor)7 Scanner (com.buschmais.jqassistant.core.scanner.api.Scanner)5 ScannerContext (com.buschmais.jqassistant.core.scanner.api.ScannerContext)4 InputStream (java.io.InputStream)4 List (java.util.List)4 Test (org.junit.Test)4 ClassFileDescriptor (com.buschmais.jqassistant.plugin.java.api.model.ClassFileDescriptor)3 TypeDescriptor (com.buschmais.jqassistant.plugin.java.api.model.TypeDescriptor)3 File (java.io.File)3 Store (com.buschmais.jqassistant.core.store.api.Store)2 ServiceLoaderDescriptor (com.buschmais.jqassistant.plugin.java.api.model.ServiceLoaderDescriptor)2 ArrayList (java.util.ArrayList)2 ArtifactFileDescriptor (com.buschmais.jqassistant.plugin.common.api.model.ArtifactFileDescriptor)1 PropertyDescriptor (com.buschmais.jqassistant.plugin.common.api.model.PropertyDescriptor)1 ContainerFileResolver (com.buschmais.jqassistant.plugin.common.api.scanner.ContainerFileResolver)1 AbstractFileResource (com.buschmais.jqassistant.plugin.common.api.scanner.filesystem.AbstractFileResource)1 FileResource (com.buschmais.jqassistant.plugin.common.api.scanner.filesystem.FileResource)1 JavaClassesDirectoryDescriptor (com.buschmais.jqassistant.plugin.java.api.model.JavaClassesDirectoryDescriptor)1 ManifestFileDescriptor (com.buschmais.jqassistant.plugin.java.api.model.ManifestFileDescriptor)1