Skip to content

Commit

Permalink
Refactored modifier handling. Added rich object methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Oct 16, 2016
1 parent a86d302 commit d7b1ae7
Show file tree
Hide file tree
Showing 27 changed files with 454 additions and 245 deletions.
@@ -1,5 +1,6 @@
package net.bytebuddy.description; package net.bytebuddy.description;


import net.bytebuddy.description.modifier.*;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;


/** /**
Expand Down Expand Up @@ -33,6 +34,13 @@ public interface ModifierReviewable {
*/ */
boolean isSynthetic(); boolean isSynthetic();


/**
* Returns this objects synthetic state.
*
* @return This objects synthetic state.
*/
SyntheticState getSyntheticState();

/** /**
* A modifier reviewable for a {@link ByteCodeElement}, i.e. a type, a field or a method. * A modifier reviewable for a {@link ByteCodeElement}, i.e. a type, a field or a method.
*/ */
Expand Down Expand Up @@ -79,6 +87,20 @@ interface OfByteCodeElement extends ModifierReviewable {
* @return {@code true} if the modifier described by this object represents the deprecated flag. * @return {@code true} if the modifier described by this object represents the deprecated flag.
*/ */
boolean isDeprecated(); boolean isDeprecated();

/**
* Return's this byte code element's ownership.
*
* @return This byte code element's ownership.
*/
Ownership getOwnership();

/**
* Returns this byte code element's visibility.
*
* @return This byte code element's visibility.
*/
Visibility getVisibility();
} }


/** /**
Expand Down Expand Up @@ -107,6 +129,13 @@ interface OfEnumeration extends OfByteCodeElement {
* @return {@code true} if the modifier described by this object represents the enum flag. * @return {@code true} if the modifier described by this object represents the enum flag.
*/ */
boolean isEnum(); boolean isEnum();

/**
* Returns this byte code element's enumeration state.
*
* @return This byte code element's enumeration state.
*/
EnumerationState getEnumerationState();
} }


/** /**
Expand All @@ -127,6 +156,13 @@ interface ForTypeDefinition extends OfAbstraction, OfEnumeration {
* @return {@code true} if the modifier described by this object represents the annotation flag. * @return {@code true} if the modifier described by this object represents the annotation flag.
*/ */
boolean isAnnotation(); boolean isAnnotation();

/**
* Returns this type's manifestation.
*
* @return This type's manifestation.
*/
TypeManifestation getTypeManifestation();
} }


/** /**
Expand All @@ -147,6 +183,13 @@ interface ForFieldDescription extends OfEnumeration {
* @return {@code true} if the modifier described by this object represents the transient flag. * @return {@code true} if the modifier described by this object represents the transient flag.
*/ */
boolean isTransient(); boolean isTransient();

/**
* Returns this field's manifestation.
*
* @return This field's manifestation.
*/
FieldManifestation getFieldManifestation();
} }


/** /**
Expand All @@ -162,18 +205,18 @@ interface ForMethodDescription extends OfAbstraction {
boolean isSynchronized(); boolean isSynchronized();


/** /**
* Specifies if the modifier described by this object is {@code native}. * Specifies if the modifier described by this object represents the var args flag.
* *
* @return {@code true} if the modifier described by this object is {@code native}. * @return {@code true} if the modifier described by this object represents the var args flag.
*/ */
boolean isNative(); boolean isVarArgs();


/** /**
* Specifies if the modifier described by this object represents the var args flag. * Specifies if the modifier described by this object is {@code native}.
* *
* @return {@code true} if the modifier described by this object represents the var args flag. * @return {@code true} if the modifier described by this object is {@code native}.
*/ */
boolean isVarArgs(); boolean isNative();


/** /**
* Specifies if the modifier described by this object represents the bridge flag. * Specifies if the modifier described by this object represents the bridge flag.
Expand All @@ -188,6 +231,27 @@ interface ForMethodDescription extends OfAbstraction {
* @return {@code true} if the modifier described by this object is {@code strictfp}. * @return {@code true} if the modifier described by this object is {@code strictfp}.
*/ */
boolean isStrict(); boolean isStrict();

/**
* Returns this method's synchronization state.
*
* @return This method's synchronization state.
*/
SynchronizationState getSynchronizationState();

/**
* Returns this method's strictness in floating-point computation.
*
* @return This method's strictness in floating-point computation.
*/
MethodStrictness getMethodStrictness();

/**
* Returns this method's manifestation.
*
* @return This method's manifestation.
*/
MethodManifestation getMethodManifestation();
} }


/** /**
Expand All @@ -201,6 +265,20 @@ interface ForParameterDescription extends ModifierReviewable {
* @return {@code true} if the modifier described by this object is mandated. * @return {@code true} if the modifier described by this object is mandated.
*/ */
boolean isMandated(); boolean isMandated();

/**
* Returns this parameter's manifestation.
*
* @return This parameter's manifestation.
*/
ParameterManifestation getParameterManifestation();

/**
* Returns this parameter's provisioning state.
*
* @return This parameter's provisioning state.
*/
ProvisioningState getProvisioningState();
} }


/** /**
Expand Down Expand Up @@ -308,6 +386,133 @@ public boolean isVarArgs() {
return matchesMask(Opcodes.ACC_VARARGS); return matchesMask(Opcodes.ACC_VARARGS);
} }


@Override
public SyntheticState getSyntheticState() {
return isSynthetic()
? SyntheticState.SYNTHETIC
: SyntheticState.PLAIN;
}

@Override
public Visibility getVisibility() {
int modifiers = getModifiers();
switch (modifiers & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED | Opcodes.ACC_PRIVATE)) {
case Opcodes.ACC_PUBLIC:
return Visibility.PUBLIC;
case Opcodes.ACC_PROTECTED:
return Visibility.PROTECTED;
case EMPTY_MASK:
return Visibility.PACKAGE_PRIVATE;
case Opcodes.ACC_PRIVATE:
return Visibility.PRIVATE;
default:
throw new IllegalStateException("Unexpected modifiers: " + modifiers);
}
}

@Override
public Ownership getOwnership() {
return isStatic()
? Ownership.STATIC
: Ownership.MEMBER;
}

@Override
public EnumerationState getEnumerationState() {
return isEnum()
? EnumerationState.ENUMERATION
: EnumerationState.PLAIN;
}

@Override
public TypeManifestation getTypeManifestation() {
int modifiers = getModifiers();
switch (modifiers & (Opcodes.ACC_ANNOTATION | Opcodes.ACC_INTERFACE | Opcodes.ACC_ABSTRACT | Opcodes.ACC_FINAL)) {
case Opcodes.ACC_FINAL:
return TypeManifestation.FINAL;
case Opcodes.ACC_ABSTRACT:
return TypeManifestation.ABSTRACT;
case Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE:
return TypeManifestation.INTERFACE;
case Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE | Opcodes.ACC_ANNOTATION:
return TypeManifestation.ANNOTATION;
case EMPTY_MASK:
return TypeManifestation.PLAIN;
default:
throw new IllegalStateException("Unexpected modifiers: " + modifiers);
}
}

@Override
public FieldManifestation getFieldManifestation() {
int modifiers = getModifiers();
switch (modifiers & (Opcodes.ACC_VOLATILE | Opcodes.ACC_FINAL | Opcodes.ACC_TRANSIENT)) {
case Opcodes.ACC_FINAL:
return FieldManifestation.FINAL;
case Opcodes.ACC_VOLATILE:
return FieldManifestation.VOLATILE;
case Opcodes.ACC_TRANSIENT:
return FieldManifestation.TRANSIENT;
case Opcodes.ACC_TRANSIENT | Opcodes.ACC_VOLATILE:
return FieldManifestation.VOLATILE_TRANSIENT;
case EMPTY_MASK:
return FieldManifestation.PLAIN;
default:
throw new IllegalStateException("Unexpected modifiers: " + modifiers);
}
}

@Override
public SynchronizationState getSynchronizationState() {
return isSynchronized()
? SynchronizationState.SYNCHRONIZED
: SynchronizationState.PLAIN;
}

@Override
public MethodManifestation getMethodManifestation() {
int modifiers = getModifiers();
switch (modifiers & (Opcodes.ACC_NATIVE | Opcodes.ACC_ABSTRACT | Opcodes.ACC_FINAL | Opcodes.ACC_BRIDGE)) {
case Opcodes.ACC_NATIVE | Opcodes.ACC_FINAL:
return MethodManifestation.FINAL_NATIVE;
case Opcodes.ACC_NATIVE:
return MethodManifestation.NATIVE;
case Opcodes.ACC_FINAL:
return MethodManifestation.FINAL;
case Opcodes.ACC_BRIDGE:
return MethodManifestation.BRIDGE;
case Opcodes.ACC_BRIDGE | Opcodes.ACC_FINAL:
return MethodManifestation.FINAL_BRIDGE;
case Opcodes.ACC_ABSTRACT:
return MethodManifestation.ABSTRACT;
case EMPTY_MASK:
return MethodManifestation.PLAIN;
default:
throw new IllegalStateException("Unexpected modifiers: " + modifiers);
}
}

@Override
public MethodStrictness getMethodStrictness() {
return isStrict()
? MethodStrictness.STRICT
: MethodStrictness.PLAIN;
}

@Override
public ParameterManifestation getParameterManifestation() {
return isFinal()
? ParameterManifestation.FINAL
: ParameterManifestation.PLAIN;
}

@Override
public ProvisioningState getProvisioningState() {
return isMandated()
? ProvisioningState.MANDATED
: ProvisioningState.PLAIN;
}

/** /**
* Checks if a mask is matched by this instance. * Checks if a mask is matched by this instance.
* *
Expand Down
Expand Up @@ -31,16 +31,6 @@ public enum EnumerationState implements ModifierContributor.ForType, ModifierCon
this.mask = mask; this.mask = mask;
} }


/**
* Creates an enumeration state from a boolean value indicating if a type or member is supposed to be synthetic.
*
* @param enumeration {@code true} if the state is supposed to describe an enumeration.
* @return The corresponding synthetic state.
*/
public static EnumerationState is(boolean enumeration) {
return enumeration ? ENUMERATION : PLAIN;
}

@Override @Override
public int getMask() { public int getMask() {
return mask; return mask;
Expand Down
Expand Up @@ -20,7 +20,17 @@ public enum FieldManifestation implements ModifierContributor.ForField {
/** /**
* Modifier for a volatile field. * Modifier for a volatile field.
*/ */
VOLATILE(Opcodes.ACC_VOLATILE); VOLATILE(Opcodes.ACC_VOLATILE),

/**
* Modifier for a transient field.
*/
TRANSIENT(Opcodes.ACC_TRANSIENT),

/**
* Modifier for a volatile, transient field.
*/
VOLATILE_TRANSIENT(Opcodes.ACC_VOLATILE | Opcodes.ACC_TRANSIENT);


/** /**
* The mask the modifier contributor. * The mask the modifier contributor.
Expand Down Expand Up @@ -70,12 +80,21 @@ public boolean isVolatile() {
} }


/** /**
* Returns {@code true} if this manifestation represents a field that is neither {@code final} or {@code volatile}. * Returns {@code true} if this manifestation represents a field that is {@code transient}.
*
* @return {@code true} if this manifestation represents a field that is {@code transient}.
*/
public boolean isTransient() {
return (mask & Opcodes.ACC_TRANSIENT) != 0;
}

/**
* Returns {@code true} if this manifestation represents a field that is neither {@code final}, {@code transient} or {@code volatile}.
* *
* @return {@code true} if this manifestation represents a field that is neither {@code final} or {@code volatile}. * @return {@code true} if this manifestation represents a field that is neither {@code final}, {@code transient} or {@code volatile}.
*/ */
public boolean isPlain() { public boolean isPlain() {
return !(isFinal() || isVolatile()); return !(isFinal() || isVolatile() || isTransient());
} }


@Override @Override
Expand Down

0 comments on commit d7b1ae7

Please sign in to comment.