下面列出了java.lang.reflect.Modifier#INTERFACE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static int translateAccessFlags(int accessFlags) {
int f = 0;
f |= (AccessFlags.ABSTRACT.isSet(accessFlags) ? Modifier.ABSTRACT : 0);
// f |= (AccessFlags.ANNOTATION.isSet(accessFlags) ? Modifier.ANNOTATION : 0);
// f |= (AccessFlags.BRIDGE.isSet(accessFlags) ? Modifier.BRIDGE : 0);
// f |= (AccessFlags.CONSTRUCTOR.isSet(accessFlags) ? Modifier.CONSTRUCTOR : 0);
// f |= (AccessFlags.DECLARED_SYNCHRONIZED.isSet(accessFlags) ? Modifier.DECLARED_SYNCHRONIZED : 0);
// f |= (AccessFlags.ENUM.isSet(accessFlags) ? Modifier.ENUM : 0);
f |= (AccessFlags.FINAL.isSet(accessFlags) ? Modifier.FINAL : 0);
f |= (AccessFlags.INTERFACE.isSet(accessFlags) ? Modifier.INTERFACE : 0);
f |= (AccessFlags.NATIVE.isSet(accessFlags) ? Modifier.NATIVE : 0);
f |= (AccessFlags.PRIVATE.isSet(accessFlags) ? Modifier.PRIVATE : 0);
f |= (AccessFlags.PROTECTED.isSet(accessFlags) ? Modifier.PROTECTED : 0);
f |= (AccessFlags.PUBLIC.isSet(accessFlags) ? Modifier.PUBLIC : 0);
f |= (AccessFlags.STATIC.isSet(accessFlags) ? Modifier.STATIC : 0);
f |= (AccessFlags.STRICTFP.isSet(accessFlags) ? Modifier.STRICT : 0);
f |= (AccessFlags.SYNCHRONIZED.isSet(accessFlags) ? Modifier.SYNCHRONIZED : 0);
// f |= (AccessFlags.SYNTHETIC.isSet(accessFlags) ? Modifier.SYNTHETIC : 0);
f |= (AccessFlags.TRANSIENT.isSet(accessFlags) ? Modifier.TRANSIENT : 0);
// f |= (AccessFlags.VARARGS.isSet(accessFlags) ? Modifier.VARARGS : 0);
f |= (AccessFlags.VOLATILE.isSet(accessFlags) ? Modifier.VOLATILE : 0);
return f;
}
public static void main(String [] argv) {
int allMods = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL |
Modifier.TRANSIENT | Modifier.VOLATILE | Modifier.SYNCHRONIZED |
Modifier.NATIVE | Modifier.STRICT | Modifier.INTERFACE;
String allModsString = "public protected private abstract static " +
"final transient volatile synchronized native strictfp interface";
/* zero should have an empty string */
testString(0, "");
/* test to make sure all modifiers print out in the proper order */
testString(allMods, allModsString);
/* verify no extraneous modifiers are printed */
testString(~0, allModsString);
}
@MethodSubstitution(isStatic = false)
public static Class<?> getSuperclass(final Class<?> thisObj) {
KlassPointer klass = ClassGetHubNode.readClass(thisObj);
if (!klass.isNull()) {
int accessFlags = klass.readInt(klassAccessFlagsOffset(INJECTED_VMCONFIG), KLASS_ACCESS_FLAGS_LOCATION);
if ((accessFlags & Modifier.INTERFACE) == 0) {
if (klassIsArray(klass)) {
return Object.class;
} else {
KlassPointer superKlass = klass.readKlassPointer(klassSuperKlassOffset(INJECTED_VMCONFIG), KLASS_SUPER_KLASS_LOCATION);
if (superKlass.isNull()) {
return null;
} else {
return readJavaMirror(superKlass);
}
}
}
} else {
// Class for primitive type
}
return null;
}
public int getModifiers(){
int mods = 0;
if ((u2innerClassAccessFlags & 0x0001) == 0x0001) mods |= Modifier.PUBLIC;
if ((u2innerClassAccessFlags & 0x0002) == 0x0002) mods |= Modifier.PRIVATE;
if ((u2innerClassAccessFlags & 0x0004) == 0x0004) mods |= Modifier.PROTECTED;
if ((u2innerClassAccessFlags & 0x0008) == 0x0008) mods |= Modifier.STATIC;
if ((u2innerClassAccessFlags & 0x0010) == 0x0010) mods |= Modifier.FINAL;
if ((u2innerClassAccessFlags & 0x0200) == 0x0200) mods |= Modifier.INTERFACE;
if ((u2innerClassAccessFlags & 0x0400) == 0x0400) mods |= Modifier.ABSTRACT;
return mods;
}
/**
* Get a string representation of this object.
*
* @return the string representation
*/
public String toString()
{
StringBuilder buf = new StringBuilder();
if ((modifier & Modifier.PUBLIC) != 0)
buf.append ("public ");
if ((modifier & Modifier.ABSTRACT) != 0)
buf.append ("abstract ");
if ((modifier & Modifier.INTERFACE) != 0)
buf.append ("interface ");
else
buf.append ("class ");
buf.append (className);
String sep = " ";
if (implementedInterfaces.length > 0)
{
buf.append (" implements");
for (String intf : implementedInterfaces)
{
buf.append (sep);
buf.append (intf);
}
}
if ((superClassName != null) &&
(! superClassName.equals ("java.lang.Object")))
{
buf.append (sep);
buf.append ("extends ");
buf.append (superClassName);
}
return (buf.toString());
}
/**
* Convert modifier bits from private coding used by
* the compiler to that of java.lang.reflect.Modifier.
*/
static int translateModifiers(long flags) {
int result = 0;
if ((flags & Flags.ABSTRACT) != 0)
result |= Modifier.ABSTRACT;
if ((flags & Flags.FINAL) != 0)
result |= Modifier.FINAL;
if ((flags & Flags.INTERFACE) != 0)
result |= Modifier.INTERFACE;
if ((flags & Flags.NATIVE) != 0)
result |= Modifier.NATIVE;
if ((flags & Flags.PRIVATE) != 0)
result |= Modifier.PRIVATE;
if ((flags & Flags.PROTECTED) != 0)
result |= Modifier.PROTECTED;
if ((flags & Flags.PUBLIC) != 0)
result |= Modifier.PUBLIC;
if ((flags & Flags.STATIC) != 0)
result |= Modifier.STATIC;
if ((flags & Flags.SYNCHRONIZED) != 0)
result |= Modifier.SYNCHRONIZED;
if ((flags & Flags.TRANSIENT) != 0)
result |= Modifier.TRANSIENT;
if ((flags & Flags.VOLATILE) != 0)
result |= Modifier.VOLATILE;
return result;
}
/**
* is object accessible?
*
* @param obj Object
* @throws StructException
*/
public static void isAccessible(Object obj) throws StructException{
int modifiers = obj.getClass().getModifiers();
if ((modifiers & Modifier.PUBLIC) == 0)
throw new StructException("Struct operations are only accessible for public classes. Class: " + obj.getClass().getName());
if ((modifiers & (Modifier.INTERFACE | Modifier.ABSTRACT)) != 0)
throw new StructException("Struct operations are not accessible for abstract classes and interfaces. Class: " + obj.getClass().getName());
}
/**
* Convert modifier bits from private coding used by
* the compiler to that of java.lang.reflect.Modifier.
*/
static int translateModifiers(long flags) {
int result = 0;
if ((flags & Flags.ABSTRACT) != 0)
result |= Modifier.ABSTRACT;
if ((flags & Flags.FINAL) != 0)
result |= Modifier.FINAL;
if ((flags & Flags.INTERFACE) != 0)
result |= Modifier.INTERFACE;
if ((flags & Flags.NATIVE) != 0)
result |= Modifier.NATIVE;
if ((flags & Flags.PRIVATE) != 0)
result |= Modifier.PRIVATE;
if ((flags & Flags.PROTECTED) != 0)
result |= Modifier.PROTECTED;
if ((flags & Flags.PUBLIC) != 0)
result |= Modifier.PUBLIC;
if ((flags & Flags.STATIC) != 0)
result |= Modifier.STATIC;
if ((flags & Flags.SYNCHRONIZED) != 0)
result |= Modifier.SYNCHRONIZED;
if ((flags & Flags.TRANSIENT) != 0)
result |= Modifier.TRANSIENT;
if ((flags & Flags.VOLATILE) != 0)
result |= Modifier.VOLATILE;
return result;
}
/**
* Convert modifier bits from private coding used by
* the compiler to that of java.lang.reflect.Modifier.
*/
static int translateModifiers(long flags) {
int result = 0;
if ((flags & Flags.ABSTRACT) != 0)
result |= Modifier.ABSTRACT;
if ((flags & Flags.FINAL) != 0)
result |= Modifier.FINAL;
if ((flags & Flags.INTERFACE) != 0)
result |= Modifier.INTERFACE;
if ((flags & Flags.NATIVE) != 0)
result |= Modifier.NATIVE;
if ((flags & Flags.PRIVATE) != 0)
result |= Modifier.PRIVATE;
if ((flags & Flags.PROTECTED) != 0)
result |= Modifier.PROTECTED;
if ((flags & Flags.PUBLIC) != 0)
result |= Modifier.PUBLIC;
if ((flags & Flags.STATIC) != 0)
result |= Modifier.STATIC;
if ((flags & Flags.SYNCHRONIZED) != 0)
result |= Modifier.SYNCHRONIZED;
if ((flags & Flags.TRANSIENT) != 0)
result |= Modifier.TRANSIENT;
if ((flags & Flags.VOLATILE) != 0)
result |= Modifier.VOLATILE;
return result;
}
@MethodSubstitution(isStatic = false)
public static boolean isInterface(final Class<?> thisObj) {
KlassPointer klass = ClassGetHubNode.readClass(thisObj);
if (klass.isNull()) {
// Class for primitive type
return false;
} else {
int accessFlags = klass.readInt(klassAccessFlagsOffset(INJECTED_VMCONFIG), KLASS_ACCESS_FLAGS_LOCATION);
return (accessFlags & Modifier.INTERFACE) != 0;
}
}
/**
* Convert modifier bits from private coding used by
* the compiler to that of java.lang.reflect.Modifier.
*/
static int translateModifiers(long flags) {
int result = 0;
if ((flags & Flags.ABSTRACT) != 0)
result |= Modifier.ABSTRACT;
if ((flags & Flags.FINAL) != 0)
result |= Modifier.FINAL;
if ((flags & Flags.INTERFACE) != 0)
result |= Modifier.INTERFACE;
if ((flags & Flags.NATIVE) != 0)
result |= Modifier.NATIVE;
if ((flags & Flags.PRIVATE) != 0)
result |= Modifier.PRIVATE;
if ((flags & Flags.PROTECTED) != 0)
result |= Modifier.PROTECTED;
if ((flags & Flags.PUBLIC) != 0)
result |= Modifier.PUBLIC;
if ((flags & Flags.STATIC) != 0)
result |= Modifier.STATIC;
if ((flags & Flags.SYNCHRONIZED) != 0)
result |= Modifier.SYNCHRONIZED;
if ((flags & Flags.TRANSIENT) != 0)
result |= Modifier.TRANSIENT;
if ((flags & Flags.VOLATILE) != 0)
result |= Modifier.VOLATILE;
return result;
}
/**
* Returns the Java language modifiers for this class or interface, encoded
* in an integer. The modifiers consist of the Java Virtual Machine's
* constants for {@code public}, {@code protected},
* {@code private}, {@code final}, {@code static},
* {@code abstract} and {@code interface}; they should be decoded
* using the methods of class {@code Modifier}.
*
* <p> If the underlying class is an array class, then its
* {@code public}, {@code private} and {@code protected}
* modifiers are the same as those of its component type. If this
* {@code Class} represents a primitive type or void, its
* {@code public} modifier is always {@code true}, and its
* {@code protected} and {@code private} modifiers are always
* {@code false}. If this object represents an array class, a
* primitive type or void, then its {@code final} modifier is always
* {@code true} and its interface modifier is always
* {@code false}. The values of its other modifiers are not determined
* by this specification.
*
* <p> The modifier encodings are defined in <em>The Java Virtual Machine
* Specification</em>, table 4.1.
*
* @return the {@code int} representing the modifiers for this class
* @see java.lang.reflect.Modifier
* @since JDK1.1
*/
public int getModifiers() {
// Array classes inherit modifiers from their component types, but in the case of arrays
// of an inner class, the class file may contain "fake" access flags because it's not valid
// for a top-level class to private, say. The real access flags are stored in the InnerClass
// attribute, so we need to make sure we drill down to the inner class: the accessFlags
// field is not the value we want to return, and the synthesized array class does not itself
// have an InnerClass attribute. https://code.google.com/p/android/issues/detail?id=56267
if (isArray()) {
int componentModifiers = getComponentType().getModifiers();
if ((componentModifiers & Modifier.INTERFACE) != 0) {
componentModifiers &= ~(Modifier.INTERFACE | Modifier.STATIC);
}
return Modifier.ABSTRACT | Modifier.FINAL | componentModifiers;
}
int JAVA_FLAGS_MASK = 0xffff;
int modifiers = this.getInnerClassFlags(accessFlags & JAVA_FLAGS_MASK);
return modifiers & JAVA_FLAGS_MASK;
}
/**
* 计算默认的 serialVersionUID
*
* @see java.io.ObjectStreamClass#lookup(Class)
* @see java.io.ObjectStreamClass#computeDefaultSUID(Class)
*/
public static long computeDefaultSUID(String className, List<Field> fields) {
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
// simple class name
dout.writeUTF(className);
int classMods = Modifier.PUBLIC & (Modifier.PUBLIC | Modifier.FINAL | Modifier.INTERFACE | Modifier.ABSTRACT);
dout.writeInt(classMods);
// interface name
dout.writeUTF("java.io.Serializable");
// fields
// fields.sort(Comparator.comparing(Field::getField));
for (Field field : fields) {
int mods = Modifier.PRIVATE &
(Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED |
Modifier.STATIC | Modifier.FINAL | Modifier.VOLATILE |
Modifier.TRANSIENT);
if (((mods & Modifier.PRIVATE) == 0) ||
((mods & (Modifier.STATIC | Modifier.TRANSIENT)) == 0)) {
dout.writeUTF(field.getField());
dout.writeInt(mods);
dout.writeUTF(field.getFieldType());
}
}
// method ignore
dout.flush();
MessageDigest md = MessageDigest.getInstance("SHA");
byte[] hashBytes = md.digest(bout.toByteArray());
long hash = 0;
for (int i = Math.min(hashBytes.length, 8) - 1; i >= 0; i--) {
hash = (hash << 8) | (hashBytes[i] & 0xFF);
}
return hash;
} catch (Exception e) {
// ignore
}
return 1;
}
/**
* Returns the Java language modifiers for this class or interface, encoded
* in an integer. The modifiers consist of the Java Virtual Machine's
* constants for {@code public}, {@code protected},
* {@code private}, {@code final}, {@code static},
* {@code abstract} and {@code interface}; they should be decoded
* using the methods of class {@code Modifier}.
*
* <p> If the underlying class is an array class, then its
* {@code public}, {@code private} and {@code protected}
* modifiers are the same as those of its component type. If this
* {@code Class} represents a primitive type or void, its
* {@code public} modifier is always {@code true}, and its
* {@code protected} and {@code private} modifiers are always
* {@code false}. If this object represents an array class, a
* primitive type or void, then its {@code final} modifier is always
* {@code true} and its interface modifier is always
* {@code false}. The values of its other modifiers are not determined
* by this specification.
*
* <p> The modifier encodings are defined in <em>The Java Virtual Machine
* Specification</em>, table 4.1.
*
* @return the {@code int} representing the modifiers for this class
* @see java.lang.reflect.Modifier
* @since JDK1.1
*/
public int getModifiers() {
// Array classes inherit modifiers from their component types, but in the case of arrays
// of an inner class, the class file may contain "fake" access flags because it's not valid
// for a top-level class to private, say. The real access flags are stored in the InnerClass
// attribute, so we need to make sure we drill down to the inner class: the accessFlags
// field is not the value we want to return, and the synthesized array class does not itself
// have an InnerClass attribute. https://code.google.com/p/android/issues/detail?id=56267
if (isArray()) {
int componentModifiers = getComponentType().getModifiers();
if ((componentModifiers & Modifier.INTERFACE) != 0) {
componentModifiers &= ~(Modifier.INTERFACE | Modifier.STATIC);
}
return Modifier.ABSTRACT | Modifier.FINAL | componentModifiers;
}
int JAVA_FLAGS_MASK = 0xffff;
int modifiers = this.getInnerClassFlags(accessFlags & JAVA_FLAGS_MASK);
return modifiers & JAVA_FLAGS_MASK;
}
protected String renderModifiers( StringBuilder sb, long modifiers, boolean isDefault, int defModifier )
{
if( isDefault )
{
sb.append( "default " );
}
if( (modifiers & Modifier.PUBLIC) != 0 )
{
sb.append( "public " );
}
else if( (modifiers & Modifier.PROTECTED) != 0 )
{
sb.append( "protected " );
}
else if( (modifiers & Modifier.PRIVATE) != 0 )
{
sb.append( "private " );
}
else if( defModifier != 0 )
{
renderModifiers( sb, defModifier, false, 0 );
}
// Canonical order
if( (modifiers & Modifier.ABSTRACT) != 0 )
{
sb.append( "abstract " );
}
if( (modifiers & Modifier.STATIC) != 0 )
{
sb.append( "static " );
}
if( (modifiers & Modifier.FINAL) != 0 )
{
sb.append( "final " );
}
if( (modifiers & Modifier.TRANSIENT) != 0 )
{
sb.append( "transient " );
}
if( (modifiers & Modifier.VOLATILE) != 0 )
{
sb.append( "volatile " );
}
if( (modifiers & Modifier.SYNCHRONIZED) != 0 )
{
sb.append( "synchronized " );
}
if( (modifiers & Modifier.INTERFACE) != 0 )
{
sb.append( "interface " );
}
return "";
}
/**
* Construct a new <tt>InterfaceOnlyClassFilter</tt> that will accept
* only classes that are interfaces.
*/
public InterfaceOnlyClassFilter()
{
super (Modifier.INTERFACE);
}
/**
* Convert an ASM access mask to a reflection Modifier mask.
*
* @param asmAccessMask the ASM access mask
*
* @return the Modifier mask
*/
private int convertAccessMaskToModifierMask(int asmAccessMask)
{
int modifier = 0;
// Convert the ASM access info into Reflection API modifiers.
if ((asmAccessMask & Opcodes.ACC_FINAL) != 0)
modifier |= Modifier.FINAL;
if ((asmAccessMask & Opcodes.ACC_NATIVE) != 0)
modifier |= Modifier.NATIVE;
if ((asmAccessMask & Opcodes.ACC_INTERFACE) != 0)
modifier |= Modifier.INTERFACE;
if ((asmAccessMask & Opcodes.ACC_ABSTRACT) != 0)
modifier |= Modifier.ABSTRACT;
if ((asmAccessMask & Opcodes.ACC_PRIVATE) != 0)
modifier |= Modifier.PRIVATE;
if ((asmAccessMask & Opcodes.ACC_PROTECTED) != 0)
modifier |= Modifier.PROTECTED;
if ((asmAccessMask & Opcodes.ACC_PUBLIC) != 0)
modifier |= Modifier.PUBLIC;
if ((asmAccessMask & Opcodes.ACC_STATIC) != 0)
modifier |= Modifier.STATIC;
if ((asmAccessMask & Opcodes.ACC_STRICT) != 0)
modifier |= Modifier.STRICT;
if ((asmAccessMask & Opcodes.ACC_SYNCHRONIZED) != 0)
modifier |= Modifier.SYNCHRONIZED;
if ((asmAccessMask & Opcodes.ACC_TRANSIENT) != 0)
modifier |= Modifier.TRANSIENT;
if ((asmAccessMask & Opcodes.ACC_VOLATILE) != 0)
modifier |= Modifier.VOLATILE;
return modifier;
}
/**
* Convert a string that contains lower case letter Java modifiers comma separated into an access mask.
*
* @param masks is the comma separated list of modifiers. The list can also contain the word {@code package}
* that will be translated to {@link GeciReflectionTools#PACKAGE} since there is no modifier {@code package}
* in Java.
* @param dfault the mask to return in case the {@code includes} string is empty.
* @return the mask converted from String
*/
public static int mask(String masks, int dfault) {
int modMask = 0;
if (masks == null) {
return dfault;
} else {
for (var maskString : masks.split(",", -1)) {
var maskTrimmed = maskString.trim();
switch (maskTrimmed) {
case "private":
modMask |= Modifier.PRIVATE;
break;
case "public":
modMask |= Modifier.PUBLIC;
break;
case "protected":
modMask |= Modifier.PROTECTED;
break;
case "static":
modMask |= Modifier.STATIC;
break;
case "package":
modMask |= GeciReflectionTools.PACKAGE;//reuse the bit
break;
case "abstract":
modMask |= Modifier.ABSTRACT;
break;
case "final":
modMask |= Modifier.FINAL;
break;
case "interface":
modMask |= Modifier.INTERFACE;
break;
case "synchronized":
modMask |= Modifier.SYNCHRONIZED;
break;
case "native":
modMask |= Modifier.NATIVE;
break;
case "transient":
modMask |= Modifier.TRANSIENT;
break;
case "volatile":
modMask |= Modifier.VOLATILE;
break;
default:
throw new IllegalArgumentException(maskTrimmed + " can not be used as a modifier string");
}
}
return modMask;
}
}
private static boolean isInterface(Method method) {
return ((method.getModifiers() & Modifier.INTERFACE) != 0);
}
protected static boolean isConcrete(Member member)
{
int mod = member.getModifiers();
return (mod & (Modifier.INTERFACE | Modifier.ABSTRACT)) == 0;
}