diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/AbstractFuzzyMember.java b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/AbstractFuzzyMember.java index 4e516b97..b4c0c39c 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/AbstractFuzzyMember.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/AbstractFuzzyMember.java @@ -164,6 +164,38 @@ public abstract class AbstractFuzzyMember extends AbstractFuzz this.sealed = true; } + /** + * Retrieve a bit field of every {@link java.lang.reflect.Modifier Modifier} that is required for the member to match. + * @return A required modifier bit field. + */ + public int getModifiersRequired() { + return modifiersRequired; + } + + /** + * Retrieve a bit field of every {@link java.lang.reflect.Modifier Modifier} that must not be present for the member to match. + * @return A banned modifier bit field. + */ + public int getModifiersBanned() { + return modifiersBanned; + } + + /** + * Retrieve the regular expression pattern that is used to match the name of a member. + * @return The regex matching a name, or NULL if everything matches. + */ + public Pattern getNameRegex() { + return nameRegex; + } + + /** + * Retrieve a class matcher for the declaring class of the member. + * @return An object matching the declaring class. + */ + public AbstractFuzzyMatcher> getDeclaringMatcher() { + return declaringMatcher; + } + @Override public boolean isMatch(T value, Object parent) { int mods = value.getModifiers(); diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/FuzzyFieldContract.java b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/FuzzyFieldContract.java index ca54f581..ad5bd16c 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/FuzzyFieldContract.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/FuzzyFieldContract.java @@ -118,6 +118,14 @@ public class FuzzyFieldContract extends AbstractFuzzyMember { super(); } + /** + * Retrieve the class matcher that matches the type of a field. + * @return The class matcher. + */ + public AbstractFuzzyMatcher> getTypeMatcher() { + return typeMatcher; + } + /** * Create a new field contract from the given contract. * @param other - the contract to clone. diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/FuzzyMethodContract.java b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/FuzzyMethodContract.java index 670b6139..50cca6b6 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/FuzzyMethodContract.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/FuzzyMethodContract.java @@ -9,6 +9,7 @@ import javax.annotation.Nonnull; import org.apache.commons.lang.NotImplementedException; import com.comphenix.protocol.reflect.MethodInfo; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; /** @@ -80,8 +81,8 @@ public class FuzzyMethodContract extends AbstractFuzzyMember { private AbstractFuzzyMatcher> returnMatcher = ExactClassMatcher.MATCH_ALL; // Handle parameters and exceptions - private List paramMatchers = Lists.newArrayList(); - private List exceptionMatchers = Lists.newArrayList(); + private List paramMatchers; + private List exceptionMatchers; // Expected parameter count private Integer paramCount; @@ -326,13 +327,14 @@ public class FuzzyMethodContract extends AbstractFuzzyMember { @Override @Nonnull protected FuzzyMethodContract initialMember() { + // With mutable lists return new FuzzyMethodContract(); } @Override public FuzzyMethodContract build() { member.prepareBuild(); - return new FuzzyMethodContract(member); + return immutableCopy(member); } } @@ -346,8 +348,10 @@ public class FuzzyMethodContract extends AbstractFuzzyMember { private FuzzyMethodContract() { // Only allow construction from the builder + paramMatchers = Lists.newArrayList(); + exceptionMatchers = Lists.newArrayList(); } - + private FuzzyMethodContract(FuzzyMethodContract other) { super(other); this.returnMatcher = other.returnMatcher; @@ -356,6 +360,58 @@ public class FuzzyMethodContract extends AbstractFuzzyMember { this.paramCount = other.paramCount; } + /** + * Construct a new immutable copy of the given method contract. + * @param other - the contract to clone. + * @return A immutable copy of the given contract. + */ + private static FuzzyMethodContract immutableCopy(FuzzyMethodContract other) { + FuzzyMethodContract copy = new FuzzyMethodContract(other); + + // Ensure that the lists are immutable + copy.paramMatchers = ImmutableList.copyOf(copy.paramMatchers); + copy.exceptionMatchers = ImmutableList.copyOf(copy.exceptionMatchers); + return copy; + } + + /** + * Retrieve the class matcher for the return type. + * @return Class matcher for the return type. + */ + public AbstractFuzzyMatcher> getReturnMatcher() { + return returnMatcher; + } + + /** + * Retrieve an immutable list of every parameter matcher for this method. + * @return Immutable list of every parameter matcher. + */ + public ImmutableList getParamMatchers() { + if (paramMatchers instanceof ImmutableList) + return (ImmutableList) paramMatchers; + else + throw new IllegalStateException("Lists haven't been sealed yet."); + } + + /** + * Retrieve an immutable list of every exception matcher for this method. + * @return Immutable list of every exception matcher. + */ + public List getExceptionMatchers() { + if (exceptionMatchers instanceof ImmutableList) + return exceptionMatchers; + else + throw new IllegalStateException("Lists haven't been sealed yet."); + } + + /** + * Retrieve the expected parameter count for this method. + * @return Expected parameter count, or NULL if anyting goes. + */ + public Integer getParamCount() { + return paramCount; + } + @Override protected void prepareBuild() { super.prepareBuild();