From 6c591bbf37df74637734adc0bf78448670682d49 Mon Sep 17 00:00:00 2001 From: Mandeep Moun Date: Sat, 11 Oct 2025 00:21:45 -0600 Subject: [PATCH] Added error handling --- .../javaparser-1.0.0/LCA_JP1_0_0.java | 53 +++++++++++++------ Assignment-2/javaparser-1.0.0/test.java | 11 ++-- 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/Assignment-2/javaparser-1.0.0/LCA_JP1_0_0.java b/Assignment-2/javaparser-1.0.0/LCA_JP1_0_0.java index a51253a..89800cc 100644 --- a/Assignment-2/javaparser-1.0.0/LCA_JP1_0_0.java +++ b/Assignment-2/javaparser-1.0.0/LCA_JP1_0_0.java @@ -4,33 +4,56 @@ import japa.parser.ast.body.*; import japa.parser.ast.visitor.VoidVisitorAdapter; import java.io.FileInputStream; +import java.io.IOException; import java.io.File; public class LCA_JP1_0_0 { public static void main(String[] args) throws Exception { + // Checks if file is provided as command line argument if (args.length == 0) { System.out.println("Usage: java LCA_JP1_0_0 "); return; } + // Makes sure file exits and can be read + File sourFile = new File(args[0]); + if (!sourFile.exists() || !sourFile.isFile() || !sourFile.canRead()) { + System.err.println("Error: File can't be read or it doesn't exist: " + args[0]); + return; + } + + try (FileInputStream in = new FileInputStream(sourFile)) // Parse the given Java file - FileInputStream in = new FileInputStream(new File(args[0])); - CompilationUnit cu = JavaParser.parse(in); - - // Traverse the AST and print local classes - new VoidVisitorAdapter() { - - @Override - public void visit(MethodDeclaration n, Void arg) { - super.visit(n, arg); + { + CompilationUnit cu; + try { + cu = JavaParser.parse(in); + } catch (Exception e) { + System.err.println("Error: Failed to parse file"); + e.printStackTrace(); + return; } + // Traverse the AST and print local classes + new VoidVisitorAdapter() { - @Override - public void visit(ClassOrInterfaceDeclaration n, Void arg) { - System.out.println("Found class: " + n.getName()); - super.visit(n, arg); - } - }.visit(cu, null); + @Override + public void visit(MethodDeclaration n, Void arg) { + super.visit(n, arg); + } + + @Override + public void visit(ClassOrInterfaceDeclaration n, Void arg) { + System.out.println("Found class: " + n.getName()); + super.visit(n, arg); + } + }.visit(cu, null); + } catch (IOException e) { + System.err.println("Error: Unable to read the file."); + e.printStackTrace(); + } catch (Exception e) { + System.err.println("Uneaxpected error occured."); + e.printStackTrace(); + } } } diff --git a/Assignment-2/javaparser-1.0.0/test.java b/Assignment-2/javaparser-1.0.0/test.java index 2946a3f..fca2c8c 100644 --- a/Assignment-2/javaparser-1.0.0/test.java +++ b/Assignment-2/javaparser-1.0.0/test.java @@ -1,14 +1,17 @@ public class test { void methodOne() { - class Helper234 { - void doStuff() {} + public class Helper234 { + void doStuff() { + } } - class Helper2 { - void doStuff() {} + private class Helper2 { + void doStuff() { + } } } void methodTwo() { System.out.println("Hello"); } + }