105 lines
2.7 KiB
Java
105 lines
2.7 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 dux.scanpath.v2.bc;
|
|
|
|
import java.lang.classfile.AccessFlags;
|
|
import java.lang.classfile.Annotation;
|
|
import java.lang.classfile.FieldModel;
|
|
import java.lang.classfile.attribute.RuntimeVisibleAnnotationsAttribute;
|
|
import java.lang.constant.ClassDesc;
|
|
import java.lang.reflect.AccessFlag;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
|
|
/**
|
|
* Encapsulates metadata for a field read directly from a class file.
|
|
*
|
|
* <p>The metadata includes the field name, whether it is static, and the types of its annotations
|
|
* that are visible at runtime.
|
|
*
|
|
* @author indroneel
|
|
*/
|
|
|
|
public class ZField {
|
|
|
|
private final String fldName;
|
|
private Set<AccessFlag> accessFlags;
|
|
private final List<ZClass> annTypes;
|
|
|
|
/**
|
|
* Creates field metadata from a classfile field model.
|
|
*
|
|
* @param ctxt the class context used to resolve annotation types
|
|
* @param fldMod the class-file model of the field
|
|
*/
|
|
|
|
public ZField(ZClassContext ctxt, FieldModel fldMod) {
|
|
annTypes = new ArrayList<>();
|
|
fldName = fldMod.fieldName().stringValue();
|
|
|
|
fldMod.forEach(ce -> {
|
|
if(ce instanceof AccessFlags aflgs) {
|
|
accessFlags = aflgs.flags();
|
|
}
|
|
else 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 = ctxt.getZClass(annType);
|
|
if(annCls != null) {
|
|
annTypes.add(annCls);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
ClassDesc desc = fldMod.fieldTypeSymbol();
|
|
}
|
|
|
|
/**
|
|
* Returns the field name.
|
|
*
|
|
* @return the field name
|
|
*/
|
|
|
|
public String getName() {
|
|
return fldName;
|
|
}
|
|
|
|
/**
|
|
* Indicates whether the field has the {@code static} modifier.
|
|
*
|
|
* @return {@code true} if the field is static; otherwise, {@code false}
|
|
*/
|
|
|
|
public boolean isStatic() {
|
|
return accessFlags.contains(AccessFlag.STATIC);
|
|
}
|
|
|
|
/**
|
|
* Returns the types of the field's runtime-visible annotations.
|
|
*
|
|
* @return the field's annotation types
|
|
*/
|
|
|
|
public List<ZClass> getAnnotations() {
|
|
return annTypes;
|
|
}
|
|
}
|