I have written Java code for removing comments, annotations and extra spaces from Java source code.
package com.dukesoftware.utils.text;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import com.dukesoftware.utils.io.IOUtils;
public class JavaSourceUtils {
public static void main(String[] args) throws FileNotFoundException, IOException {
String cleanedUpSource = removeCommentAndAnnotation(IOUtils.userDirectory(
"workspace2/DukeSoftwareUtils/src/main/java/com/dukesoftware/utils/text/JavaSourceUtils.java"
));
System.out.println(cleanedUpSource);
}
public static final String removeCommentAndAnnotation(File file) throws IOException {
try(BufferedReader br = new BufferedReader(new FileReader(file)))
{
return removeCommentAndAnnotation(br);
}
}
private final static int CODE = 0;
private final static int IN_STRING = 1;
private final static int LINE_IN_COMMENT = 2;
private final static int IN_CHAR = 3;
public static final String removeCommentAndAnnotation(BufferedReader reader) throws IOException {
String line;
StringBuilder result = new StringBuilder();
int status = CODE;
while((line = reader.readLine()) != null){
line = line.trim();
if (line.isEmpty()) continue;
if (line.startsWith("@")) continue;
char[] chars = line.toCharArray();
for(int current = 0; current < chars.length; current++)
{
char c = chars[current];
final int next = current+1;
switch(c)
{
case '\\':
current++;
result.append(c);
if(next < chars.length) result.append(chars[next]);
break;
case '/':
if(status == CODE)
{
if(next < chars.length)
{
if(chars[next] == '/')
{
current = chars.length; // skip rest of line
}
else if(chars[next] == '*')
{
status = LINE_IN_COMMENT;
}
else
{
result.append(c);
}
}
else
{
result.append(c);
}
}
else if(status == IN_STRING || status == IN_CHAR)
{
result.append(c);
}
break;
case '*':
if(status == LINE_IN_COMMENT && next < chars.length && chars[next] == '/')
{
status = CODE;
current++;
}
else
{
result.append(c);
}
break;
case '"':
if(status == CODE)
{
status = IN_STRING;
}
else if(status == IN_STRING)
{
status = CODE;
}
result.append(c);
break;
case '\'':
if(status == CODE)
{
status = IN_CHAR;
}
else if(status == IN_CHAR)
{
status = CODE;
}
result.append(c);
break;
default:
result.append(c);
break;
}
}
result.append("\n");
}
return result.toString();
}
}
コメント