Skip to content

Commit

Permalink
Fix includes/excludes and some code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
baloo42 committed May 4, 2024
1 parent 6399eb3 commit 6b9ec1f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private Class<?> loadClass(String className) {
try {
return getClassLoader().loadClass(className);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
throw new CustomResourceCollectorException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ public class CustomResourceCollector {
private final Collection<IndexView> indexes = new LinkedList<>();
private final Set<File> filesToIndex = new HashSet<>();

private final List<Predicate<String>> classNameIncludes = new LinkedList<>();
private final List<Predicate<String>> classNameExcludes = new LinkedList<>();
private final List<Predicate<CustomResourceInfo>> customResourceInfoIncludes = new LinkedList<>();
private final List<Predicate<CustomResourceInfo>> customResourceInfoExcludes = new LinkedList<>();
private final List<Predicate<String>> classNamePredicates = new LinkedList<>();
private final List<Predicate<CustomResourceInfo>> customResourceInfoPredicates = new LinkedList<>();

private boolean forceIndex = false;

Expand Down Expand Up @@ -144,7 +142,7 @@ public CustomResourceCollector withIncludePackages(Collection<String> packages)
.filter(Objects::nonNull)
.map(pkg -> (Predicate<String>) s -> s.startsWith(pkg))
.reduce(Predicate::or)
.ifPresent(this.classNameIncludes::add);
.ifPresent(this.classNamePredicates::add);
}
return this;
}
Expand All @@ -153,9 +151,9 @@ public CustomResourceCollector withExcludePackages(Collection<String> packages)
if (packages != null) {
packages.stream()
.filter(Objects::nonNull)
.map(pkg -> (Predicate<String>) s -> s.startsWith(pkg))
.map(pkg -> (Predicate<String>) s -> !s.startsWith(pkg))
.reduce(Predicate::or)
.ifPresent(this.classNameExcludes::add);
.ifPresent(this.classNamePredicates::add);
}
return this;
}
Expand All @@ -166,7 +164,7 @@ public CustomResourceCollector withIncludeGroups(Collection<String> groups) {
.filter(Objects::nonNull)
.map(group -> (Predicate<CustomResourceInfo>) cr -> group.equals(cr.group()))
.reduce(Predicate::or)
.ifPresent(customResourceInfoIncludes::add);
.ifPresent(customResourceInfoPredicates::add);
}
return this;
}
Expand All @@ -175,9 +173,9 @@ public CustomResourceCollector withExcludeGroups(Collection<String> groups) {
if (groups != null) {
groups.stream()
.filter(Objects::nonNull)
.map(groupToFilter -> (Predicate<CustomResourceInfo>) cr -> groupToFilter.equals(cr.group()))
.map(groupToFilter -> (Predicate<CustomResourceInfo>) cr -> !groupToFilter.equals(cr.group()))
.reduce(Predicate::or)
.ifPresent(customResourceInfoExcludes::add);
.ifPresent(customResourceInfoPredicates::add);
}
return this;
}
Expand All @@ -188,7 +186,7 @@ public CustomResourceCollector withIncludeVersions(Collection<String> versions)
.filter(Objects::nonNull)
.map(versionToFilter -> (Predicate<CustomResourceInfo>) cr -> versionToFilter.equals(cr.version()))
.reduce(Predicate::or)
.ifPresent(customResourceInfoIncludes::add);
.ifPresent(customResourceInfoPredicates::add);
}
return this;
}
Expand All @@ -197,9 +195,9 @@ public CustomResourceCollector withExcludeVersions(Collection<String> versions)
if (versions != null) {
versions.stream()
.filter(Objects::nonNull)
.map(versionToFilter -> (Predicate<CustomResourceInfo>) cr -> versionToFilter.equals(cr.version()))
.map(versionToFilter -> (Predicate<CustomResourceInfo>) cr -> !versionToFilter.equals(cr.version()))
.reduce(Predicate::or)
.ifPresent(customResourceInfoExcludes::add);
.ifPresent(customResourceInfoPredicates::add);
}
return this;
}
Expand Down Expand Up @@ -262,31 +260,19 @@ public CustomResourceInfo[] findCustomResources() {
log.debug("Using explicit {} custom resource classes and skip scanning", customResourcesClassNames);
}

Predicate<String> classNameIncludePredicate = classNameIncludes.stream()
.reduce(Predicate::and)
.orElse(s -> true);

Predicate<String> classNameExcludePredicate = classNameExcludes.stream()
.reduce(Predicate::and)
.orElse(s -> false)
.negate();

Predicate<CustomResourceInfo> customResourceInfoIncludePredicate = customResourceInfoIncludes.stream()
Predicate<String> classNamePredicate = classNamePredicates.stream()
.reduce(Predicate::and)
.orElse(x -> true);
.orElse(className -> true);

Predicate<CustomResourceInfo> customResourceInfoExcludePredicate = customResourceInfoExcludes.stream()
Predicate<CustomResourceInfo> customResourceInfoPredicate = customResourceInfoPredicates.stream()
.reduce(Predicate::and)
.orElse(x -> false)
.negate();
.orElse(info -> true);

CustomResourceInfo[] infos = customResourcesClassNames.stream()
.filter(classNameIncludePredicate)
.filter(classNameExcludePredicate)
.filter(classNamePredicate)
.map(customResourceClassLoader::loadCustomResourceClass)
.map(this::createCustomResourceInfo)
.filter(customResourceInfoIncludePredicate)
.filter(customResourceInfoExcludePredicate)
.filter(customResourceInfoPredicate)
.toArray(CustomResourceInfo[]::new);

log.debug("Found {} custom resource classes after filtering", infos.length);
Expand Down Expand Up @@ -320,7 +306,7 @@ private Index createBaseIndex() {
indexer.indexClass(CustomResource.class);
return indexer.complete();
} catch (IOException e) {
throw new RuntimeException(e);
throw new CustomResourceCollectorException(e);
}
}

Expand All @@ -333,7 +319,7 @@ private Index getIndexFromDirectory(File dir) {
IndexReader reader = new IndexReader(in);
return reader.read();
} catch (IOException e) {
throw new RuntimeException(e);
throw new CustomResourceCollectorException(e);
}
}

Expand All @@ -343,7 +329,7 @@ private boolean isArchiveWithIndex(File file) {
ZipEntry entry = zip.getEntry(DEFAULT_JANDEX_INDEX);
return entry != null;
} catch (IOException e) {
throw new RuntimeException(e);
throw new CustomResourceCollectorException(e);
}
}
return false;
Expand All @@ -357,7 +343,7 @@ private Index getIndexFromArchive(File file) {
return reader.read();
}
} catch (IOException e) {
throw new RuntimeException(e);
throw new CustomResourceCollectorException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.crd.generator.collector;

public class CustomResourceCollectorException extends RuntimeException {

public CustomResourceCollectorException(String message) {
super(message);
}

public CustomResourceCollectorException(String message, Throwable cause) {
super(message, cause);
}

public CustomResourceCollectorException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public class JandexIndexer {

private static final Logger log = LoggerFactory.getLogger(JandexIndexer.class);

private static final String CLASS_FILE_SUFFIX = ".class";
private static final String JAR_FILE_SUFFIX = ".jar";

private final List<File> files = new ArrayList<>();

public JandexIndexer withFile(File... file) {
Expand Down Expand Up @@ -72,9 +75,9 @@ void appendToIndex(Indexer indexer) {
for (File file : files) {
if (file.isDirectory()) {
scanDirectoryAndAddToIndex(file, indexer);
} else if (file.isFile() && file.getName().endsWith(".class")) {
} else if (file.isFile() && file.getName().endsWith(CLASS_FILE_SUFFIX)) {
scanClassFileAndAddToIndex(file, indexer);
} else if (file.isFile() && file.getName().endsWith(".jar")) {
} else if (file.isFile() && file.getName().endsWith(JAR_FILE_SUFFIX)) {
scanJarFileAndAddToIndex(file, indexer);
} else {
throw new IllegalArgumentException("Not a class file, JAR file or directory: " + file);
Expand All @@ -84,10 +87,9 @@ void appendToIndex(Indexer indexer) {

private void scanClassFileAndAddToIndex(File file, Indexer indexer) {
try (InputStream in = Files.newInputStream(file.toPath())) {
ClassSummary info = indexer.indexWithSummary(in);
log.debug("Indexed: {} ({} Annotations)", info.name(), info.annotationsCount());
addToIndex(in, indexer);
} catch (IOException e) {
throw new RuntimeException(e);
throw new CustomResourceCollectorException(e);
}
}

Expand All @@ -97,39 +99,42 @@ private void scanJarFileAndAddToIndex(File file, Indexer indexer) {
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (!entry.isDirectory()) {
if (entry.getName().endsWith(".class")) {
if (entry.getName().endsWith(CLASS_FILE_SUFFIX)) {
try (InputStream in = zip.getInputStream(entry)) {
ClassSummary info = indexer.indexWithSummary(in);
log.debug("Indexed: {} ({} Annotations)", info.name(), info.annotationsCount());
addToIndex(in, indexer);
} catch (Exception e) {
throw new RuntimeException(e);
throw new CustomResourceCollectorException(e);
}
}
}
}
} catch (IOException e) {
throw new RuntimeException(e);
throw new CustomResourceCollectorException(e);
}
}

private void scanDirectoryAndAddToIndex(File directory, Indexer indexer) {
try (Stream<Path> stream = Files.walk(directory.toPath())) {
stream
.filter(Files::isRegularFile)
.filter(file -> file.toString().endsWith(".class"))
.filter(file -> file.toString().endsWith(CLASS_FILE_SUFFIX))
.forEach(file -> {
try (InputStream in = Files.newInputStream(file)) {
ClassSummary info = indexer.indexWithSummary(in);
log.debug("Indexed: {} ({} Annotations)", info.name(), info.annotationsCount());
addToIndex(in, indexer);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
} catch (IOException e) {
throw new RuntimeException(e);
throw new CustomResourceCollectorException(e);
}
}

private void addToIndex(InputStream inputStream, Indexer indexer) throws IOException {
ClassSummary info = indexer.indexWithSummary(inputStream);
log.debug("Indexed: {} ({} Annotations)", info.name(), info.annotationsCount());
}

public static Index indexFor(Collection<File> files) {
return new JandexIndexer()
.withFiles(files)
Expand Down

0 comments on commit 6b9ec1f

Please sign in to comment.