Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Adam Gerber
/
labJava
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
b07a7103
authored
Oct 25, 2015
by
Adam Gerber
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
include Reflector in lab05c
parent
636c71af
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
136 additions
and
0 deletions
src/lec05/glab/reflection/Reflector.java
src/lec05/glab/reflection/Reflector.java
View file @
b07a7103
...
...
@@ -13,7 +13,142 @@ import java.util.Stack;
public
class
Reflector
{
public
static
void
main
(
String
[]
args
)
{
// read class name from command line args or user input
// printClass(java.util.Date.class);
// return;
String
strClass
=
""
;
Scanner
scan
=
new
Scanner
(
System
.
in
);
Class
cls
=
null
;
while
(
true
)
{
try
{
System
.
out
.
println
(
"Enter \"exit\" to quit, or a fully qualified class name (e.g. java.util.Date): "
);
strClass
=
scan
.
next
();
if
(
strClass
.
equalsIgnoreCase
(
"exit"
)){
System
.
exit
(
0
);
}
printClass
(
strClass
);
}
catch
(
ClassNotFoundException
e
)
{
// e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
System
.
out
.
println
(
"That's now a valid class name, try again."
);
continue
;
}
}
}
public
static
void
printClass
(
Class
cls
)
{
//create a stack and push the original class onto it
Stack
<
Class
>
clsStacks
=
new
Stack
<>();
clsStacks
.
push
(
cls
);
//keep pushing its superclass until superclass is null (I've reached grand-daddy Object)
while
(
clsStacks
.
peek
().
getSuperclass
()
!=
null
)
{
clsStacks
.
push
(
clsStacks
.
peek
().
getSuperclass
());
}
while
(!
clsStacks
.
empty
())
{
cls
=
clsStacks
.
pop
();
System
.
out
.
println
(
"###############CLASS###############\n"
+
cls
.
toString
()
+
"\n###############CLASS###############\n"
);
System
.
out
.
println
(
"//INTERFACES"
);
printInterfaces
(
cls
);
System
.
out
.
println
(
"//FIELDS"
);
printFields
(
cls
);
System
.
out
.
println
();
System
.
out
.
println
(
"//CONSTRUCTORS"
);
printConstructors
(
cls
);
System
.
out
.
println
();
System
.
out
.
println
(
"//METHODS"
);
printMethods
(
cls
);
System
.
out
.
println
();
}
}
//overlaoded to take string
public
static
void
printClass
(
String
strClass
)
throws
ClassNotFoundException
{
Class
cls
;
//the following line could throw an exception
cls
=
Class
.
forName
(
strClass
);
printClass
(
cls
);
}
public
static
void
printConstructors
(
Class
cl
)
{
Constructor
[]
constructors
=
cl
.
getDeclaredConstructors
();
for
(
Constructor
c
:
constructors
)
{
String
name
=
c
.
getName
();
System
.
out
.
print
(
" "
+
Modifier
.
toString
(
c
.
getModifiers
()));
System
.
out
.
print
(
" "
+
name
+
"("
);
// print parameter types
Class
[]
paramTypes
=
c
.
getParameterTypes
();
for
(
int
j
=
0
;
j
<
paramTypes
.
length
;
j
++)
{
if
(
j
>
0
)
System
.
out
.
print
(
", "
);
System
.
out
.
print
(
paramTypes
[
j
].
getName
());
}
System
.
out
.
println
(
"); //no-arg constructor"
);
}
}
public
static
void
printInterfaces
(
Class
cl
)
{
System
.
out
.
print
(
" "
);
Class
[]
interfaces
=
cl
.
getInterfaces
();
for
(
int
nC
=
0
;
nC
<
interfaces
.
length
;
nC
++)
{
if
(
nC
>
0
)
System
.
out
.
print
(
", "
);
System
.
out
.
print
(
interfaces
[
nC
].
getName
());
}
System
.
out
.
println
();
System
.
out
.
println
();
}
public
static
void
printMethods
(
Class
cl
)
{
Method
[]
methods
=
cl
.
getDeclaredMethods
();
for
(
Method
m
:
methods
)
{
Class
retType
=
m
.
getReturnType
();
String
name
=
m
.
getName
();
// print modifiers, return type and method name
System
.
out
.
print
(
" "
+
Modifier
.
toString
(
m
.
getModifiers
()));
System
.
out
.
print
(
" "
+
retType
.
getName
()
+
" "
+
name
+
"("
);
// print parameter types
Class
[]
paramTypes
=
m
.
getParameterTypes
();
for
(
int
j
=
0
;
j
<
paramTypes
.
length
;
j
++)
{
if
(
j
>
0
)
System
.
out
.
print
(
", "
);
System
.
out
.
print
(
paramTypes
[
j
].
getName
());
}
System
.
out
.
println
(
");"
);
}
}
public
static
void
printFields
(
Class
cl
)
{
Field
[]
fields
=
cl
.
getDeclaredFields
();
for
(
Field
f
:
fields
)
{
String
strMods
=
Modifier
.
toString
(
f
.
getModifiers
());
Class
type
=
f
.
getType
();
String
name
=
f
.
getName
();
System
.
out
.
print
(
" "
+
Modifier
.
toString
(
f
.
getModifiers
()));
System
.
out
.
println
(
" "
+
type
.
getName
()
+
" "
+
name
+
";"
);
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment