Skip to content

Commit

Permalink
Add superclass and interfaces to classinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
espenhw committed Mar 4, 2009
1 parent 27a2196 commit 802f2e6
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/main/groovy/org/grumblesmurf/malabar/Classpath.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ class Classpath
Class c = this.classLoader.loadClass(className)
print "(class "
printName(c)
printSuperclass(c)
printInterfaces(c)
printModifiers(c)
printDeclaringClass(c)
printTypeParameters(c)
Expand Down Expand Up @@ -223,7 +225,20 @@ class Classpath
print ") "
}
}


def printSuperclass(it) {
if (it.genericSuperclass) {
print " :super-class " + quotify(typeString(it.genericSuperclass, true))
}
}

def printInterfaces(it) {
if (it.genericInterfaces) {
print " :interfaces "
Utils.printAsLispList(it.genericInterfaces.collect { intf -> typeString(intf, true) })
}
}

def getMembersInternal(Class c, Set seenMethods) {
ClassReader cr = getClassReader(c.name);
ClassInfo cir = new ClassInfo();
Expand Down Expand Up @@ -281,6 +296,8 @@ class Classpath

def classPrinter = {
print "(class :name " + quotify(it.simpleName)
printSuperclass(it)
printInterfaces(it)
printModifiers(it)
printDeclaringClass(it)
printTypeParameters(it)
Expand Down Expand Up @@ -318,23 +335,28 @@ class Classpath
}

def typeString(type) {
return typeString(type, false);
}

def typeString(type, qualify) {
if (type instanceof Class) {
def name = qualify ? type.name : type.simpleName
if (type.enclosingClass) {
return typeString(type.enclosingClass) + "." + type.simpleName
return typeString(type.enclosingClass, qualify) + "." + name
}
return type.simpleName
return name
}
if (type instanceof GenericArrayType) {
return typeString(type.genericComponentType) + "[]"
return typeString(type.genericComponentType, qualify) + "[]"
}
if (type instanceof ParameterizedType) {
def str = typeString(type.rawType)
def str = typeString(type.rawType, qualify)
str += "<";
type.actualTypeArguments.eachWithIndex{ it, i ->
if (i > 0) {
str += ", "
}
str += typeString(it)
str += typeString(it, qualify)
}
str += ">"
return str;
Expand All @@ -345,7 +367,7 @@ class Classpath
if (type.bounds.length > 1 ||
type.bounds[0] != Object.class) {
str += " extends "
str += type.bounds.collect{ typeString(it) }.join(" & ")
str += type.bounds.collect{ typeString(it, qualify) }.join(" & ")
}
}
return str
Expand Down
104 changes: 104 additions & 0 deletions src/test/groovy/org/grumblesmurf/malabar/ClasspathTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* Copyright (c) 2009 Espen Wiborg <espenhw@grumblesmurf.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
package org.grumblesmurf.malabar;

import org.junit.Test;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.AfterClass;

import static org.junit.Assert.*;
import static org.junit.matchers.JUnitMatchers.*;
import static org.hamcrest.CoreMatchers.*;

import org.codehaus.groovy.tools.shell.IO;

class ClasspathTest
{
def out;

@BeforeClass
static void redirectOutput() {
ExpandoMetaClass.enableGlobally();

Object.metaClass.println = Utils.&println;
Object.metaClass.print = Utils.&print;
}

@Before
void setIO() {
out = new ByteArrayOutputStream();
GroovyServer.io.set(new IO(System.in, out, out));
}


@AfterClass
static void setExceptionHandler() {
Thread[] threads = new Thread[Thread.activeCount() * 2];
Thread.enumerate(threads);
threads.each {
if (it) {
it.setUncaughtExceptionHandler(GroovyServerTest.exceptionHandler);
}
}
}

@Test
void objectHasNoSuper() {
String result = classInfo("java.lang.Object");
assertThat(result, containsString('(class :name "java.lang.Object"'));
assertThat(result, not(containsString(":super-class")));
}

@Test
void superOfIntegerIsNumber() {
String result = classInfo("java.lang.Integer");
assertThat(result, containsString('(class :name "java.lang.Integer"'));
assertThat(result, containsString(':super-class "java.lang.Number"'));
}

@Test
void inputStreamImplementsCloseable() {
String result = classInfo("java.io.InputStream");
assertThat(result, containsString(':interfaces ("java.io.Closeable" )'));
}

@Test
void outputStreamImplementsCloseableAndFlushable() {
String result = classInfo("java.io.OutputStream");
assertThat(result, containsString(':interfaces ("java.io.Closeable" "java.io.Flushable" )'));
}

@Test
void collectionImplementsIterableE() {
String result = classInfo("java.util.Collection");
assertThat(result, containsString(':interfaces ("java.lang.Iterable<E>" )'));
}

@Test
void scannerImplementsIteratorString() {
String result = classInfo("java.util.Scanner");
assertThat(result, containsString(':interfaces ("java.util.Iterator<java.lang.String>" )'));
}

def classInfo(name) {
new Classpath().getClassInfo(name);
return out.toString();
}
}

0 comments on commit 802f2e6

Please sign in to comment.