Files
scanpath-v2/src/main/java/dxt/scanpath/v2/bc/ZClass.java
T
2026-11-14 21:14:29 +05:30

221 lines
5.6 KiB
Java

/*
* Copyright (c) DuxTechnica
*
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
* use this file except in compliance with the License. You may obtain a copy of the License by
* contacting:
*
* Dux Technica, [email protected]
*
* This file 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 dxt.scanpath.v2.bc;
import java.lang.classfile.Annotation;
import java.lang.classfile.ClassFile;
import java.lang.classfile.ClassModel;
import java.lang.classfile.FieldModel;
import java.lang.classfile.MethodModel;
import java.lang.classfile.attribute.RuntimeVisibleAnnotationsAttribute;
import java.lang.classfile.constantpool.ClassEntry;
import java.lang.constant.ClassDesc;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
/**
* Encapsulates metadata for a class read directly from a class file.
*
* <p>The metadata includes the class name, availability of class-file data, superclass, implemented
* interfaces, runtime-visible annotations, fields, and methods.
*
* @author indroneel
*/
public class ZClass {
private final String clsName;
private final boolean available;
private ZClass supType;
private List<ZClass> ifTypes;
private List<ZClass> annTypes;
private List<ZField> fields;
private List<ZMethod> methods;
/**
* Creates a placeholder for a class whose classfile data is unavailable.
*
* @param fqcn the fully qualified class name
*/
ZClass(String fqcn) {
clsName = fqcn;
available = false;
ifTypes = new ArrayList<>(); // avoid null pointer
annTypes = new ArrayList<>(); // avoid null pointer
fields = new ArrayList<>(); // avoid null pointer
methods = new ArrayList<>(); // avoid null pointer
}
/**
* Parses class metadata from the bytes in a class file.
*
* @param ctxt the class context used to resolve related classes
* @param clsData the class-file contents
*/
ZClass(ZClassContext ctxt, byte[] clsData) {
ClassModel clsMod = ClassFile.of().parse(clsData);
available = true;
ClassEntry ce = clsMod.thisClass();
clsName = ce.asInternalName().replace('/', '.');
prepareSuperClass(ctxt, clsMod);
prepareInterfaces(ctxt, clsMod);
prepareAnnotations(ctxt, clsMod);
prepareFields(ctxt, clsMod);
prepareMethods(ctxt, clsMod);
}
/**
* Returns the fully qualified class name.
*
* @return the class name
*/
public String getName() {
return clsName;
}
/**
* Indicates whether classfile data was available when this object was created.
*
* @return {@code true} if the class was parsed from class-file data; otherwise, {@code false}
*/
public boolean isAvailable() {
return available;
}
/**
* Returns the interfaces implemented by this class.
*
* @return the implemented interface metadata
*/
public List<ZClass> getInterfaces() {
return ifTypes;
}
/**
* Returns the superclass metadata.
*
* @return the superclass, or {@code null} if no superclass is present
*/
public ZClass getSuperClass() {
return supType;
}
/**
* Returns the types of the class's runtime-visible annotations.
*
* @return the class's annotation types
*/
public List<ZClass> getAnnotations() {
return annTypes;
}
/**
* Returns the fields declared by this class.
*
* @return all field metadata as a list
*/
public List<ZField> getFields() {
return fields;
}
/**
* Returns the methods declared by this class.
*
* @return all method metadata as a list
*/
public List<ZMethod> getMethods() {
return methods;
}
////////////////////////////////////////////////////////////////////////////////////////////////
// Helper methods
private void prepareSuperClass(ZClassContext clsCtxt, ClassModel clsMod) {
supType = null;
Optional<ClassEntry> optSup = clsMod.superclass();
if(optSup.isPresent()) {
ClassEntry ce = optSup.get();
String pclsName = ce.asInternalName().replace('/', '.');
supType = clsCtxt.getZClass(pclsName);
}
}
private void prepareInterfaces(ZClassContext clsCtxt, ClassModel clsMod) {
ifTypes = new ArrayList<>();
List<ClassEntry> ifentries = clsMod.interfaces();
if(ifentries == null) {
return;
}
for (ClassEntry ce : ifentries) {
String iclsName = ce.asInternalName().replace('/', '.');
ZClass iftype = clsCtxt.getZClass(iclsName);
if (iftype != null) {
ifTypes.add(iftype);
}
}
}
private void prepareAnnotations(ZClassContext clsCtxt, ClassModel clsMod) {
annTypes = new ArrayList<>();
clsMod.forEach(ce -> {
if(ce instanceof RuntimeVisibleAnnotationsAttribute annAtr) {
List<Annotation> annotations = annAtr.annotations();
for (Annotation ann : annotations) {
ClassDesc desc = ann.classSymbol();
String annType = desc.packageName().isEmpty() ?
desc.displayName() : desc.packageName() + "." + desc.displayName();
annType = annType.replace('/', '.');
ZClass annCls = clsCtxt.getZClass(annType);
if(annCls != null) {
annTypes.add(annCls);
}
}
}
});
}
private void prepareFields(ZClassContext clsCtxt, ClassModel clsMod) {
fields = new ArrayList<>();
clsMod.forEach(ce -> {
if(ce instanceof FieldModel) {
ZField zfld = new ZField(clsCtxt, (FieldModel) ce);
fields.add(zfld);
}
});
}
private void prepareMethods(ZClassContext clsCtxt, ClassModel clsMod) {
methods = new ArrayList<>();
clsMod.forEach(ce -> {
if(ce instanceof MethodModel) {
ZMethod zmthd = new ZMethod(clsCtxt, (MethodModel) ce);
methods.add(zmthd);
}
});
}
}