Commits

Eduardo Ferreira committed 709a752

Changed the project name from 'Object Sniffer' to 'Sniffer'. The project is now managed with Maven for dependency management and automated build purposes.

  • Participants
  • Parent commits 7613d22

Comments (0)

Files changed (8)

File lib/guava-13.0.1.jar

Binary file removed.
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>com.mulberrybeacon.sniffer</groupId>
+  <artifactId>sniffer</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <packaging>jar</packaging>
+
+  <name>sniffer</name>
+  <url>http://maven.apache.org</url>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>com.google.guava</groupId>
+      <artifactId>guava-primitives</artifactId>
+      <version>r03</version>
+    </dependency>
+  </dependencies>
+</project>

File src/com/mulberrybeacon/logging/ObjectSniffer.java

-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
-
-import com.google.common.primitives.Primitives;
-
-/**
- * Rudimentary logging tool. The goal is to be able to automatically log the values of the fields
- * declared by the class or interface represented by a given {@link Class} object.
- *
- * @author Eduardo Ferreira
- * @version 0.1.1
- */
-public final class ObjectSniffer {
-
-	// ~ Static fields/initializers
-	// --------------------------------------------------------------------------------------------
-	/** Canonical name of the Java {@link String} class. */
-	private static final String CLASS_STRING = "java.lang.String";
-
-	/** Error message shown in the logs when a field is inaccessible */
-	private static final String ERROR_INACCESSIBLE_FIELD = "Unable to retrieve value!";
-
-	// ~ Constructors
-	// --------------------------------------------------------------------------------------------
-	/**
-	 * Private constructor to prevent instantiation.
-	 */
-	private ObjectSniffer() {}
-
-	// ~ Methods (public)
-	// --------------------------------------------------------------------------------------------
-	/**
-	 * Analyzes the given {@link Object} instance and logs its fields (names and values).
-	 *
-	 * @param object {@link Object} instance to log
-	 * @return A {@link String} instance with the fields' names and values in XML format
-	 */
-	public static String logObject(final Object object) {
-		return logObject(object, 0);
-	}
-
-	// ~ Methods (private)
-	// --------------------------------------------------------------------------------------------
-	/**
-	 * Analyzes the given {@link Object} instance and logs its fields (names and values).
-	 *
-	 * @param object {@link Object} instance to log
-	 * @param depth Depth of the fields in the {@link Object} instance
-	 * @return A {@link String} instance with the fields' names and values in XML format
-	 */
-	private static String logObject(final Object object, final int depth) {
-
-		/* Result of logging the object fields */
-		String xLoggedFields = "\n";
-
-		/* Gets the class represented by the given object and its list of fields */
-		final Class xClass = object.getClass();
-		final Field[] xFields = xClass.getDeclaredFields();
-
-		/* Goes through the list of fields */
-		for (Field xField : xFields) {
-			final String xFieldName = xField.getName();
-			final int xModifier = xField.getModifiers();
-			Object xFieldValue = null;
-
-			try {
-				/* Gets the field value */
-				xFieldValue = isModifierInaccessible(xModifier)
-							? runGetterMethod(object, xFieldName)
-							: xField.get(object);
-			}
-			catch (Exception e) {
-				e.printStackTrace();
-			}
-
-			/* Checks if the field class is one of the following:
-			 * 1) primitive type;
-			 * 2) primitive-wrapper type;
-			 * 3) String type.
-			 */
-			final Class xFieldClass = xFieldValue.getClass();
-			if (xFieldClass.isPrimitive()
-				|| xFieldClass.getName().matches(CLASS_STRING)
-				|| Primitives.isWrapperType(xFieldClass)) {
-				xLoggedFields += PrettyPrinter.generateMarkup(xFieldName, xFieldValue.toString(),
-					depth + 1);
-			}
-			else {
-				/* TODO: Recursively invoke this method to log the subfields of the current field.
-				 * Still no code available, though.
-				 */
-			}
-		}
-
-		/* Enclose the previous set of fields with the object name */
-		return PrettyPrinter.generateMarkup(xClass.getName(), xLoggedFields, 0);
-	}
-
-	/**
-	 * Provides a very simple way to look for a getter method in case the modifier of the given
-	 * object field is either "private" or "protected".
-	 *
-	 * @param object {@link Object} instance to log
-	 * @param fieldName Field name
-	 * @return An {@link Object} instance with either the value of the field or an error message
-	 */
-	private static Object runGetterMethod(final Object object, final String fieldName) {
-
-		/* Gets the class represented by the given object and its list of methods */
-		final Class xClass = object.getClass();
-		final Method[] xMethods = xClass.getDeclaredMethods();
-
-		/* Goes through the list of methods */
-		for (Method xMethod : xMethods) {
-			final String xMethodName = xMethod.getName();
-			final int xModifier = xMethod.getModifiers();
-
-			/* Stores the original field name and its length */
-			String xFieldName = fieldName;
-			int xFieldNameLength = fieldName.length();
-
-			/* Checks if the field name has a preffix of the form "\w+_" */
-			if (fieldName.matches("\\w+_\\w+")) {
-				xFieldName = xFieldName.substring(xFieldName.indexOf("_") + 1);
-//				xFieldName = xFieldName.replaceFirst("\\w+_", "");
-				xFieldNameLength = xFieldName.length();
-			}
-
-			/* Creates a class with the set of characters in the field name (both upper- and
-			 * lower-case)
-			 */
-			xFieldName = xFieldName.toUpperCase() + xFieldName.toLowerCase();
-
-			try {
-				/* Checks if the method name contains the field name */
-				if (xMethodName.matches("[Gg][Ee][Tt][" + xFieldName + "]{" + xFieldNameLength
-					+ "}\\w*")) {
-					return xMethod.invoke(object);
-				}
-			}
-			catch (Exception e) {
-				/* The exception thrown by the {@code invoke} method could either be an
-				 * {@link IllegalAccessException} or an {@link InvocationTargetException}. For this
-				 * purpose, the for loop should just take its course and exhaust all possibilities
-				 * for a getter method.
-				 */
-				continue;
-			}
-		}
-
-		return ERROR_INACCESSIBLE_FIELD;
-	}
-
-	/**
-	 * Checks if the modifier of a {@link Field} or {@link Method} object is "private" or "protected".
-	 *
-	 * @param modifier Field or methor modifier
-	 * @return {@code True} if the modifier is "private" or "protected"; {@code False} otherwise
-	 */
-	private static boolean isModifierInaccessible(final int modifier) {
-		return (modifier == Modifier.PRIVATE || modifier == Modifier.PROTECTED);
-	}
-}

File src/com/mulberrybeacon/logging/PrettyPrinter.java

-/**
- * Pretty printer class that uses a XML logging style.
- *
- * @author Eduardo Ferreira
- * @version 0.1.1
- */
-public final class PrettyPrinter {
-
-	// ~ Static fields/initializers
-	// --------------------------------------------------------------------------------------------
-	/** Tab marker to represent a single depth level for the field tags (two whitespace characters) */
-	private static final String TAB = "  ";
-
-	/** Dummy marker to replace with the field name */
-	private static final String DUMMY = "X";
-
-	/** XML start tag */
-	private static final String START_TAG = "<" + DUMMY + ">";
-
-	/** XML end tag */
-	private static final String END_TAG = "</" + DUMMY + ">\n";
-
-	// ~ Constructors
-	// --------------------------------------------------------------------------------------------
-	/**
-	 * Private constructor to prevent instantiation.
-	 */
-	private PrettyPrinter() {}
-
-	// ~ Methods (public)
-	// --------------------------------------------------------------------------------------------
-	/**
-	 * Encloses the value of the given field with XML tags containing its name.
-	 * The result will be the following: <FIELD_NAME>FIELD_VALUE</FIELD_NAME>
-	 *
-	 * @param fieldName Name of the field
-	 * @param fieldValue Value of the field
-	 * @param depth Depth of the field
-	 * @return A {@link String} instance with the final XML layout for the field
-	 */
-	public static String generateMarkup(final String fieldName, final String fieldValue,
-		final int depth) {
-
-		final String xMarkup =
-			sumTabs(depth) +
-			START_TAG.replaceFirst(DUMMY, fieldName) +
-			fieldValue +
-			END_TAG.replaceFirst(DUMMY, fieldName);
-
-		return xMarkup;
-	}
-
-	// ~ Methods (private)
-	// --------------------------------------------------------------------------------------------
-	/**
-	 * Outputs the necessary number of tabs to represent the depth of a XML tag.
-	 * (incremental implementation)
-	 *
-	 * @param depth Depth of the XML tag
-	 * @return A {@link String} instance with the number of tabs representing the given depth
-	 */
-	private static String sumTabs(final int depth) {
-		if (depth <= 0) {
-			return "";
-		}
-
-		String xTabs = "";
-		for (int i = 0; i < depth; i++) {
-			xTabs += TAB;
-		}
-
-		return xTabs;
-	}
-
-	/**
-	 * Outputs the necessary number of tabs to represent the depth of a XML tag.
-	 * (recursive implementation)
-	 *
-	 * @param depth Depth of the XML tag
-	 * @return A {@link String} instance with the number of tabs representing the given depth
-	 */
-	private static String sumTabsRecursive(final int depth) {
-		if (depth <= 0) {
-			return "";
-		}
-
-		if (depth == 1) {
-			return TAB;
-		}
-
-		return (TAB + sumTabs(depth - 1));
-	}
-}

File src/main/java/com/mulberrybeacon/sniffer/App.java

+package com.mulberrybeacon.sniffer;
+
+/**
+ * Hello world!
+ *
+ */
+public class App 
+{
+    public static void main( String[] args )
+    {
+        System.out.println( "Hello World!" );
+    }
+}

File src/main/java/com/mulberrybeacon/sniffer/ObjectSniffer.java

+package com.mulberrybeacon.sniffer;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+import com.google.common.primitives.Primitives;
+
+/**
+ * Rudimentary logging tool. The goal is to be able to automatically log the values of the fields
+ * declared by the class or interface represented by a given {@link Class} object.
+ *
+ * @author Eduardo Ferreira
+ * @version 0.1.1
+ */
+public final class ObjectSniffer {
+
+    // ~ Static fields/initializers
+    // --------------------------------------------------------------------------------------------
+    /** Canonical name of the Java {@link String} class. */
+    private static final String CLASS_STRING = "java.lang.String";
+
+    /** Error message shown in the logs when a field is inaccessible */
+    private static final String ERROR_INACCESSIBLE_FIELD = "Unable to retrieve value!";
+
+    // ~ Constructors
+    // --------------------------------------------------------------------------------------------
+    /**
+     * Private constructor to prevent instantiation.
+     */
+    private ObjectSniffer() {}
+
+    // ~ Methods (public)
+    // --------------------------------------------------------------------------------------------
+    /**
+     * Analyzes the given {@link Object} instance and logs its fields (names and values).
+     *
+     * @param object {@link Object} instance to log
+     * @return A {@link String} instance with the fields' names and values in XML format
+     */
+    public static String logObject(final Object object) {
+        return logObject(object, 0);
+    }
+
+    // ~ Methods (private)
+    // --------------------------------------------------------------------------------------------
+    /**
+     * Analyzes the given {@link Object} instance and logs its fields (names and values).
+     *
+     * @param object {@link Object} instance to log
+     * @param depth Depth of the fields in the {@link Object} instance
+     * @return A {@link String} instance with the fields' names and values in XML format
+     */
+    private static String logObject(final Object object, final int depth) {
+
+        /* Result of logging the object fields */
+        String xLoggedFields = "\n";
+
+        /* Gets the class represented by the given object and its list of fields */
+        final Class xClass = object.getClass();
+        final Field[] xFields = xClass.getDeclaredFields();
+
+        /* Goes through the list of fields */
+        for (Field xField : xFields) {
+            final String xFieldName = xField.getName();
+            final int xModifier = xField.getModifiers();
+            Object xFieldValue = null;
+
+            try {
+                /* Gets the field value */
+                xFieldValue = isModifierInaccessible(xModifier)
+                            ? runGetterMethod(object, xFieldName)
+                            : xField.get(object);
+            }
+            catch (IllegalAccessException e) {
+                e.printStackTrace();
+            }
+
+            /* Checks if the field class is one of the following:
+             * 1) primitive type;
+             * 2) primitive-wrapper type;
+             * 3) String type.
+             */
+            final Class xFieldClass = xFieldValue.getClass();
+            if (xFieldClass.isPrimitive()
+                || xFieldClass.getName().matches(CLASS_STRING)
+                || Primitives.isWrapperType(xFieldClass)) {
+                xLoggedFields += PrettyPrinter.generateMarkup(xFieldName, xFieldValue.toString(),
+                    depth + 1);
+            }
+            else {
+                /* TODO: Recursively invoke this method to log the subfields of the current field.
+                 * Still no code available, though.
+                 */
+            }
+        }
+
+        /* Enclose the previous set of fields with the object name */
+        return PrettyPrinter.generateMarkup(xClass.getName(), xLoggedFields, 0);
+    }
+
+    /**
+     * Provides a very simple way to look for a getter method in case the modifier of the given
+     * object field is either "private" or "protected".
+     *
+     * @param object {@link Object} instance to log
+     * @param fieldName Field name
+     * @return An {@link Object} instance with either the value of the field or an error message
+     */
+    private static Object runGetterMethod(final Object object, final String fieldName) {
+
+        /* Gets the class represented by the given object and its list of methods */
+        final Class xClass = object.getClass();
+        final Method[] xMethods = xClass.getDeclaredMethods();
+
+        /* Goes through the list of methods */
+        for (Method xMethod : xMethods) {
+            final String xMethodName = xMethod.getName();
+            final int xModifier = xMethod.getModifiers();
+
+            /* Stores the original field name and its length */
+            String xFieldName = fieldName;
+            int xFieldNameLength = fieldName.length();
+
+            /* Checks if the field name has a preffix of the form "\w+_" */
+            if (fieldName.matches("\\w+_\\w+")) {
+                xFieldName = xFieldName.substring(xFieldName.indexOf("_") + 1);
+//              xFieldName = xFieldName.replaceFirst("\\w+_", "");
+                xFieldNameLength = xFieldName.length();
+            }
+
+            /* Creates a class with the set of characters in the field name (both upper- and
+             * lower-case)
+             */
+            xFieldName = xFieldName.toUpperCase() + xFieldName.toLowerCase();
+
+            try {
+                /* Checks if the method name contains the field name */
+                if (xMethodName.matches("[Gg][Ee][Tt][" + xFieldName + "]{" + xFieldNameLength
+                    + "}\\w*")) {
+                    return xMethod.invoke(object);
+                }
+            }
+            catch (Exception e) {
+                /* The exception thrown by the {@code invoke} method could either be an
+                 * {@link IllegalAccessException} or an {@link InvocationTargetException}. For this
+                 * purpose, the for loop should just take its course and exhaust all possibilities
+                 * for a getter method.
+                 */
+                continue;
+            }
+        }
+
+        return ERROR_INACCESSIBLE_FIELD;
+    }
+
+    /**
+     * Checks if the modifier of a {@link Field} or {@link Method} object is "private" or "protected".
+     *
+     * @param modifier Field or methor modifier
+     * @return {@code True} if the modifier is "private" or "protected"; {@code False} otherwise
+     */
+    private static boolean isModifierInaccessible(final int modifier) {
+        return (modifier == Modifier.PRIVATE || modifier == Modifier.PROTECTED);
+    }
+}

File src/main/java/com/mulberrybeacon/sniffer/PrettyPrinter.java

+package com.mulberrybeacon.sniffer;
+
+/**
+ * Pretty printer class that uses a XML logging style.
+ *
+ * @author Eduardo Ferreira
+ * @version 0.1.1
+ */
+public final class PrettyPrinter {
+
+	// ~ Static fields/initializers
+	// --------------------------------------------------------------------------------------------
+	/** Tab marker to represent a single depth level for the field tags (two whitespace characters) */
+	private static final String TAB = "  ";
+
+	/** Dummy marker to replace with the field name */
+	private static final String DUMMY = "X";
+
+	/** XML start tag */
+	private static final String START_TAG = "<" + DUMMY + ">";
+
+	/** XML end tag */
+	private static final String END_TAG = "</" + DUMMY + ">\n";
+
+	// ~ Constructors
+	// --------------------------------------------------------------------------------------------
+	/**
+	 * Private constructor to prevent instantiation.
+	 */
+	private PrettyPrinter() {}
+
+	// ~ Methods (public)
+	// --------------------------------------------------------------------------------------------
+	/**
+	 * Encloses the value of the given field with XML tags containing its name.
+	 * The result will be the following: <FIELD_NAME>FIELD_VALUE</FIELD_NAME>
+	 *
+	 * @param fieldName Name of the field
+	 * @param fieldValue Value of the field
+	 * @param depth Depth of the field
+	 * @return A {@link String} instance with the final XML layout for the field
+	 */
+	public static String generateMarkup(final String fieldName, final String fieldValue,
+		final int depth) {
+
+		final String xMarkup =
+			sumTabs(depth) +
+			START_TAG.replaceFirst(DUMMY, fieldName) +
+			fieldValue +
+			END_TAG.replaceFirst(DUMMY, fieldName);
+
+		return xMarkup;
+	}
+
+	// ~ Methods (private)
+	// --------------------------------------------------------------------------------------------
+	/**
+	 * Outputs the necessary number of tabs to represent the depth of a XML tag.
+	 * (incremental implementation)
+	 *
+	 * @param depth Depth of the XML tag
+	 * @return A {@link String} instance with the number of tabs representing the given depth
+	 */
+	private static String sumTabs(final int depth) {
+		if (depth <= 0) {
+			return "";
+		}
+
+		String xTabs = "";
+		for (int i = 0; i < depth; i++) {
+			xTabs += TAB;
+		}
+
+		return xTabs;
+	}
+
+	/**
+	 * Outputs the necessary number of tabs to represent the depth of a XML tag.
+	 * (recursive implementation)
+	 *
+	 * @param depth Depth of the XML tag
+	 * @return A {@link String} instance with the number of tabs representing the given depth
+	 */
+	private static String sumTabsRecursive(final int depth) {
+		if (depth <= 0) {
+			return "";
+		}
+
+		if (depth == 1) {
+			return TAB;
+		}
+
+		return (TAB + sumTabs(depth - 1));
+	}
+}

File src/test/java/com/mulberrybeacon/sniffer/AppTest.java

+package com.mulberrybeacon.sniffer;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Unit test for simple App.
+ */
+public class AppTest 
+    extends TestCase
+{
+    /**
+     * Create the test case
+     *
+     * @param testName name of the test case
+     */
+    public AppTest( String testName )
+    {
+        super( testName );
+    }
+
+    /**
+     * @return the suite of tests being tested
+     */
+    public static Test suite()
+    {
+        return new TestSuite( AppTest.class );
+    }
+
+    /**
+     * Rigourous Test :-)
+     */
+    public void testApp()
+    {
+        assertTrue( true );
+    }
+}