Namenskonvention und Reflection
Methoden werden über Namen oder implizite Konventionen erkannt.
Vollständiger Legacy-Modern-Vergleich aller Java-Klassen, Tests, Maven-POMs und relevanten Ressourcen dieses Moduls. Jede Datei ist genau einmal einer fachlich passenden Vergleichsgruppe zugeordnet.
Methoden werden über Namen oder implizite Konventionen erkannt.
Eine Runtime-Annotation markiert die erlaubten Methoden explizit.
Die Ähnlichkeit ist eine technische Orientierung auf normalisiertem Quelltext. Sie ersetzt keine fachliche Bewertung.
| Vergleichsgruppe | Legacy-Dateien | Modern-Dateien | Legacy-Zeilen | Modern-Zeilen | Ähnlichkeit |
|---|---|---|---|---|---|
| Build-Konfiguration | 1 | 1 | 34 | 34 | 100% |
| Registry, Annotation und Test | 2 | 3 | 32 | 58 | 41% |
| Kommandos | 1 | 1 | 11 | 13 | 80% |
Das Pair-POM bindet die Legacy- und Modern-Submodule zusammen und gehört deshalb ebenfalls zur vollständigen Codeabdeckung.
Reflection-/Testkonfiguration wird verglichen.
| Legacy-Dateien | Modern-Dateien | Einordnung |
|---|---|---|
| pom.xml | pom.xml | Direktvergleich Semantische Ähnlichkeit: 100% |
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>at.aydin.academy</groupId>
<artifactId>14annotationsreflection-pair</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>annotations-reflection-legacy</artifactId>
<packaging>jar</packaging>
<name>Annotations Reflection Legacy</name>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project><?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>at.aydin.academy</groupId>
<artifactId>14annotationsreflection-pair</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>annotations-reflection-modern</artifactId>
<packaging>jar</packaging>
<name>Annotations Reflection Modern</name>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>Namenskonvention wird durch explizite Annotation und geprüfte Registry ersetzt.
| Legacy-Dateien | Modern-Dateien | Einordnung |
|---|---|---|
| LegacyCommandRegistry.java LegacyCommandRegistryTest.java | Command.java CommandRegistry.java CommandRegistryTest.java | Direktvergleich Semantische Ähnlichkeit: 41% |
package at.aydin.lab.annotations.legacy;
import java.lang.reflect.*;
// Design Pattern: Registry
// Zweck: Historische Registrierung über String-Konventionen und Reflection.
public final class LegacyCommandRegistry {
private final Object target;
public LegacyCommandRegistry(Object target) {
this.target = target;
}
public String execute(String command, String argument) {
try {
Method m = target.getClass().getMethod("command_" + command, String.class);
return (String) m.invoke(target, argument);
} catch (ReflectiveOperationException ex) {
throw new IllegalArgumentException("Unbekanntes Kommando: " + command, ex);
}
}
}package at.aydin.lab.annotations.legacy;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class LegacyCommandRegistryTest {
@Test
void invokesByNamingConvention() {
assertEquals("Hallo Test", new LegacyCommandRegistry(new LegacyCommands()).execute("hello", "Test"));
}
}package at.aydin.lab.annotations.modern;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public
@interface
Command {
String value();
}package at.aydin.lab.annotations.modern;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
// Design Pattern: Command + Metadata Registry
// Zweck: Annotationen machen Namen explizit und entkoppeln sie von Methodenkonventionen.
public final class CommandRegistry {
private record Handler(Object target, Method method) {}
private final Map<String, Handler> handlers = new HashMap<>();
public CommandRegistry(Object... targets) {
for (Object target : targets) {
for (Method method : target.getClass().getDeclaredMethods()) {
Command command = method.getAnnotation(Command.class);
if (command != null) {
handlers.put(command.value(), new Handler(target, method));
}
}
}
}
public String execute(String command, String argument) {
Handler handler = Optional.ofNullable(handlers.get(command))
.orElseThrow(() -> new IllegalArgumentException(
"Unbekanntes Kommando: " + command));
try {
return (String) handler.method().invoke(handler.target(), argument);
} catch (ReflectiveOperationException exception) {
throw new IllegalStateException(exception);
}
}
}package at.aydin.lab.annotations.modern;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class CommandRegistryTest {
@Test
void usesExplicitAnnotation() {
assertEquals("Hallo Test", new CommandRegistry(new ModernCommands()).execute("hello", "Test"));
}
}Aufrufbare Methoden werden direkt verglichen.
| Legacy-Dateien | Modern-Dateien | Einordnung |
|---|---|---|
| LegacyCommands.java | ModernCommands.java | Direktvergleich Semantische Ähnlichkeit: 80% |
package at.aydin.lab.annotations.legacy;
public final class LegacyCommands {
public String command_hello(String name) {
return "Hallo " + name;
}
public String command_bye(String name) {
return "Tschüss " + name;
}
}package at.aydin.lab.annotations.modern;
public final class ModernCommands {
@Command("hello")
public String greet(String name) {
return "Hallo " + name;
}
@Command("bye")
public String farewell(String name) {
return "Tschüss " + name;
}
}