Skip to content

Commit

Permalink
cleanup + language modernizations.
Browse files Browse the repository at this point in the history
- lambdas, diamonds, overrides, final fields, arm blocks
- if chain over Strings -> switch
- str.replaceAll -> str.replace for non-regex usages
- map.contains + map.get -> map.get + null check
- collection sizing where known
- deprecations and warning removals
- dead code removal
- formatting fixes
  • Loading branch information
mbien committed Feb 20, 2022
1 parent 98086fb commit a3e9997
Show file tree
Hide file tree
Showing 24 changed files with 554 additions and 726 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class ArtifactDependencyIndexCreator extends AbstractIndexCreator {
private static final IndexerField FLD_NB_DEPENDENCY_VERSION = new IndexerField(new Field(null, NS, NB_DEPENDENCY_VERSION, "Dependency version"), IndexerFieldVersion.V3, NB_DEPENDENCY_VERSION, "Dependency version", IndexerField.KEYWORD_NOT_STORED);

private final List<ArtifactRepository> remoteRepos;
private final Map<ArtifactInfo, List<Dependency>> dependenciesByArtifact = new WeakHashMap<ArtifactInfo, List<Dependency>>();
private final Map<ArtifactInfo, List<Dependency>> dependenciesByArtifact = new WeakHashMap<>();
private final MavenEmbedder embedder;

ArtifactDependencyIndexCreator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Cancellation() {
super("canceled");
}

private static final Set<Cancellable> cancellables = new WeakSet<Cancellable>();
private static final Set<Cancellable> cancellables = new WeakSet<>();

public static synchronized void register(Cancellable c) {
cancellables.add(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -63,6 +63,7 @@
import org.netbeans.modules.maven.indexer.api.NBVersionInfo;
import org.netbeans.modules.maven.indexer.api.RepositoryQueries.ClassUsage;


/**
* Scans classes in (local) JARs for their Java dependencies.
*/
Expand Down Expand Up @@ -120,12 +121,9 @@ public void populateArtifactInfo(ArtifactContext context) throws IOException {

// adapted from FileUtil, since we do not want to have to use FileObject's here
private static boolean isArchiveFile(File jar) throws IOException {
InputStream in = new FileInputStream(jar);
try {
try (InputStream in = new FileInputStream(jar)) {
byte[] buffer = new byte[4];
return in.read(buffer, 0, 4) == 4 && (Arrays.equals(ZIP_HEADER_1, buffer) || Arrays.equals(ZIP_HEADER_2, buffer));
} finally {
in.close();
}
}
private static final byte[] ZIP_HEADER_1 = {80, 75, 3, 4};
Expand Down Expand Up @@ -169,38 +167,37 @@ private static boolean isArchiveFile(File jar) throws IOException {

static void search(String className, Indexer indexer, Collection<IndexingContext> contexts, List<? super ClassUsage> results) throws IOException {
String searchString = crc32base32(className.replace('.', '/'));
Query refClassQuery = indexer.constructQuery(ClassDependencyIndexCreator.FLD_NB_DEPENDENCY_CLASS.getOntology(), new StringSearchExpression(searchString));
Query refClassQuery = indexer.constructQuery(FLD_NB_DEPENDENCY_CLASS.getOntology(), new StringSearchExpression(searchString));
TopScoreDocCollector collector = TopScoreDocCollector.create(NexusRepositoryIndexerImpl.MAX_RESULT_COUNT, Integer.MAX_VALUE);
for (IndexingContext context : contexts) {
IndexSearcher searcher = context.acquireIndexSearcher();
try {
searcher.search(refClassQuery, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
LOG.log(Level.FINER, "for {0} ~ {1} found {2} hits", new Object[] {className, searchString, hits.length});
for (ScoreDoc hit : hits) {
int docId = hit.doc;
Document d = searcher.doc(docId);
String fldValue = d.get(ClassDependencyIndexCreator.NB_DEPENDENCY_CLASSES);
LOG.log(Level.FINER, "{0} uses: {1}", new Object[] {className, fldValue});
Set<String> refClasses = parseField(searchString, fldValue, d.get(ArtifactInfo.NAMES));
if (!refClasses.isEmpty()) {
ArtifactInfo ai = IndexUtils.constructArtifactInfo(d, context);
if (ai != null) {
ai.setRepository(context.getRepositoryId());
List<NBVersionInfo> version = NexusRepositoryIndexerImpl.convertToNBVersionInfo(Collections.singleton(ai));
if (!version.isEmpty()) {
results.add(new ClassUsage(version.get(0), refClasses));
searcher.search(refClassQuery, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
LOG.log(Level.FINER, "for {0} ~ {1} found {2} hits", new Object[] {className, searchString, hits.length});
for (ScoreDoc hit : hits) {
Document d = searcher.doc(hit.doc);
String fldValue = d.get(NB_DEPENDENCY_CLASSES);
LOG.log(Level.FINER, "{0} uses: {1}", new Object[] {className, fldValue});
Set<String> refClasses = parseField(searchString, fldValue, d.get(ArtifactInfo.NAMES));
if (!refClasses.isEmpty()) {
ArtifactInfo ai = IndexUtils.constructArtifactInfo(d, context);
if (ai != null) {
ai.setRepository(context.getRepositoryId());
List<NBVersionInfo> version = NexusRepositoryIndexerImpl.convertToNBVersionInfo(Collections.singleton(ai));
if (!version.isEmpty()) {
results.add(new ClassUsage(version.get(0), refClasses));
}
}
}
}
} finally {
context.releaseIndexSearcher(searcher);
}
}
} finally {
context.releaseIndexSearcher(searcher);
}
}
}
private static Set<String> parseField(String refereeCRC, String field, String referrersNL) {
Set<String> referrers = new TreeSet<String>();
Set<String> referrers = new TreeSet<>();
int p = 0;
for (String referrer : referrersNL.split("\n")) {
while (true) {
Expand Down Expand Up @@ -365,10 +362,8 @@ void read(File jar, JarClassEntryConsumer consumer) throws IOException {

// adapted from org.netbeans.nbbuild.VerifyClassLinkage
private static Collection<String> dependencies(InputStream data, File jar) throws IOException {
Set<String> result = new HashSet<String>();
ClassFile cf = new ClassFile(data);

Set<ClassName> cl = cf.getAllClassNames();
Set<ClassName> cl = new ClassFile(data).getAllClassNames();
Set<String> result = new HashSet<>(cl.size());
for (ClassName className : cl) {
result.add(className.getInternalName());
}
Expand All @@ -387,15 +382,15 @@ public Collection<IndexerField> getIndexerFields() {
*/
static String crc32base32(String s) {
crc.reset();
crc.update(s.getBytes(UTF8));
crc.update(s.getBytes(StandardCharsets.UTF_8));
long v = crc.getValue();
byte[] b64 = base32.encode(new byte[] {(byte) (v >> 24 & 0xFF), (byte) (v >> 16 & 0xFF), (byte) (v >> 8 & 0xFF), (byte) (v & 0xFF)});
assert b64.length == 8;
assert b64[7] == '=';
return new String(b64, 0, 7, LATIN1).toLowerCase();
return new String(b64, 0, 7, StandardCharsets.ISO_8859_1).toLowerCase();
}

private static final CRC32 crc = new CRC32();
private static final Base32 base32 = new Base32();
private static final Charset UTF8 = Charset.forName("UTF-8");
private static final Charset LATIN1 = Charset.forName("ISO-8859-1");

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public FastScanner(ArtifactContextProducer artifactContextProducer) {
this.artifactContextProducer = artifactContextProducer;
}

@Override
public ScanningResult scan(ScanningRequest request) {
request.getArtifactScanningListener().scanningStarted(request.getIndexingContext());
ScanningResult result = new ScanningResult(request);
Expand Down
Loading

0 comments on commit a3e9997

Please sign in to comment.