Skip to content

Commit

Permalink
[JENKINS-66247] Call Index.listClassNames (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Dec 1, 2022
1 parent 0d849fd commit 643a650
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 30 deletions.
Expand Up @@ -23,7 +23,6 @@
*/
package org.kohsuke.accmod.impl;

import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Set;
import org.apache.maven.plugin.logging.Log;
Expand All @@ -40,20 +39,17 @@
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.jvnet.hudson.annotation_indexer.Index;

import static org.objectweb.asm.ClassReader.SKIP_FRAMES;

Expand Down Expand Up @@ -133,13 +129,7 @@ public void check(File f) throws IOException {
* Loads all the access restrictions defined in our dependencies.
*/
private void loadAccessRestrictions() throws IOException {
for (String prefix : new String[] {"META-INF/services/annotations/", "META-INF/annotations/"}) {
final Enumeration<URL> res = dependencies.getResources(prefix + Restricted.class.getName());
while (res.hasMoreElements()) {
URL url = res.nextElement();
loadRestrictions(url.openStream(), false);
}
}
loadRestrictions(dependencies, false);
}

/**
Expand All @@ -148,12 +138,8 @@ private void loadAccessRestrictions() throws IOException {
* @param isInTheInspectedModule
* This value shows up in {@link RestrictedElement#isInTheInspectedModule()}.
*/
public void loadRestrictions(InputStream stream, final boolean isInTheInspectedModule) throws IOException {
if (stream==null) return;

BufferedReader r = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
String className;
while ((className=r.readLine())!=null) {
public void loadRestrictions(ClassLoader cl, final boolean isInTheInspectedModule) throws IOException {
for (String className : Index.listClassNames(Restricted.class, cl)) {
InputStream is = dependencies.getResourceAsStream(className.replace('.','/') + ".class");
if (is==null) {
errorListener.onWarning(null,null,"Failed to find class file for "+ className);
Expand Down
Expand Up @@ -14,7 +14,6 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
Expand Down Expand Up @@ -95,17 +94,11 @@ public void onWarning(Throwable t, Location loc, String msg) {
}, properties != null ? properties : new Properties(), getLog());

// If there is a restriction list in the inspected module itself, load it as well:
for (String prefix : new String[] {"META-INF/services/annotations/", "META-INF/annotations/"}) {
URL local = new URL(outputURL, prefix + Restricted.class.getName());
InputStream self = null;
try {
self = local.openStream();
getLog().debug("loaded local index " + local);
} catch (IOException e) {
getLog().debug("could not load local index " + local, e);
}
if (self!=null)
checker.loadRestrictions(self, true);
try {
checker.loadRestrictions(new URLClassLoader(new URL[] {outputURL}, ClassLoader.getSystemClassLoader().getParent()), true);
getLog().debug("loaded local index " + outputURL);
} catch (IOException e) {
getLog().debug("could not load local index " + outputURL, e);
}

// perform checks
Expand Down

0 comments on commit 643a650

Please sign in to comment.