Skip to content

Commit

Permalink
8331855: Convert jdk.jdeps jdeprscan and jdeps to use the Classfile API
Browse files Browse the repository at this point in the history
Reviewed-by: asotona
  • Loading branch information
liach authored and asotona committed May 17, 2024
1 parent beeffd4 commit d4c2edf
Show file tree
Hide file tree
Showing 19 changed files with 411 additions and 864 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,16 +25,13 @@

package com.sun.tools.jdeprscan.scan;

import java.lang.classfile.ClassModel;
import java.lang.classfile.constantpool.*;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;
import java.util.Locale;

import com.sun.tools.classfile.ClassFile;
import com.sun.tools.classfile.ConstantPool;

import static com.sun.tools.classfile.ConstantPool.CPInfo;

/**
* A container for selected constant pool entries. There are currently
* lists that contain the following types of CP entries:
Expand All @@ -45,15 +42,21 @@
* - CONSTANT_InterfaceMethodref_info
*/
class CPEntries {
final List<ConstantPool.CONSTANT_Class_info> classes = new ArrayList<>();
final List<ConstantPool.CONSTANT_Fieldref_info> fieldRefs = new ArrayList<>();
final List<ConstantPool.CONSTANT_Methodref_info> methodRefs = new ArrayList<>();
final List<ConstantPool.CONSTANT_InterfaceMethodref_info> intfMethodRefs = new ArrayList<>();
final List<ClassEntry> classes = new ArrayList<>();
final List<FieldRefEntry> fieldRefs = new ArrayList<>();
final List<MethodRefEntry> methodRefs = new ArrayList<>();
final List<InterfaceMethodRefEntry> intfMethodRefs = new ArrayList<>();

public static CPEntries loadFrom(ClassFile cf) {
public static CPEntries loadFrom(ClassModel cf) {
CPEntries entries = new CPEntries();
for (CPInfo cpi : cf.constant_pool.entries()) {
cpi.accept(new CPSelector(), entries);
for (PoolEntry cpi : cf.constantPool()) {
switch (cpi) {
case ClassEntry ce -> entries.classes.add(ce);
case MethodRefEntry mref -> entries.methodRefs.add(mref);
case InterfaceMethodRefEntry imref -> entries.intfMethodRefs.add(imref);
case FieldRefEntry fref -> entries.fieldRefs.add(fref);
default -> {}
}
}
return entries;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,10 +25,9 @@

package com.sun.tools.jdeprscan.scan;

import com.sun.tools.classfile.ClassFile;
import com.sun.tools.classfile.ConstantPoolException;

import java.io.IOException;
import java.lang.classfile.ClassFile;
import java.lang.classfile.ClassModel;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
Expand Down Expand Up @@ -92,9 +91,9 @@ public void addJrt() {
* @param className the class to search for
* @return a ClassFile instance, or null if not found
*/
public ClassFile find(String className) {
public ClassModel find(String className) {
for (PathEntry pe : list) {
ClassFile cf = pe.find(className);
ClassModel cf = pe.find(className);
if (cf != null) {
return cf;
}
Expand All @@ -113,7 +112,7 @@ interface PathEntry {
* @param className the class to search for
* @return a ClassFile instance, or null if not found
*/
ClassFile find(String className);
ClassModel find(String className);
}

/**
Expand All @@ -127,14 +126,14 @@ class JarPathEntry implements PathEntry {
}

@Override
public ClassFile find(String className) {
public ClassModel find(String className) {
JarEntry entry = jarFile.getJarEntry(className + ".class");
if (entry == null) {
return null;
}
try {
return ClassFile.read(jarFile.getInputStream(entry));
} catch (IOException | ConstantPoolException ex) {
return ClassFile.of().parse(jarFile.getInputStream(entry).readAllBytes());
} catch (IOException | IllegalArgumentException ex) {
if (verbose) {
ex.printStackTrace();
}
Expand All @@ -154,13 +153,13 @@ class DirPathEntry implements PathEntry {
}

@Override
public ClassFile find(String className) {
public ClassModel find(String className) {
Path classFileName = dir.resolve(className + ".class");
try {
return ClassFile.read(classFileName);
return ClassFile.of().parse(classFileName);
} catch (NoSuchFileException nsfe) {
// not found, return silently
} catch (IOException | ConstantPoolException ex) {
} catch (IOException | IllegalArgumentException ex) {
if (verbose) {
ex.printStackTrace();
}
Expand All @@ -181,7 +180,7 @@ class JrtPathEntry implements PathEntry {
final FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));

@Override
public ClassFile find(String className) {
public ClassModel find(String className) {
int end = className.lastIndexOf('/');
if (end < 0) {
return null;
Expand All @@ -194,13 +193,13 @@ public ClassFile find(String className) {
.filter(Files::exists)
.findFirst();
if (opath.isPresent()) {
return ClassFile.read(opath.get());
return ClassFile.of().parse(opath.get());
} else {
return null;
}
} catch (NoSuchFileException nsfe) {
// not found, return silently
} catch (IOException | ConstantPoolException ex) {
} catch (IOException | IllegalArgumentException ex) {
if (verbose) {
ex.printStackTrace();
}
Expand Down
Loading

1 comment on commit d4c2edf

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.