Created
March 24, 2012 14:03
-
-
Save thekid/2183228 to your computer and use it in GitHub Desktop.
XP Tools: Command line reflection + "-v" and "-vv"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
xp -r lang.types.Long
xp -r lang.types.Long -v
xp -r lang.types.Long -vv