commit d4488c550ba1e495de503a9e0ffe9164b992dad8 Author: alumnoT Date: Tue Oct 13 17:18:26 2026 +0200 1 diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..4e29ea7 --- /dev/null +++ b/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..98a8c43 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.settings/ +bin/ \ No newline at end of file diff --git a/.project b/.project new file mode 100644 index 0000000..dd4aad2 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + aadd_comp02 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/6.2_RIF.pdf b/6.2_RIF.pdf new file mode 100644 index 0000000..44de310 Binary files /dev/null and b/6.2_RIF.pdf differ diff --git a/src/org/palomafp/aadd/rif/Main.java b/src/org/palomafp/aadd/rif/Main.java new file mode 100644 index 0000000..aa07ea1 --- /dev/null +++ b/src/org/palomafp/aadd/rif/Main.java @@ -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(); + } + } +} \ No newline at end of file diff --git a/src/org/palomafp/aadd/rif/model/Continente.java b/src/org/palomafp/aadd/rif/model/Continente.java new file mode 100644 index 0000000..4676292 --- /dev/null +++ b/src/org/palomafp/aadd/rif/model/Continente.java @@ -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 + "]"; + } +}