first commit
This commit is contained in:
+16
@@ -0,0 +1,16 @@
|
|||||||
|
# Eclipse
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
|
.settings/
|
||||||
|
|
||||||
|
# Compilación Java
|
||||||
|
bin/
|
||||||
|
target/
|
||||||
|
*.class
|
||||||
|
|
||||||
|
# Logs
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# Sistema operativo
|
||||||
|
.DS_Store
|
||||||
|
Thumbs.db
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package org.palomafp.aadd.rmr.main;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Modifier;
|
||||||
|
|
||||||
|
public class Principal {
|
||||||
|
|
||||||
|
private static final String CLASE = "org.palomafp.aadd.rmr.model.Continente";
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
Class<?> clase = Class.forName(CLASE);
|
||||||
|
System.out.println("Introspección de " + clase.getName());
|
||||||
|
|
||||||
|
System.out.println("Atributos:");
|
||||||
|
for (Field f : clase.getDeclaredFields()) {
|
||||||
|
System.out.println(Modifier.toString(f.getModifiers()) + " " + f.getType().getSimpleName() + " " + f.getName());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Constructores:");
|
||||||
|
for (Constructor<?> c : clase.getDeclaredConstructors()) {
|
||||||
|
System.out.println(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Métodos:");
|
||||||
|
for (Method m : clase.getDeclaredMethods()) {
|
||||||
|
System.out.println(m);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ahora la reflexión
|
||||||
|
|
||||||
|
// creo el objeto sin usar new Continente()
|
||||||
|
Constructor<?> constructor = clase.getDeclaredConstructor();
|
||||||
|
Object continente = constructor.newInstance();
|
||||||
|
System.out.println("Objeto creado por reflexión:");
|
||||||
|
System.out.println(continente);
|
||||||
|
|
||||||
|
// asigno un valor al code
|
||||||
|
Field campoCodigo = clase.getDeclaredField("codigo");
|
||||||
|
campoCodigo.setAccessible(true);
|
||||||
|
campoCodigo.set(continente, 1);
|
||||||
|
|
||||||
|
// imprimo el objeto cargado
|
||||||
|
System.out.println("Objeto después de asignar el código:");
|
||||||
|
System.out.println(continente);
|
||||||
|
|
||||||
|
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
System.out.println("Error: no se encuentra la clase");
|
||||||
|
} catch (ReflectiveOperationException e) {
|
||||||
|
System.out.println("Error de reflexión: " + e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package org.palomafp.aadd.rmr.model;
|
||||||
|
|
||||||
|
public class Continente {
|
||||||
|
|
||||||
|
private int codigo;
|
||||||
|
private String nombre;
|
||||||
|
|
||||||
|
public Continente(int codigo, String nombre) {
|
||||||
|
this.codigo = codigo;
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Continente() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCodigo() {
|
||||||
|
return codigo;
|
||||||
|
}
|
||||||
|
public void setCodigo(int codigo) {
|
||||||
|
this.codigo = codigo;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String getNombre() {
|
||||||
|
return nombre;
|
||||||
|
}
|
||||||
|
public void setNombre(String nombre) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Continente [codigo=" + codigo + ", nombre=" + nombre + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user