Skip to content

Commit 9347530

Browse files
committed
Add @IgnoreNonStaticTraceComponent annotation for instance-specific tracing
- Created new annotation to mark legitimate non-static TraceComponent fields - Updated instrumentation tool to skip annotated fields - Migrated 3 files from Object-cast workaround to direct field access - Eliminates method call and cast overhead on every trace operation - Improves code clarity and maintainability
1 parent e9fa577 commit 9347530

File tree

26 files changed

+1060
-89
lines changed

26 files changed

+1060
-89
lines changed
3.36 KB
Binary file not shown.

DirectoryComparer/Makefile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
JAVAC = javac
2+
JAVA = java
3+
JAR = jar
4+
SRC_DIR = src
5+
BIN_DIR = bin
6+
MAIN_CLASS = com.dircompare.DirectoryComparer
7+
CLASSPATH = $(BIN_DIR)
8+
JAR_FILE = DirectoryComparer.jar
9+
10+
all: compile
11+
12+
compile:
13+
@mkdir -p $(BIN_DIR)
14+
$(JAVAC) -d $(BIN_DIR) $(SRC_DIR)/com/dircompare/DirectoryComparer.java
15+
16+
jar: compile
17+
$(JAR) cfm $(JAR_FILE) manifest.txt -C $(BIN_DIR) .
18+
19+
run:
20+
$(JAVA) -cp $(CLASSPATH) $(MAIN_CLASS) $(DIR1) $(DIR2)
21+
22+
run-jar:
23+
$(JAVA) -jar $(JAR_FILE) $(DIR1) $(DIR2)
24+
25+
clean:
26+
rm -rf $(BIN_DIR)
27+
rm -f $(JAR_FILE)
28+
29+
.PHONY: all compile jar run run-jar clean

DirectoryComparer/README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Directory Comparer
2+
3+
A Java utility to compare Java files between two directories.
4+
5+
## Features
6+
7+
- Identifies Java files that are in the first directory but not in the second
8+
- Identifies Java files that are in the second directory but not in the first
9+
- Identifies Java files that exist in both directories but have different content
10+
- Ignores all non-Java files
11+
12+
## Requirements
13+
14+
- Java JDK 8 or higher
15+
- Make (optional, for using the Makefile)
16+
17+
## Building the Project
18+
19+
Using Make:
20+
21+
```bash
22+
cd DirectoryComparer
23+
make compile
24+
```
25+
26+
Manually:
27+
28+
```bash
29+
cd DirectoryComparer
30+
mkdir -p bin
31+
javac -d bin src/com/dircompare/DirectoryComparer.java
32+
```
33+
34+
## Running the Program
35+
36+
Using Make:
37+
38+
```bash
39+
make run DIR1=/path/to/first/directory DIR2=/path/to/second/directory
40+
```
41+
42+
Manually:
43+
44+
```bash
45+
java -cp bin com.dircompare.DirectoryComparer /path/to/first/directory /path/to/second/directory
46+
```
47+
48+
## Creating and Running a JAR File
49+
50+
To create an executable JAR file:
51+
52+
```bash
53+
cd DirectoryComparer
54+
make jar
55+
```
56+
57+
This will create `DirectoryComparer.jar` in the project directory.
58+
59+
To run the JAR file:
60+
61+
```bash
62+
java -jar DirectoryComparer.jar /path/to/first/directory /path/to/second/directory
63+
```
64+
65+
Or using Make:
66+
67+
```bash
68+
make run-jar DIR1=/path/to/first/directory DIR2=/path/to/second/directory
69+
```
70+
71+
## Example Output
72+
73+
```
74+
Directory Comparison Results:
75+
============================
76+
77+
Files only in /path/to/first/directory:
78+
SomeFile.java
79+
subdir/AnotherFile.java
80+
81+
Files only in /path/to/second/directory:
82+
NewFile.java
83+
84+
Files with different content:
85+
CommonFile.java
86+
utils/Helper.java
87+
```
88+
89+
## How It Works
90+
91+
The program recursively scans both directories, filtering for Java files only. It then:
92+
93+
1. Compares file paths to identify files unique to each directory
94+
2. For files that exist in both directories, compares their content byte-by-byte
95+
3. Outputs the results in a clear, categorized format

DirectoryComparer/manifest.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Main-Class: com.dircompare.DirectoryComparer
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package com.dircompare;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
import java.util.ArrayList;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.stream.Collectors;
12+
import java.util.stream.Stream;
13+
14+
/**
15+
* A utility to compare Java files between two directories.
16+
* Identifies files that are:
17+
* 1. In the first directory but not in the second
18+
* 2. In the second directory but not in the first
19+
* 3. In both directories but with different contents
20+
*/
21+
public class DirectoryComparer {
22+
23+
public static void main(String[] args) {
24+
if (args.length != 2) {
25+
System.out.println("Usage: java DirectoryComparer <directory1> <directory2>");
26+
System.exit(1);
27+
}
28+
29+
String dir1Path = args[0];
30+
String dir2Path = args[1];
31+
32+
DirectoryComparer comparer = new DirectoryComparer();
33+
try {
34+
comparer.compareDirectories(dir1Path, dir2Path);
35+
} catch (IOException e) {
36+
System.err.println("Error comparing directories: " + e.getMessage());
37+
e.printStackTrace();
38+
}
39+
}
40+
41+
/**
42+
* Compares two directories and prints the differences.
43+
*
44+
* @param dir1Path Path to the first directory
45+
* @param dir2Path Path to the second directory
46+
* @throws IOException If an I/O error occurs
47+
*/
48+
public void compareDirectories(String dir1Path, String dir2Path) throws IOException {
49+
Path path1 = Paths.get(dir1Path);
50+
Path path2 = Paths.get(dir2Path);
51+
52+
// Validate directories
53+
if (!Files.isDirectory(path1)) {
54+
throw new IllegalArgumentException(dir1Path + " is not a directory");
55+
}
56+
if (!Files.isDirectory(path2)) {
57+
throw new IllegalArgumentException(dir2Path + " is not a directory");
58+
}
59+
60+
// Get all Java files from both directories
61+
Map<String, Path> javaFilesDir1 = findJavaFiles(path1);
62+
Map<String, Path> javaFilesDir2 = findJavaFiles(path2);
63+
64+
// Find files in dir1 but not in dir2
65+
List<String> onlyInDir1 = new ArrayList<>();
66+
for (String relativePath : javaFilesDir1.keySet()) {
67+
if (!javaFilesDir2.containsKey(relativePath)) {
68+
onlyInDir1.add(relativePath);
69+
}
70+
}
71+
72+
// Find files in dir2 but not in dir1
73+
List<String> onlyInDir2 = new ArrayList<>();
74+
for (String relativePath : javaFilesDir2.keySet()) {
75+
if (!javaFilesDir1.containsKey(relativePath)) {
76+
onlyInDir2.add(relativePath);
77+
}
78+
}
79+
80+
// Find files in both directories but with different content
81+
List<String> differentContent = new ArrayList<>();
82+
for (String relativePath : javaFilesDir1.keySet()) {
83+
if (javaFilesDir2.containsKey(relativePath)) {
84+
Path file1 = javaFilesDir1.get(relativePath);
85+
Path file2 = javaFilesDir2.get(relativePath);
86+
87+
if (!filesHaveSameContent(file1, file2)) {
88+
differentContent.add(relativePath);
89+
}
90+
}
91+
}
92+
93+
// Print results
94+
System.out.println("Directory Comparison Results:");
95+
System.out.println("============================");
96+
97+
System.out.println("\nFiles only in " + dir1Path + ":");
98+
if (onlyInDir1.isEmpty()) {
99+
System.out.println("None");
100+
} else {
101+
onlyInDir1.forEach(file -> System.out.println(" " + file));
102+
}
103+
104+
System.out.println("\nFiles only in " + dir2Path + ":");
105+
if (onlyInDir2.isEmpty()) {
106+
System.out.println("None");
107+
} else {
108+
onlyInDir2.forEach(file -> System.out.println(" " + file));
109+
}
110+
111+
System.out.println("\nFiles with different content:");
112+
if (differentContent.isEmpty()) {
113+
System.out.println("None");
114+
} else {
115+
differentContent.forEach(file -> System.out.println(" " + file));
116+
}
117+
}
118+
119+
/**
120+
* Finds all Java files in the given directory and its subdirectories.
121+
*
122+
* @param directory The directory to search
123+
* @return A map of relative paths to absolute paths for all Java files
124+
* @throws IOException If an I/O error occurs
125+
*/
126+
private Map<String, Path> findJavaFiles(Path directory) throws IOException {
127+
Map<String, Path> javaFiles = new HashMap<>();
128+
129+
try (Stream<Path> walk = Files.walk(directory)) {
130+
List<Path> files = walk
131+
.filter(Files::isRegularFile)
132+
.filter(path -> path.toString().toLowerCase().endsWith(".java"))
133+
.collect(Collectors.toList());
134+
135+
for (Path file : files) {
136+
String relativePath = directory.relativize(file).toString();
137+
javaFiles.put(relativePath, file);
138+
}
139+
}
140+
141+
return javaFiles;
142+
}
143+
144+
/**
145+
* Compares the content of two files.
146+
*
147+
* @param file1 Path to the first file
148+
* @param file2 Path to the second file
149+
* @return true if the files have the same content, false otherwise
150+
* @throws IOException If an I/O error occurs
151+
*/
152+
private boolean filesHaveSameContent(Path file1, Path file2) throws IOException {
153+
byte[] content1 = Files.readAllBytes(file1);
154+
byte[] content2 = Files.readAllBytes(file2);
155+
156+
if (content1.length != content2.length) {
157+
return false;
158+
}
159+
160+
for (int i = 0; i < content1.length; i++) {
161+
if (content1[i] != content2[i]) {
162+
return false;
163+
}
164+
}
165+
166+
return true;
167+
}
168+
}
169+
170+
// Made with Bob

0 commit comments

Comments
 (0)