Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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>
69 changes: 31 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,39 +32,34 @@ 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);
}
}


/** * RETAINED FOR SAFETY: Returns a DEFENSIVE COPY instead of the raw Map. */
public List<String> getHistory() {
return new ArrayList<>(history);
}

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

public Instant getStartTime() {
return startTime;
public Map<String, String> getEnvVars() {
return new HashMap<>(envVars);
}


public void clearHistory() {
history.clear();
}

// Alias management methods
public void addAlias(String name, String command) {
aliases.put(name, command);
saveAliases();
Expand All @@ -64,19 +69,25 @@ 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);
}

public void setEnvVar(String key, String value) {
envVars.put(key, value);
}

public String getEnvVar(String key) {
return envVars.get(key);
}



private void loadAliases() {
File aliasFile = new File(System.getProperty("user.home"), ALIAS_FILE);
if (!aliasFile.exists()) {
Expand Down Expand Up @@ -130,22 +141,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);
}

}
}