From a045da701d56c88d3213043dbfeda5b676694c42 Mon Sep 17 00:00:00 2001 From: HossamSaberr Date: Tue, 28 Oct 2025 07:51:51 +0300 Subject: [PATCH 1/6] feat(lombok): Use Lombok Annotations to resolve #52 --- pom.xml | 11 ++++- src/main/java/com/mycmd/ShellContext.java | 50 +++-------------------- 2 files changed, 15 insertions(+), 46 deletions(-) diff --git a/pom.xml b/pom.xml index 726bee2..f64f67f 100644 --- a/pom.xml +++ b/pom.xml @@ -17,9 +17,16 @@ 17 + + + org.projectlombok + lombok + 1.18.32 + provided + + - org.apache.maven.plugins maven-jar-plugin @@ -34,4 +41,4 @@ - + \ No newline at end of file diff --git a/src/main/java/com/mycmd/ShellContext.java b/src/main/java/com/mycmd/ShellContext.java index 64397d3..8068e70 100644 --- a/src/main/java/com/mycmd/ShellContext.java +++ b/src/main/java/com/mycmd/ShellContext.java @@ -1,9 +1,13 @@ package com.mycmd; +import lombok.Getter; +import lombok.Setter; import java.io.*; import java.util.*; import java.time.Instant; +@Getter +@Setter public class ShellContext { private File currentDir; private List history; @@ -13,6 +17,8 @@ public class ShellContext { private final List commandHistory; private final Instant startTime; + private final Map envVars = new HashMap<>(); + public ShellContext() { this.currentDir = new File(System.getProperty("user.dir")); this.history = new ArrayList<>(); @@ -22,14 +28,6 @@ public ShellContext() { loadAliases(); } - public File getCurrentDir() { - return currentDir; - } - - public void setCurrentDir(File dir) { - this.currentDir = dir; - } - public void addToHistory(String command) { history.add(command); commandHistory.add(command); // Add to command history @@ -38,17 +36,6 @@ public void addToHistory(String command) { } } - public List getHistory() { - return new ArrayList<>(history); - } - - public List getCommandHistory() { - return commandHistory; - } - - public Instant getStartTime() { - return startTime; - } public void clearHistory() { history.clear(); @@ -65,14 +52,6 @@ public void removeAlias(String name) { saveAliases(); } - public String getAlias(String name) { - return aliases.get(name); - } - - public Map getAliases() { - return new HashMap<>(aliases); - } - public boolean hasAlias(String name) { return aliases.containsKey(name); } @@ -131,21 +110,4 @@ public File resolvePath(String path) { } } - // environmental variable map - // author: Kaveesha Fernando - // date: 2024-06-10 - private final Map envVars = new HashMap<>(); - - public void setEnvVar(String key, String value) { - envVars.put(key, value); - } - - public String getEnvVar(String key) { - return envVars.get(key); - } - - public Map getEnvVars() { - return new HashMap<>(envVars); - } - } From 5376f2efb8ea919f4b3e446d6b5cfb47dc27baf0 Mon Sep 17 00:00:00 2001 From: HossamSaberr Date: Tue, 28 Oct 2025 19:47:11 +0300 Subject: [PATCH 2/6] feat: Add Lombok --- pom.xml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pom.xml b/pom.xml index f64f67f..977df1a 100644 --- a/pom.xml +++ b/pom.xml @@ -27,6 +27,21 @@ + + org.apache.maven.plugins + maven-compiler-plugin + 3.11.0 + ${maven.compiler.source} + ${maven.compiler.target} + + + org.projectlombok + lombok + 1.18.32 + + + + org.apache.maven.plugins maven-jar-plugin From 5b893e7cbf68b624b37473bcf723cbf452142437 Mon Sep 17 00:00:00 2001 From: HossamSaberr Date: Tue, 28 Oct 2025 20:04:23 +0300 Subject: [PATCH 3/6] fix(lombok): Refine Lombok annotations --- pom.xml | 6 ++- src/main/java/com/mycmd/ShellContext.java | 45 +++++++++++++++++++++-- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index 977df1a..46e6efc 100644 --- a/pom.xml +++ b/pom.xml @@ -30,14 +30,16 @@ org.apache.maven.plugins maven-compiler-plugin - 3.11.0 + 3.11.0 + ${maven.compiler.source} ${maven.compiler.target} org.projectlombok lombok - 1.18.32 + 1.18.32 + diff --git a/src/main/java/com/mycmd/ShellContext.java b/src/main/java/com/mycmd/ShellContext.java index 8068e70..69246ed 100644 --- a/src/main/java/com/mycmd/ShellContext.java +++ b/src/main/java/com/mycmd/ShellContext.java @@ -2,14 +2,18 @@ import lombok.Getter; import lombok.Setter; +import lombok.NonNull; import java.io.*; import java.util.*; import java.time.Instant; @Getter -@Setter public class ShellContext { + + @Setter + @NonNull private File currentDir; + private List history; private Map aliases; private static final String ALIAS_FILE = ".mycmd_aliases"; @@ -30,12 +34,30 @@ public ShellContext() { public void addToHistory(String command) { history.add(command); - commandHistory.add(command); // Add to command history + commandHistory.add(command); if (history.size() > MAX_HISTORY) { history.remove(0); } } + /** * OVERRIDES Lombok's generated getter to return a DEFENSIVE COPY + * to prevent external code from modifying the shell directly. + */ + @Override + public List getHistory() { + return new ArrayList<>(history); + } + + @Override + public Map getAliases() { + return new HashMap<>(aliases); + } + + @Override + public Map getEnvVars() { + return new HashMap<>(envVars); + } + public void clearHistory() { history.clear(); @@ -51,11 +73,27 @@ public void removeAlias(String name) { aliases.remove(name); saveAliases(); } + + public String getAlias(String name) { + return aliases.get(name); + } public boolean hasAlias(String name) { return aliases.containsKey(name); } + // Environmental variable accessors + public void setEnvVar(String key, String value) { + envVars.put(key, value); + } + + public String getEnvVar(String key) { + return envVars.get(key); + } + + + // --- Private Persistence Methods --- + private void loadAliases() { File aliasFile = new File(System.getProperty("user.home"), ALIAS_FILE); if (!aliasFile.exists()) { @@ -109,5 +147,4 @@ public File resolvePath(String path) { return new File(currentDir, path); } } - -} +} \ No newline at end of file From be7db4c2576a05641d0e04d259cba551bacba036 Mon Sep 17 00:00:00 2001 From: HossamSaberr Date: Tue, 28 Oct 2025 20:09:29 +0300 Subject: [PATCH 4/6] fix(lombok): Refine Lombok annotations --- src/main/java/com/mycmd/ShellContext.java | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/mycmd/ShellContext.java b/src/main/java/com/mycmd/ShellContext.java index 69246ed..568fb4a 100644 --- a/src/main/java/com/mycmd/ShellContext.java +++ b/src/main/java/com/mycmd/ShellContext.java @@ -32,6 +32,7 @@ public ShellContext() { loadAliases(); } + public void addToHistory(String command) { history.add(command); commandHistory.add(command); @@ -40,20 +41,16 @@ public void addToHistory(String command) { } } - /** * OVERRIDES Lombok's generated getter to return a DEFENSIVE COPY - * to prevent external code from modifying the shell directly. - */ - @Override + + /** * RETAINED FOR SAFETY: Returns a DEFENSIVE COPY instead of the raw Map. */ public List getHistory() { return new ArrayList<>(history); } - @Override public Map getAliases() { return new HashMap<>(aliases); } - @Override public Map getEnvVars() { return new HashMap<>(envVars); } @@ -63,7 +60,6 @@ public void clearHistory() { history.clear(); } - // Alias management methods public void addAlias(String name, String command) { aliases.put(name, command); saveAliases(); @@ -82,7 +78,6 @@ public boolean hasAlias(String name) { return aliases.containsKey(name); } - // Environmental variable accessors public void setEnvVar(String key, String value) { envVars.put(key, value); } @@ -92,7 +87,6 @@ public String getEnvVar(String key) { } - // --- Private Persistence Methods --- private void loadAliases() { File aliasFile = new File(System.getProperty("user.home"), ALIAS_FILE); From 714734517271682611324e9cb5f3aa17f51cfd81 Mon Sep 17 00:00:00 2001 From: HossamSaberr Date: Tue, 28 Oct 2025 20:17:09 +0300 Subject: [PATCH 5/6] fix(lombok): do some reviwes --- src/main/java/com/mycmd/ShellContext.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/mycmd/ShellContext.java b/src/main/java/com/mycmd/ShellContext.java index 568fb4a..704f71d 100644 --- a/src/main/java/com/mycmd/ShellContext.java +++ b/src/main/java/com/mycmd/ShellContext.java @@ -13,8 +13,9 @@ public class ShellContext { @Setter @NonNull private File currentDir; - + @Getter(AccessLevel.NONE) private List history; + @Getter(AccessLevel.NONE) private Map aliases; private static final String ALIAS_FILE = ".mycmd_aliases"; private static final int MAX_HISTORY = 100; From d232cbd010f136a1d94427631b2300169831d28a Mon Sep 17 00:00:00 2001 From: HossamSaberr Date: Tue, 28 Oct 2025 20:20:39 +0300 Subject: [PATCH 6/6] fix(lombok): do some reviwes --- src/main/java/com/mycmd/ShellContext.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/mycmd/ShellContext.java b/src/main/java/com/mycmd/ShellContext.java index 704f71d..9db6dd6 100644 --- a/src/main/java/com/mycmd/ShellContext.java +++ b/src/main/java/com/mycmd/ShellContext.java @@ -3,24 +3,25 @@ import lombok.Getter; import lombok.Setter; import lombok.NonNull; +import lombok.AccessLevel; import java.io.*; import java.util.*; import java.time.Instant; -@Getter +@Getter(AccessLevel.PUBLIC) public class ShellContext { @Setter @NonNull private File currentDir; - @Getter(AccessLevel.NONE) private List history; - @Getter(AccessLevel.NONE) private Map aliases; + private static final String ALIAS_FILE = ".mycmd_aliases"; private static final int MAX_HISTORY = 100; + private final List commandHistory; - private final Instant startTime; + private final Instant startTime; private final Map envVars = new HashMap<>(); @@ -43,7 +44,7 @@ public void addToHistory(String command) { } - /** * RETAINED FOR SAFETY: Returns a DEFENSIVE COPY instead of the raw Map. */ + /** RETAINED FOR SAFETY: Returns a DEFENSIVE COPY instead of the raw Map. */ public List getHistory() { return new ArrayList<>(history); } @@ -87,10 +88,9 @@ public String getEnvVar(String key) { return envVars.get(key); } - - private void loadAliases() { File aliasFile = new File(System.getProperty("user.home"), ALIAS_FILE); + // ... (method body remains the same) if (!aliasFile.exists()) { return; } @@ -116,6 +116,7 @@ private void loadAliases() { private void saveAliases() { File aliasFile = new File(System.getProperty("user.home"), ALIAS_FILE); + // ... (method body remains the same) try (BufferedWriter writer = new BufferedWriter(new FileWriter(aliasFile))) { writer.write("# MyCMD Aliases Configuration\n"); writer.write("# Format: aliasName=command\n\n");