Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,33 @@
<maven.compiler.target>17</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.32</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Creates executable jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.32</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
Expand All @@ -34,4 +58,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
75 changes: 37 additions & 38 deletions src/main/java/com/mycmd/ShellContext.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package com.mycmd;

import lombok.Getter;
import lombok.Setter;
import lombok.NonNull;
import java.io.*;
import java.util.*;
import java.time.Instant;

@Getter
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Fix: Class-level @Getter conflicts with custom getter implementations.

The class-level @Getter generates getters for all fields, but you're attempting to provide custom implementations for getHistory(), getAliases(), and getEnvVars() that return defensive copies. This approach causes the @Override compilation errors on lines 46, 51, and 56 because you cannot override methods generated in the same class.

Solution: Exclude specific fields from the class-level @Getter by annotating them with @Getter(AccessLevel.NONE), then provide your custom getters without @Override.

Apply this diff:

+import lombok.AccessLevel;
+
 @Getter 
 public class ShellContext {
     
     @Setter
     @NonNull 
     private File currentDir;
     
+    @Getter(AccessLevel.NONE)
     private List<String> history;
+    @Getter(AccessLevel.NONE)
     private Map<String, String> aliases;
     private static final String ALIAS_FILE = ".mycmd_aliases";
     private static final int MAX_HISTORY = 100;
     private final List<String> commandHistory;
     private final Instant startTime;
 
+    @Getter(AccessLevel.NONE)
     private final Map<String, String> envVars = new HashMap<>();

Then remove the @Override annotations from lines 46, 51, and 56 (see separate comments below).

🤖 Prompt for AI Agents
In src/main/java/com/mycmd/ShellContext.java around line 10, the class-level
@Getter generates getters for all fields which conflicts with your custom
getHistory(), getAliases(), and getEnvVars() methods; annotate the specific
fields (history, aliases, envVars) with @Getter(AccessLevel.NONE) to exclude
them from Lombok generation and keep the class-level @Getter for others, then
remove the @Override annotations from your custom getter methods on lines ~46,
~51, and ~56 so they become regular methods returning defensive copies.

public class ShellContext {

@Setter
@NonNull
private File currentDir;

private List<String> history;
private Map<String, String> aliases;
private static final String ALIAS_FILE = ".mycmd_aliases";
private static final int MAX_HISTORY = 100;
private final List<String> commandHistory;
private final Instant startTime;

private final Map<String, String> envVars = new HashMap<>();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Add @Getter(AccessLevel.NONE) to the envVars field.

The envVars field has a custom getter at lines 54-56 that returns a defensive copy. Consistent with the recommendation for history and aliases, this field should also be annotated with @Getter(AccessLevel.NONE).

Apply this diff:

+    @Getter(AccessLevel.NONE)
     private final Map<String, String> envVars = new HashMap<>();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private final Map<String, String> envVars = new HashMap<>();
@Getter(AccessLevel.NONE)
private final Map<String, String> envVars = new HashMap<>();
🤖 Prompt for AI Agents
In src/main/java/com/mycmd/ShellContext.java around line 24, the envVars field
needs to prevent Lombok from generating a getter because a custom defensive-copy
getter already exists; add the annotation @Getter(AccessLevel.NONE) to the
private final Map<String, String> envVars = new HashMap<>(); and ensure
lombok.Getter and lombok.AccessLevel are imported (or existing imports include
them), so the generated getter is suppressed and the class uses the custom
getter at lines 54-56.


public ShellContext() {
this.currentDir = new File(System.getProperty("user.dir"));
this.history = new ArrayList<>();
Expand All @@ -22,34 +32,33 @@ 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
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<String> getHistory() {
return new ArrayList<>(history);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical: Remove invalid @OverRide annotation (compilation error).

Line 46's @Override annotation causes a compilation error because Lombok generates getHistory() in the same class, not in a supertype. You cannot override a method generated in the same class.

Fix: After applying the @Getter(AccessLevel.NONE) fix on the history field (see comment on line 10), remove the @Override annotation and update the misleading comment:

-    /** * OVERRIDES Lombok's generated getter to return a DEFENSIVE COPY 
-     * to prevent external code from modifying the shell directly.
+    /**
+     * Returns a defensive copy of the command history
+     * to prevent external code from modifying it directly.
      */
-    @Override
     public List<String> getHistory() {
         return new ArrayList<>(history);
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/** * OVERRIDES Lombok's generated getter to return a DEFENSIVE COPY
* to prevent external code from modifying the shell directly.
*/
@Override
public List<String> getHistory() {
return new ArrayList<>(history);
}
/**
* Returns a defensive copy of the command history
* to prevent external code from modifying it directly.
*/
public List<String> getHistory() {
return new ArrayList<>(history);
}
🧰 Tools
🪛 GitHub Actions: ⚙️ Java CI Build

[error] 46-46: Override annotation error: method does not override or implement a method from a supertype.

🤖 Prompt for AI Agents
In src/main/java/com/mycmd/ShellContext.java around lines 43 to 49, remove the
invalid @Override annotation from the getHistory() method (it causes a
compilation error because Lombok generates the getter in the same class), and
update the Javadoc to remove the "OVERRIDES" wording — instead document that
this is a custom getter that returns a defensive copy to prevent external
mutation; keep the method body returning new ArrayList<>(history) and ensure you
have previously added @Getter(AccessLevel.NONE) to the history field so Lombok
does not generate a conflicting getter.


public List<String> getCommandHistory() {
return commandHistory;
@Override
public Map<String, String> getAliases() {
return new HashMap<>(aliases);
}

public Instant getStartTime() {
return startTime;

@Override
public Map<String, String> getEnvVars() {
return new HashMap<>(envVars);
}


public void clearHistory() {
history.clear();
}
Expand All @@ -64,19 +73,27 @@ public void removeAlias(String name) {
aliases.remove(name);
saveAliases();
}

public String getAlias(String name) {
return aliases.get(name);
}

public Map<String, String> getAliases() {
return new HashMap<>(aliases);
}

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()) {
Expand Down Expand Up @@ -130,22 +147,4 @@ public File resolvePath(String path) {
return new File(currentDir, path);
}
}

// environmental variable map
// author: Kaveesha Fernando
// date: 2024-06-10
private final Map<String, String> 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<String, String> getEnvVars() {
return new HashMap<>(envVars);
}

}
}
Loading