1
This commit is contained in:
+10
@@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk-27">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="module" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" path="src"/>
|
||||||
|
<classpathentry kind="output" path="bin"/>
|
||||||
|
</classpath>
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
.settings/
|
||||||
|
bin/
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>aadd_comp02</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
||||||
BIN
Binary file not shown.
@@ -0,0 +1,76 @@
|
|||||||
|
package org.palomafp.aadd.rif;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import org.palomafp.aadd.rif.model.Continente;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
System.out.println("=== 1. INTROSPECCIÓN DE LA CLASE CONTINENTE ===");
|
||||||
|
introspeccionarClase(Continente.class);
|
||||||
|
|
||||||
|
System.out.println("\n=== 2. OPERACIONES POR REFLEXIÓN CON LITERAL ===");
|
||||||
|
// Literal con el nombre cualificado de la clase
|
||||||
|
String nombreClaseLiteral = "org.palomafp.aadd.rif.model.Continente";
|
||||||
|
ejecutarReflexionConLiteral(nombreClaseLiteral);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bloque 1: Examina e imprime los componentes de la clase
|
||||||
|
*/
|
||||||
|
private static void introspeccionarClase(Class<?> clase) {
|
||||||
|
System.out.println("Atributos declarados:");
|
||||||
|
Field[] atributos = clase.getDeclaredFields();
|
||||||
|
for (Field atributo : atributos) {
|
||||||
|
// Imprime el tipo de dato y el nombre del atributo
|
||||||
|
System.out.println(" - " + atributo.getType().getSimpleName() + " " + atributo.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\nMétodos declarados:");
|
||||||
|
Method[] metodos = clase.getDeclaredMethods();
|
||||||
|
for (Method metodo : metodos) {
|
||||||
|
// Imprime el tipo de retorno y el nombre del método
|
||||||
|
System.out.println(" - " + metodo.getReturnType().getSimpleName() + " " + metodo.getName() + "()");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bloque 2: Carga, instancia, modifica e imprime la clase mediante su String
|
||||||
|
*/
|
||||||
|
private static void ejecutarReflexionConLiteral(String rutaClase) {
|
||||||
|
try {
|
||||||
|
// o Obtener la clase a partir del literal String
|
||||||
|
Class<?> claseCargada = Class.forName(rutaClase);
|
||||||
|
|
||||||
|
// o Construir el objeto sin usar "new" (usando el constructor por defecto)
|
||||||
|
Constructor<?> constructorVacio = claseCargada.getDeclaredConstructor();
|
||||||
|
Object objetoContinente = constructorVacio.newInstance();
|
||||||
|
System.out.println("[OK] Objeto instanciado dinámicamente en memoria.");
|
||||||
|
|
||||||
|
// o Cargar el objeto asignando un valor al código ("EU" por ejemplo)
|
||||||
|
// Buscamos el atributo 'codigo' dentro de la clase
|
||||||
|
Field atributoCodigo = claseCargada.getDeclaredField("codigo");
|
||||||
|
|
||||||
|
// Forzamos el acceso ya que el atributo de persistencia es private
|
||||||
|
atributoCodigo.setAccessible(true);
|
||||||
|
|
||||||
|
// Asignamos el valor al objeto concreto
|
||||||
|
atributoCodigo.set(objetoContinente, "EU");
|
||||||
|
System.out.println("[OK] Atributo 'codigo' asignado mediante Reflexión.");
|
||||||
|
|
||||||
|
// o Imprimir el objeto previamente cargado
|
||||||
|
System.out.println("\nResultado del objeto impreso:");
|
||||||
|
System.out.println(objetoContinente.toString());
|
||||||
|
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
System.err.println("Error: No se encontró la clase especificada en el literal -> " + rutaClase);
|
||||||
|
} catch (NoSuchFieldException e) {
|
||||||
|
System.err.println("Error: El atributo especificado no existe en la clase.");
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Ocurrió un error durante la ejecución: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package org.palomafp.aadd.rif.model;
|
||||||
|
|
||||||
|
public class Continente {
|
||||||
|
// Atributos privados
|
||||||
|
private String codigo;
|
||||||
|
private String nombre;
|
||||||
|
|
||||||
|
// Constructor vacío (necesario para instanciación dinámica)
|
||||||
|
public Continente() {
|
||||||
|
}
|
||||||
|
|
||||||
|
// Constructor con parámetros
|
||||||
|
public Continente(String codigo, String nombre) {
|
||||||
|
this.codigo = codigo;
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getters y Setters
|
||||||
|
public String getCodigo() {
|
||||||
|
return codigo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCodigo(String codigo) {
|
||||||
|
this.codigo = codigo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNombre() {
|
||||||
|
return nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNombre(String nombre) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Método toString para imprimir el objeto
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Continente [codigo=" + codigo + ", nombre=" + nombre + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user