From 75f05732bbd26887c261904d8db45f68214a9d4a Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Sun, 3 Feb 2013 04:08:30 +0100 Subject: [PATCH] Improve the toString()-functionality of each fuzzy contract. --- .../player/PlayerInjectionHandler.java | 1 - .../reflect/fuzzy/AbstractFuzzyMember.java | 50 ++++++++++++++++++- .../reflect/fuzzy/FuzzyClassContract.java | 19 +++++++ .../reflect/fuzzy/FuzzyFieldContract.java | 11 ++++ .../protocol/reflect/fuzzy/FuzzyMatchers.java | 16 ++++++ .../reflect/fuzzy/FuzzyMethodContract.java | 21 ++++++++ 6 files changed, 116 insertions(+), 2 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/PlayerInjectionHandler.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/PlayerInjectionHandler.java index 85420784..f324f712 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/PlayerInjectionHandler.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/PlayerInjectionHandler.java @@ -39,7 +39,6 @@ import com.comphenix.protocol.injector.ListenerInvoker; import com.comphenix.protocol.injector.PlayerLoggedOutException; import com.comphenix.protocol.injector.PacketFilterManager.PlayerInjectHooks; import com.comphenix.protocol.injector.player.TemporaryPlayerFactory.InjectContainer; -import com.comphenix.protocol.utility.MinecraftReflection; import com.google.common.base.Predicate; import com.google.common.collect.Maps; 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 b4c0c39c..0aa97a88 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 @@ -1,10 +1,13 @@ package com.comphenix.protocol.reflect.fuzzy; import java.lang.reflect.Member; +import java.util.Map; import java.util.regex.Pattern; import javax.annotation.Nonnull; +import com.google.common.collect.Maps; + /** * Represents a matcher that matches members. * @@ -201,12 +204,17 @@ public abstract class AbstractFuzzyMember extends AbstractFuzz int mods = value.getModifiers(); // Match accessibility and name - return (mods & modifiersRequired) != 0 && + return (mods & modifiersRequired) == modifiersRequired && (mods & modifiersBanned) == 0 && declaringMatcher.isMatch(value.getDeclaringClass(), value) && isNameMatch(value.getName()); } + /** + * Determine if a given name matches the current member matcher. + * @param name - the name to match. + * @return TRUE if the name matches, FALSE otherwise. + */ private boolean isNameMatch(String name) { if (nameRegex == null) return true; @@ -223,4 +231,44 @@ public abstract class AbstractFuzzyMember extends AbstractFuzz // NULL is zero return declaringMatcher.getRoundNumber(); } + + @Override + public String toString() { + return getKeyValueView().toString(); + } + + /** + * Generate a view of this matcher as a key-value map. + *

+ * Used by {@link #toString()} to print a representation of this object. + * @return A modifiable key-value view. + */ + protected Map getKeyValueView() { + Map map = Maps.newLinkedHashMap(); + + // Build our representation + if (modifiersRequired != Integer.MAX_VALUE || modifiersBanned != 0) { + map.put("modifiers", String.format("[required: %s, banned: %s]", + getBitView(modifiersRequired, 16), + getBitView(modifiersBanned, 16)) + ); + } + if (nameRegex != null) { + map.put("name", nameRegex.pattern()); + } + if (declaringMatcher != ExactClassMatcher.MATCH_ALL) { + map.put("declaring", declaringMatcher); + } + + return map; + } + + private static String getBitView(int value, int bits) { + if (bits < 0 || bits > 31) + throw new IllegalArgumentException("Bits must be a value between 0 and 32"); + + // Extract our needed bits + int snipped = value & ((1 << bits) - 1); + return Integer.toBinaryString(snipped); + } } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/FuzzyClassContract.java b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/FuzzyClassContract.java index 4d803b1c..bb1bfcdb 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/FuzzyClassContract.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/FuzzyClassContract.java @@ -4,11 +4,14 @@ import java.lang.reflect.Field; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Map; import com.comphenix.protocol.reflect.FuzzyReflection; import com.comphenix.protocol.reflect.MethodInfo; +import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; +import com.google.common.collect.Maps; /** * Determine if a given class implements a given fuzzy (duck typed) contract. @@ -218,4 +221,20 @@ public class FuzzyClassContract extends AbstractFuzzyMatcher> { // Failure return -1; } + + @Override + public String toString() { + Map params = Maps.newLinkedHashMap(); + + if (fieldContracts.size() > 0) { + params.put("fields", fieldContracts); + } + if (methodContracts.size() > 0) { + params.put("methods", methodContracts); + } + if (constructorContracts.size() > 0) { + params.put("constructors", constructorContracts); + } + return "{\n " + Joiner.on(", \n ").join(params.entrySet()) + "\n}"; + } } 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 ad5bd16c..62e12afc 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 @@ -1,6 +1,7 @@ package com.comphenix.protocol.reflect.fuzzy; import java.lang.reflect.Field; +import java.util.Map; import java.util.regex.Pattern; import javax.annotation.Nonnull; @@ -150,4 +151,14 @@ public class FuzzyFieldContract extends AbstractFuzzyMember { return combineRounds(super.calculateRoundNumber(), typeMatcher.calculateRoundNumber()); } + + @Override + protected Map getKeyValueView() { + Map member = super.getKeyValueView(); + + if (typeMatcher != ExactClassMatcher.MATCH_ALL) { + member.put("type", typeMatcher); + } + return member; + } } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/FuzzyMatchers.java b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/FuzzyMatchers.java index d8dd70de..bad44d31 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/FuzzyMatchers.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/FuzzyMatchers.java @@ -4,6 +4,7 @@ import java.lang.reflect.Member; import java.util.Set; import java.util.regex.Pattern; +import com.google.common.base.Joiner; import com.google.common.collect.Sets; /** @@ -56,6 +57,11 @@ public class FuzzyMatchers { } return roundNumber; } + + @Override + public String toString() { + return String.format("match any: %s", Joiner.on(",").join(classes)); + } }; } @@ -97,6 +103,11 @@ public class FuzzyMatchers { protected int calculateRoundNumber() { return -priority; } + + @Override + public String toString() { + return "class name of " + regex.toString(); + } }; } @@ -133,6 +144,11 @@ public class FuzzyMatchers { // We match a very specific type return -100; } + + @Override + public String toString() { + return "match parent class"; + } }; } } 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 50cca6b6..4e08f226 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 @@ -2,6 +2,7 @@ package com.comphenix.protocol.reflect.fuzzy; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.regex.Pattern; import javax.annotation.Nonnull; @@ -493,4 +494,24 @@ public class FuzzyMethodContract extends AbstractFuzzyMember { return combineRounds(super.calculateRoundNumber(), current); } + + @Override + protected Map getKeyValueView() { + Map member = super.getKeyValueView(); + + // Only add fields that are actual contraints + if (returnMatcher != ExactClassMatcher.MATCH_ALL) { + member.put("return", returnMatcher); + } + if (paramMatchers.size() > 0) { + member.put("params", paramMatchers); + } + if (exceptionMatchers.size() > 0) { + member.put("exceptions", exceptionMatchers); + } + if (paramCount != null) { + member.put("paramCount", paramCount); + } + return member; + } }