Skip to content

Commit

Permalink
Cleanup of scanners
Browse files Browse the repository at this point in the history
Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Sep 18, 2022
1 parent e1cd0fe commit 87fd0de
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 247 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,17 +17,19 @@

package org.glassfish.apf;

import org.glassfish.hk2.classmodel.reflect.Types;
import org.jvnet.hk2.annotations.Contract;

import java.util.Set;
import java.io.File;
import java.io.IOException;
import java.util.Set;

import org.glassfish.hk2.classmodel.reflect.Types;
import org.jvnet.hk2.annotations.Contract;

/**
* This interface is responsible for scanning the binary location
* provided and provide each binary file through a pull interfaces
*
* @param <T>
*
* @author Jerome Dochez
*/
@Contract
Expand All @@ -35,41 +38,44 @@ public interface Scanner<T> {
/**
* Scan the archive file and gather a list of classes
* that should be processed for anntoations
*
* @param archiveFile the archive file for scanning
* @param bundleDesc the bundle descriptor associated with this archive
* @param classloader the classloader used to scan the annotation
* @param classLoader the classloader used to scan the annotation
*/
public void process(File archiveFile, T bundleDesc,
ClassLoader classLoader) throws IOException;

void process(File archiveFile, T bundleDesc, ClassLoader classLoader) throws IOException;

/**
* Returns a ClassLoader capable of loading classes from the
* underlying medium
*
* @return a class loader capable of loading the classes
*/
public ClassLoader getClassLoader();
ClassLoader getClassLoader();

/**
* Return a complete set of classes available from this location.
*
* @return the complete set of classes
*/
public Set<Class> getElements();
Set<Class<?>> getElements();

/**
* Sometimes, annotations processing requires more than a single class,
* especially when such classes end up being a Java Component (Java Beans,
* Java EE). The implementation returned from the getComponent will be
* responsible for defining the complete view of this component starting
* from it's implementation class.
*
* @param componentImpl class of the component.
*/
public ComponentInfo getComponentInfo(Class componentImpl);
ComponentInfo getComponentInfo(Class componentImpl);

/**
* Return the types information for this module
*
* @return types the archive resulting types
*/
public Types getTypes();
Types getTypes();

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -19,18 +20,13 @@
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.security.PrivilegedAction;
import java.util.Set;
import java.util.Iterator;
import java.util.Enumeration;
import java.util.HashSet;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.PrivilegedAction;
import java.util.HashSet;
import java.util.Set;

import org.glassfish.apf.Scanner;
import org.glassfish.hk2.classmodel.reflect.Parser;
import org.glassfish.hk2.classmodel.reflect.ParsingContext;
import org.glassfish.hk2.classmodel.reflect.Types;

/**
* Implementation of the Scanner interface for a directory
Expand All @@ -40,9 +36,10 @@
public class DirectoryScanner extends JavaEEScanner implements Scanner {

File directory;
Set<String> entries = new HashSet<String>();
Set<String> entries = new HashSet<>();
ClassLoader classLoader = null;

@Override
public void process(File directory, Object bundleDesc, ClassLoader classLoader)
throws IOException {
AnnotationUtils.getLogger().finer("dir is " + directory);
Expand All @@ -60,6 +57,7 @@ private void init(File directory) throws java.io.IOException {
private void init(File top, File directory) throws java.io.IOException {

File[] dirFiles = directory.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getAbsolutePath().endsWith(".class");
}
Expand All @@ -71,6 +69,7 @@ public boolean accept(File pathname) {
}

File[] subDirs = directory.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isDirectory();
}
Expand All @@ -86,11 +85,14 @@ protected Set<String> getEntries() {
return entries;
}

@Override
public ClassLoader getClassLoader() {
if (classLoader==null) {
final URL[] urls = new URL[1];
try {
if (directory == null) throw new IllegalStateException("directory must first be set by calling the process method.");
if (directory == null) {
throw new IllegalStateException("directory must first be set by calling the process method.");
}
urls[0] = directory.getAbsoluteFile().toURL();
classLoader = new PrivilegedAction<URLClassLoader>() {
@Override
Expand All @@ -105,10 +107,11 @@ public URLClassLoader run() {
return classLoader;
}

@Override
public Set<Class> getElements() {


Set<Class> elements = new HashSet<Class>();
Set<Class> elements = new HashSet<>();
if (getClassLoader()==null) {
AnnotationUtils.getLogger().severe("Class loader null");
return elements;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -17,30 +18,30 @@
package org.glassfish.apf.impl;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.PrivilegedAction;
import java.util.Set;
import java.util.Iterator;
import java.util.Enumeration;
import java.util.HashSet;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.jar.JarFile;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.glassfish.apf.Scanner;

/**
* Implements the scanner interface on a jar file.
*
* @author Jerome Dochez
*/
public class JarScanner extends JavaEEScanner implements Scanner<Object> {
public class JarScanner extends JavaEEScanner implements Scanner {

File jarFile;
Set<JarEntry> entries = new HashSet<JarEntry>();
ClassLoader classLoader = null;
Set<JarEntry> entries = new HashSet<>();
ClassLoader classLoader;


@Override
public void process(File jarFile, Object bundleDesc, ClassLoader loader) throws java.io.IOException {
this.jarFile = jarFile;
JarFile jf = new JarFile(jarFile);
Expand All @@ -59,11 +60,14 @@ public void process(File jarFile, Object bundleDesc, ClassLoader loader) throws
initTypes(jarFile);
}

@Override
public ClassLoader getClassLoader() {
if (classLoader==null) {
final URL[] urls = new URL[1];
try {
if (jarFile == null) throw new IllegalStateException("jarFile must first be set with the process method.");
if (jarFile == null) {
throw new IllegalStateException("jarFile must first be set with the process method.");
}
urls[0] = jarFile.getAbsoluteFile().toURL();
classLoader = new PrivilegedAction<URLClassLoader>() {
@Override
Expand All @@ -78,10 +82,9 @@ public URLClassLoader run() {
return classLoader;
}

public Set<Class> getElements() {


Set<Class> elements = new HashSet<Class>();
@Override
public Set<Class<?>> getElements() {
Set<Class<?>> elements = new HashSet<>();
if (getClassLoader()==null) {
AnnotationUtils.getLogger().severe("Class loader null");
return elements;
Expand All @@ -100,7 +103,4 @@ public Set<Class> getElements() {
}
return elements;
}



}

0 comments on commit 87fd0de

Please sign in to comment.