Skip to content
Snippets Groups Projects
Commit 7a9710c8 authored by sperson's avatar sperson
Browse files

initial commit

parents
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="output" path="bin"/>
</classpath>
/bin/
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>SETools</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
package cse.unl.edu.code.size;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class ComputeCodeSize {
static boolean ignoreComments = false;
int loc=0;
private static final long MEGABYTE = 1024L * 1024L;
public static long bytesToMegabytes(long bytes) {
return bytes / MEGABYTE;
}
private List<File> getFileListingNoSort(File aStartingDir)
throws FileNotFoundException {
List<File> result = new ArrayList<File>();
File[] filesAndDirs = aStartingDir.listFiles();
List<File> filesDirs = Arrays.asList(filesAndDirs);
for(File file : filesDirs) {
result.add(file); //always add, even if directory
if ( ! file.isFile() ) {
//must be a directory
//recursive call!
List<File> deeperList = getFileListingNoSort(file);
result.addAll(deeperList);
}
}
return result;
}
private void validateDirectory (File aDirectory) throws FileNotFoundException {
if (aDirectory == null) {
throw new IllegalArgumentException("Directory should not be null.");
}
if (!aDirectory.exists()) {
throw new FileNotFoundException("Directory does not exist: " + aDirectory);
}
if (!aDirectory.isDirectory()) {
throw new IllegalArgumentException("Is not a directory: " + aDirectory);
}
if (!aDirectory.canRead()) {
throw new IllegalArgumentException("Directory cannot be read: " + aDirectory);
}
}
public List<File> getFileListing(File aStartingDir)
throws FileNotFoundException {
validateDirectory(aStartingDir);
List<File> result = getFileListingNoSort(aStartingDir);
Collections.sort(result);
return result;
}
private List<String> removeComments(File f){
ArrayList<String> lines= new ArrayList<String>();
InputStream is = null;
//Read the contents of the files
try{
is = new FileInputStream(f);
}catch (Exception e){
e.printStackTrace();
}
BufferedReader br = new BufferedReader (new InputStreamReader(is));
//each line read in
String line="";
String tmp = "";
try{
boolean inComment = false;
while((line=br.readLine())!=null){
if (line.trim().length()>0){
if (inComment){ //ignore everything until end of comment
if (line.contains("*/") && !line.contains("\"*/\"")){
tmp = line.substring(line.indexOf("*/")+2);
if (tmp.trim().length() >0)
lines.add(tmp);
inComment = false;
}
}else{
//check for comments
if (line.contains("/*") && !line.contains("\"/*\"")){
//don't add if there is only a comment on this line
//don't consider it a comment if it is part of a string
if (line.indexOf("/*") != 0 && !line.contains("\"/*\"")){
tmp = line.substring(0, line.indexOf("/*")-1);
if (tmp.trim().length()>0)
lines.add(tmp);
}
String restOfLine = line.substring(line.indexOf("/*"));
if (!restOfLine.endsWith("*/"))
inComment = true;
} else if (line.contains("//") && !line.contains("\"//\"")){
if (line.indexOf("//") != 0 ){
tmp = line.substring(0, line.indexOf("//")-1);
if (tmp.trim().length()>0)
lines.add(tmp);
}
}else
lines.add(line);
}
}
}
is.close();
}catch(Exception e){
e.printStackTrace();
}
return lines;
}
private int getFileLOC(File file){
int count = 0;
try{
InputStream is = null;
//Read the contents of the files
is = new FileInputStream(file);
BufferedReader br = new BufferedReader (new InputStreamReader(is));
//each line read in
String line="";
while((line=br.readLine())!=null){
if (line.trim().length()>0)
count++;
//System.out.println(line);
}
br.close();
return count;
}catch(Exception e){
e.printStackTrace();
return count;
}
}
private void countLOC(String fileName){
try{
File file = new File(fileName);
if (file.isDirectory()){
int totalLOC = 0;
List<File> files = getFileListing(file);
// Get the Java runtime
Runtime runtime = Runtime.getRuntime();
// Run the garbage collector
runtime.gc();
// Calculate the used memory
//totalMemory is the JVM heap capacity
//freeMemory is unused memory
long memory = runtime.totalMemory() - runtime.freeMemory();
System.out.println("Used memory in bytes: " + memory);
System.out.println("Used memory in megabytes: "
+ bytesToMegabytes(memory));
Iterator<File> it0 = files.iterator();
while (it0.hasNext()){
File f = it0.next();
if (f.getName().endsWith(".java")){
String code = "";
Runtime rt = Runtime.getRuntime();
// Run the garbage collector
runtime.gc();
// Calculate the used memory
//totalMemory is the JVM heap capacity
//freeMemory is unused memory
long mem = rt.totalMemory() - rt.freeMemory();
System.out.println("Used memory in bytes: " + mem);
System.out.println("Used memory in megabytes: "
+ bytesToMegabytes(mem));
if (ignoreComments){
List<String> lines = removeComments(f);
int loc = lines.size();
System.out.println("File " + f.getAbsolutePath() + " contains "
+ loc + " lines of code");
totalLOC = totalLOC + loc;
}else{
int loc = getFileLOC(f);
System.out.println("File " + f.getAbsolutePath() + " contains "
+ loc + " lines of code");
totalLOC = totalLOC + loc;
}
}
}
System.out.println("Total LOC is: " + totalLOC);
}else{
if (file.getName().endsWith(".java")){
int count = 0;
if (ignoreComments){
count = removeComments(file).size();
}else{
count = getFileLOC(file);
}
System.out.println("File " + fileName + " contains " + count + " lines of code");
}
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
try{
long startTime = System.currentTimeMillis();
String fileName = "";
if (args.length < 2){
System.out.println("USAGE: java ComputeCodeSize -f <file>.java OR " +
"<directory name> -c where");
System.out.println("-c: indicates ignore comments");
System.exit(0);
}else{
for (int i=0; i<args.length; i++){
if (args[i].startsWith("-f")) {
fileName = args[i+1];
i++;
}else if (args[i].startsWith("-c")) {
ignoreComments = true;
}
}
}
File file = new File(fileName);
if (file.exists()) {
ComputeCodeSize compute = new ComputeCodeSize();
compute.countLOC(fileName);
}else{
System.out.println("ComputeCodeSize: The source file or directory does not exist...");
}
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("Elapsed time in milliseconds is: " + elapsedTime);
}catch(Exception e){
e.printStackTrace();
}
}
}
package cse.unl.edu.code.size.test;
public class MultipleEmptyLines {
private void doNothing(){
}
}
package cse.unl.edu.code.size.test;
public class NoEmptyLines {
private void doNothing(){
}
}
package cse.unl.edu.code.size.test;
public class OneEmptyLine {
private void doNothing(){
}
}
package cse.unl.edu.code.size.test;
/*
* A comment here
*/
public class WithComments {
private void doNothing(){ //foobar
//a comment here
int i=0;/*
foo
*/
int y=1; /*bar*/
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment