Reflection
Jim once believed that the following Ruby code constitutes an example of reflection.
def create(klass, value)
klass.new(value)
end
g = create(Greeting, "Hello")
g.show
Neither introspection nor metaprogramming occur in this code. This is not reflection. However, Jim argues that since getConstructor exists in Sun’s Java reflect class and because the Ruby code accomplishes the same task as his Java example that this is reflection. This logic is tenuous. However, I can defeat it because his Java code does not exemplify reflection.
public static Object create(Class c, String value)
throws Exception
{
Constructor ctor = c.getConstructor(
new Class[] { String.class } );
return ctor.newInstance(
new Object[] { "Hello" } );
}
public static void main (String args[])
throws Exception
{
Greeting g =
(Greeting) create(Greeting.class, "Hello");
g.show();
}
Using Java’s reflect class does not preclude that reflection has occured. If these examples are reflection, I could argue that the following C code is an example of reflection.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void* create(void* klass, char* value);
void* create(void* klass, char* value) {
size_t value_length = strlen(value);
klass = malloc(value_length+1);
strncpy(klass,value, value_length);
return klass;
}
int main() {
char* Greeting;
Greeting = create(Greeting, "Hello");
printf("%s", Greeting);
free(Greeting);
return 0;
}
Oh dear. Why did I switch to Ruby anyway if I can reflect in C?






