Skip to content

Instantly share code, notes, and snippets.

@thekid
Created March 24, 2012 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thekid/2183228 to your computer and use it in GitHub Desktop.
Save thekid/2183228 to your computer and use it in GitHub Desktop.
XP Tools: Command line reflection + "-v" and "-vv"
diff --git a/tools/src/main/php/xp/runtime/Reflect.class.php b/tools/src/main/php/xp/runtime/Reflect.class.php
index 58b2bf4..8941f2f 100644
--- a/tools/src/main/php/xp/runtime/Reflect.class.php
+++ b/tools/src/main/php/xp/runtime/Reflect.class.php
@@ -33,11 +33,13 @@
* Prints methods - static first, rest then
*
* @param lang.reflect.Method[] methods
+ * @param bool methodComments
*/
- protected static function printMethods(array $methods) {
+ protected static function printMethods(array $methods, $methodComments) {
$i= 0;
foreach ($methods as $method) {
if (!Modifiers::isStatic($method->getModifiers())) continue;
+ $methodComments && Console::writeLine("\n", self::textOf($method->getComment(), ' # '));
Console::writeLine(' ', $method);
$i++;
}
@@ -46,6 +48,7 @@
$i= 0;
foreach ($methods as $method) {
if (Modifiers::isStatic($method->getModifiers())) continue;
+ $methodComments && Console::writeLine("\n", self::textOf($method->getComment(), ' # '));
Console::writeLine(' ', $method);
$i++;
}
@@ -55,8 +58,9 @@
* Handles enums
*
* @param lang.XPClass class
+ * @param bool methodComments
*/
- protected static function printEnum(XPClass $enum) {
+ protected static function printEnum(XPClass $enum, $methodComments) {
Console::write(implode(' ', Modifiers::namesOf($enum->getModifiers())));
Console::write(' enum ', self::displayNameOf($enum));
@@ -95,7 +99,7 @@
// Methods
$i && Console::writeLine();
- self::printMethods($enum->getMethods());
+ self::printMethods($enum->getMethods(), $methodComments);
Console::writeLine('}');
}
@@ -104,8 +108,9 @@
* Handles interfaces
*
* @param lang.XPClass class
+ * @param bool methodComments
*/
- protected static function printInterface(XPClass $iface) {
+ protected static function printInterface(XPClass $iface, $methodComments) {
Console::write(implode(' ', Modifiers::namesOf($iface->getModifiers() ^ MODIFIER_ABSTRACT)));
Console::write(' interface ', self::displayNameOf($iface));
@@ -122,12 +127,14 @@
$i= 0;
if ($iface->hasConstructor()) {
+ $methodComments && Console::writeLine("\n", self::textOf($iface->getConstructor()->getComment(), ' # '));
Console::writeLine(' ', $iface->getConstructor());
$i++;
}
// Methods
foreach ($iface->getMethods() as $method) {
+ $methodComments && Console::writeLine("\n", self::textOf($method->getComment(), ' # '));
Console::write(' ', $method->getReturnTypeName(), ' ', $method->getName(), '(');
if ($params= $method->getParameters()) {
$s= sizeof($params)- 1;
@@ -147,8 +154,9 @@
* Handles classes
*
* @param lang.XPClass class
+ * @param bool methodComments
*/
- protected static function printClass(XPClass $class) {
+ protected static function printClass(XPClass $class, $methodComments) {
Console::write(implode(' ', Modifiers::namesOf($class->getModifiers())));
Console::write(' class ', self::displayNameOf($class));
@@ -176,13 +184,14 @@
$i && Console::writeLine();
$i= 0;
if ($class->hasConstructor()) {
+ $methodComments && Console::writeLine("\n", self::textOf($class->getConstructor()->getComment(), ' # '));
Console::writeLine(' ', $class->getConstructor());
$i++;
}
// Methods
$i && Console::writeLine();
- self::printMethods($class->getMethods());
+ self::printMethods($class->getMethods(), $methodComments);
Console::writeLine('}');
}
@@ -233,6 +242,22 @@
}
/**
+ * Converts api-doc "markup" to plain text w/ ASCII "art"
+ *
+ * @param string markup
+ * @param string prefix
+ * @return string text
+ */
+ protected static function textOf($markup, $prefix) {
+ $line= str_repeat('=', 72);
+ return $prefix.strip_tags(preg_replace(array(
+ '#<pre>#', '#</pre>#', '#<li>#', "#\\n#",
+ ), array(
+ $line, $line, '* ', "\n".$prefix
+ ), trim($markup)));
+ }
+
+ /**
* Main
*
* @param string[] args
@@ -243,31 +268,39 @@
return 2;
}
+ // Details
+ if (sizeof($args) > 1) {
+ $methods= '-vv' == $args[1];
+ $comments= '-v' == $args[1] || $methods;
+ }
+
// Check whether a class or a package is given
$cl= ClassLoader::getDefault();
if ($cl->providesClass($args[0])) {
$class= XPClass::forName($args[0], $cl);
Console::writeLine('@', $class->getClassLoader());
+ $comments && Console::writeLine(self::textOf($class->getComment(), '# '));
if ($class->isInterface()) {
- self::printInterface($class);
+ self::printInterface($class, $methods);
} else if ($class->isEnum()) {
- self::printEnum($class);
+ self::printEnum($class, $methods);
} else {
- self::printClass($class);
+ self::printClass($class, $methods);
}
return 0;
}
// Not a class, check for packages
- $provided= FALSE;
+ $package= NULL;
foreach ($cl->getLoaders() as $loader) {
if (!$loader->providesPackage($args[0])) continue;
+ $package= Package::forName($args[0]);
+ $comments && Console::writeLine(self::textOf($package->getComment(), '# '));
Console::writeLine('@', $loader);
- $provided= TRUE;
}
- if ($provided) {
- self::printPackage(Package::forName($args[0]));
+ if ($package) {
+ self::printPackage($package);
return 0;
}
@thekid
Copy link
Author

thekid commented Mar 24, 2012

xp -r lang.types.Long

@lang.FileSystemClassLoader<C:\cygwin\home\friebe\devel\xp.thekid.core\core\src\main\php\>
public class lang.types.Long extends lang.types.Number {
  public lang.types.Number::$value

  public lang.types.Long __construct(var $value) throws lang.IllegalArgumentException

  public int intValue()
  public double floatValue()
  public double doubleValue()
  public string hashCode()
  public string toString()
  public bool equals(var $cmp)
  public string getClassName()
  public lang.XPClass getClass()
}

xp -r lang.types.Long -v

@lang.FileSystemClassLoader<C:\cygwin\home\friebe\devel\xp.thekid.core\core\src\main\php\>
# The Long class wraps a value of the type long
#
# Range: -2^63 - (2^63)- 1
public class lang.types.Long extends lang.types.Number {
  public lang.types.Number::$value

  public lang.types.Long __construct(var $value) throws lang.IllegalArgumentException

  public int intValue()
  public double floatValue()
  public double doubleValue()
  public string hashCode()
  public string toString()
  public bool equals(var $cmp)
  public string getClassName()
  public lang.XPClass getClass()
}

xp -r lang.types.Long -vv

@lang.FileSystemClassLoader<C:\cygwin\home\friebe\devel\xp.thekid.core\core\src\main\php\>
# The Long class wraps a value of the type long
#
# Range: -2^63 - (2^63)- 1
public class lang.types.Long extends lang.types.Number {
  public lang.types.Number::$value


  # Constructor
  public lang.types.Long __construct(var $value) throws lang.IllegalArgumentException


  # Returns the value of this number as an int.
  public int intValue()

  # Returns the value of this number as a double.
  public double floatValue()

  # Returns the value of this number as a float.
  public double doubleValue()

  # Returns a hashcode for this number
  public string hashCode()

  # Returns a string representation of this number object
  public string toString()

  # Indicates whether some other object is "equal to" this one.
  public bool equals(var $cmp)

  # Returns the fully qualified class name for this class
  # (e.g. "io.File")
  public string getClassName()

  # Returns the runtime class of an object.
  public lang.XPClass getClass()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment