Skip to content

Instantly share code, notes, and snippets.

@KILL3RTACO
Created January 4, 2014 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KILL3RTACO/8251950 to your computer and use it in GitHub Desktop.
Save KILL3RTACO/8251950 to your computer and use it in GitHub Desktop.
Program designed to profile Java apps/scripts/programs
package com.kill3rtaco.programutil; //either change or remove
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ProgramStats {
private String project;
private int packages, classes, lines,
emptyLines, trueLines;
public ProgramStats(String project, String format) {
this.project = project;
packages = classes = lines = 0;
count(new File(format.replaceAll("%project", project)));
packages -= 1;
}
//<workspace folder> <project>
public static void main(String[] args) {
ProgramStats stats;
if(args.length == 0) {
System.out.println("missing eclipse workspace folder");
return;
} else if(args.length == 1) {
System.out.println("missing project name");
return;
} else {
stats = new ProgramStats(args[1], args[0] + "/%project/src");
}
stats.showStats();
}
private void count(File file) {
if(file.isDirectory()) {
packages++;
for(File f : file.listFiles()) {
count(f);
}
} else {
try {
if(!file.getName().substring(file.getName().lastIndexOf(".")).equalsIgnoreCase(".java"))
return;
Scanner x = new Scanner(file);
classes++;
System.out.println("Reading " + file.getName() + "...");
while (x.hasNextLine()) {
String line = x.nextLine().trim();
if(line.equalsIgnoreCase(""))
emptyLines++;
if(line.length() > 3 && !line.startsWith("import") && !line.startsWith("package"))
trueLines++;
lines++;
}
x.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
public int getPackages() {
return packages;
}
public int getClasses() {
return classes;
}
public int getEmptyLines() {
return emptyLines;
}
public int getLines() {
return lines;
}
public void showStats() {
System.out.println("\nStats for " + project);
System.out.println("============================");
System.out.printf("Total lines: %5d\n", lines);
System.out.printf("True lines: %5d\n", trueLines);
System.out.printf("Empty lines: %5d\n", emptyLines);
System.out.printf("Classes: %5d\n", classes);
System.out.printf("Packages: %5d\n", packages);
}
public static void countAll() {
int tl = 0, tel = 0, tc = 0, tp = 0;
String root = "/home/taco/.workspace";
for(File f : new File(root).listFiles()) {
if(f.isDirectory() && !f.getName().equalsIgnoreCase(".metadata")) {
String format = root + "/%project/src";
ProgramStats stats = new ProgramStats(f.getName(), format);
tl += stats.getLines();
tel += stats.getEmptyLines();
tc += stats.getClasses();
tp += stats.getPackages();
}
}
System.out.println("\nStats for all projects");
System.out.println("==========================================");
System.out.printf("Total number of packages: %5d\n", tp);
System.out.printf("Total number of classes: %5d\n", tc);
System.out.printf("Total number of lines: %5d\n", tl);
System.out.printf("Total number of empty lines: %5d\n", tel);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment