Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8331855: Convert jdk.jdeps jdeprscan and jdeps to use the Classfile API #19193

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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