diff --git a/src/main/java/edu/university/ecs/lab/common/models/Annotation.java b/src/main/java/edu/university/ecs/lab/common/models/Annotation.java new file mode 100644 index 00000000..ad122256 --- /dev/null +++ b/src/main/java/edu/university/ecs/lab/common/models/Annotation.java @@ -0,0 +1,35 @@ +package edu.university.ecs.lab.common.models; + +import lombok.*; + +import javax.json.Json; +import javax.json.JsonObject; +import javax.json.JsonObjectBuilder; + +/** Represents an annotation in Java */ +@Data +@AllArgsConstructor +@NoArgsConstructor +@ToString +public class Annotation implements JsonSerializable{ + /** The name of the annotation **/ + protected String annotationName; + + /** The contents of the annotation **/ + protected String contents; + + @Override + public JsonObject toJsonObject() { + return createBuilder().build(); + } + + protected JsonObjectBuilder createBuilder() { + JsonObjectBuilder methodObjectBuilder = Json.createObjectBuilder(); + + methodObjectBuilder.add("annotationName", annotationName); + methodObjectBuilder.add("contents", contents); + + return methodObjectBuilder; + } + +} diff --git a/src/main/java/edu/university/ecs/lab/common/models/Endpoint.java b/src/main/java/edu/university/ecs/lab/common/models/Endpoint.java index 5c9e1def..7d1b5b89 100644 --- a/src/main/java/edu/university/ecs/lab/common/models/Endpoint.java +++ b/src/main/java/edu/university/ecs/lab/common/models/Endpoint.java @@ -37,7 +37,7 @@ public class Endpoint extends Method implements JsonSerializable { // private String mappingPath; public Endpoint(Method method, String url, String decorator, String httpMethod, String msId) { - super(method.getMethodName(), method.getParameterList(), method.getReturnType()); + super(method.getMethodName(), method.getParameterList(), method.getReturnType(), method.getAnnotations()); setMsId(msId); setUrl(url); setDecorator(decorator); diff --git a/src/main/java/edu/university/ecs/lab/common/models/JClass.java b/src/main/java/edu/university/ecs/lab/common/models/JClass.java index eab097a0..4a1372e5 100644 --- a/src/main/java/edu/university/ecs/lab/common/models/JClass.java +++ b/src/main/java/edu/university/ecs/lab/common/models/JClass.java @@ -29,6 +29,9 @@ public class JClass implements JsonSerializable { @SerializedName("variables") protected List fields; + /** Class level annotations **/ + protected List annotations; + protected List methodCalls; /** The associated microservice object for this class */ @@ -50,16 +53,20 @@ public JClass(String className, ClassRole classRole, List methods, List fields, + List annotations, List methodCalls, String msId) { - this.className = className; + + setClassName(className); setClassPath(classPath); - this.packageName = packageName; - this.classRole = classRole; - this.methods = methods; - this.fields = fields; - this.methodCalls = methodCalls; - this.msId = msId; + setPackageName(packageName); + setClassRole(classRole); + setMethods(methods); + setFields(fields); + setAnnotations(annotations); + setMethodCalls(methodCalls); + setMsId(msId); + } /** @@ -75,14 +82,15 @@ public JsonObject toJsonObject() { protected JsonObjectBuilder createBuilder() { JsonObjectBuilder jClassBuilder = Json.createObjectBuilder(); - jClassBuilder.add("className", this.className); - jClassBuilder.add("classPath", this.classPath); - jClassBuilder.add("packageName", this.packageName); - jClassBuilder.add("classRole", this.classRole.name()); - jClassBuilder.add("msId", msId); - jClassBuilder.add("methods", listToJsonArray(methods)); - jClassBuilder.add("variables", listToJsonArray(fields)); - jClassBuilder.add("methodCalls", listToJsonArray(methodCalls)); + jClassBuilder.add("className", getClassName()); + jClassBuilder.add("classPath", getClassPath()); + jClassBuilder.add("packageName", getPackageName()); + jClassBuilder.add("classRole", getClassRole().name()); + jClassBuilder.add("msId", getMsId()); + jClassBuilder.add("methods", listToJsonArray(getMethods())); + jClassBuilder.add("variables", listToJsonArray(getFields())); + jClassBuilder.add("methodCalls", listToJsonArray(getMethodCalls())); + jClassBuilder.add("annotations", listToJsonArray(getAnnotations())); return jClassBuilder; } diff --git a/src/main/java/edu/university/ecs/lab/common/models/JController.java b/src/main/java/edu/university/ecs/lab/common/models/JController.java index 92412d40..90da59a5 100644 --- a/src/main/java/edu/university/ecs/lab/common/models/JController.java +++ b/src/main/java/edu/university/ecs/lab/common/models/JController.java @@ -27,6 +27,7 @@ public JController(JClass jClass) { jClass.getClassRole(), jClass.getMethods(), jClass.getFields(), + jClass.getAnnotations(), jClass.getMethodCalls(), jClass.getMsId() ); diff --git a/src/main/java/edu/university/ecs/lab/common/models/JService.java b/src/main/java/edu/university/ecs/lab/common/models/JService.java index 81850c17..da8144d8 100644 --- a/src/main/java/edu/university/ecs/lab/common/models/JService.java +++ b/src/main/java/edu/university/ecs/lab/common/models/JService.java @@ -25,6 +25,7 @@ public JService(@NonNull JClass jClass) { jClass.getClassRole(), jClass.getMethods(), jClass.getFields(), + jClass.getAnnotations(), jClass.getMethodCalls(), jClass.getMsId() ); diff --git a/src/main/java/edu/university/ecs/lab/common/models/Method.java b/src/main/java/edu/university/ecs/lab/common/models/Method.java index 86ed26c2..266e2742 100644 --- a/src/main/java/edu/university/ecs/lab/common/models/Method.java +++ b/src/main/java/edu/university/ecs/lab/common/models/Method.java @@ -9,6 +9,9 @@ import javax.json.Json; import javax.json.JsonObject; import javax.json.JsonObjectBuilder; +import java.util.List; + +import static edu.university.ecs.lab.common.utils.ObjectToJsonUtils.listToJsonArray; /** Represents a method declaration in Java. */ @Data @@ -26,6 +29,9 @@ public class Method implements JsonSerializable { protected String returnType; + /** Method definition level annotations **/ + protected List annotations; + @Override public JsonObject toJsonObject() { return createBuilder().build(); @@ -37,6 +43,7 @@ protected JsonObjectBuilder createBuilder() { methodObjectBuilder.add("methodName", methodName); methodObjectBuilder.add("parameter", parameterList); methodObjectBuilder.add("returnType", returnType); + methodObjectBuilder.add("annotations", listToJsonArray(annotations)); return methodObjectBuilder; } diff --git a/src/main/java/edu/university/ecs/lab/common/utils/SourceToObjectUtils.java b/src/main/java/edu/university/ecs/lab/common/utils/SourceToObjectUtils.java index 6a5e928a..e4f7cf74 100644 --- a/src/main/java/edu/university/ecs/lab/common/utils/SourceToObjectUtils.java +++ b/src/main/java/edu/university/ecs/lab/common/utils/SourceToObjectUtils.java @@ -13,12 +13,14 @@ import edu.university.ecs.lab.intermediate.utils.StringParserUtils; import edu.university.ecs.lab.common.models.*; import javassist.NotFoundException; +import org.checkerframework.checker.units.qual.A; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.Optional; /** Static utility class for parsing a file and returning associated models from code structure. */ public class SourceToObjectUtils { @@ -47,10 +49,11 @@ public static JClass parseClass(File sourceFile, InputConfig config) throws IOEx .className(sourceFile.getName().replace(".java", "")) .packageName(packageName) .methods(parseMethods(cu)) - .fields(parseFields(sourceFile)) - .methodCalls(parseMethodCalls(sourceFile, msId)) + .fields(parseFields(cu)) + .methodCalls(parseMethodCalls(cu, msId)) .msId(msId) .classRole(ClassRole.fromSourceFile(sourceFile)) + .annotations(parseAnnotations(cu.getClassByName(sourceFile.getName().replace(".java", "")))) .build(); // Handle special class roles @@ -60,7 +63,7 @@ public static JClass parseClass(File sourceFile, InputConfig config) throws IOEx return controller; } else if (jClass.getClassRole() == ClassRole.SERVICE) { JService service = new JService(jClass); - service.setRestCalls(parseRestCalls(sourceFile, msId)); + service.setRestCalls(parseRestCalls(cu, msId)); return service; } @@ -216,13 +219,13 @@ public static Method parseMethod(MethodDeclaration md) { method.setParameterList(parameter.toString()); method.setReturnType(md.getTypeAsString()); + method.setAnnotations(parseAnnotations(md.getAnnotations())); return method; } - public static List parseRestCalls(File sourceFile, String msId) throws IOException { + public static List parseRestCalls(CompilationUnit cu, String msId) throws IOException { List restCalls = new ArrayList<>(); - CompilationUnit cu = StaticJavaParser.parse(sourceFile); // loop through class declarations for (ClassOrInterfaceDeclaration cid : cu.findAll(ClassOrInterfaceDeclaration.class)) { @@ -272,8 +275,7 @@ public static List parseRestCalls(File sourceFile, String msId) throws return restCalls; } - public static List parseMethodCalls(File sourceFile, String msId) throws IOException { - CompilationUnit cu = StaticJavaParser.parse(sourceFile); + public static List parseMethodCalls(CompilationUnit cu, String msId) throws IOException { List methodCalls = new ArrayList<>(); // loop through class declarations @@ -306,11 +308,9 @@ public static List parseMethodCalls(File sourceFile, String msId) th return methodCalls; } - private static List parseFields(File sourceFile) throws IOException { + private static List parseFields(CompilationUnit cu) throws IOException { List javaFields = new ArrayList<>(); - CompilationUnit cu = StaticJavaParser.parse(sourceFile); - // loop through class declarations for (ClassOrInterfaceDeclaration cid : cu.findAll(ClassOrInterfaceDeclaration.class)) { for (FieldDeclaration fd : cid.findAll(FieldDeclaration.class)) { @@ -344,6 +344,7 @@ private static String pathFromAnnotation(AnnotationExpr ae) { return ""; } + /** * Get the name of the object a method is being called from (callingObj.methodName()) * @param scope the scope to search @@ -447,4 +448,34 @@ private static String formatURL(StringLiteralExpr stringLiteralExpr) { return str; } + + private static List parseAnnotations(Optional cid) { + if(cid.isEmpty()) { + return new ArrayList<>(); + } + + return parseAnnotations(cid.get().getAnnotations()); + } + + private static List parseAnnotations(NodeList annotationExprs) { + List annotations = new ArrayList<>(); + + for(AnnotationExpr ae : annotationExprs) { + Annotation annotation; + if (ae.isNormalAnnotationExpr()) { + NormalAnnotationExpr normal = ae.asNormalAnnotationExpr(); + annotation = new Annotation(ae.getNameAsString(), normal.getPairs().toString()); + + } else if (ae.isSingleMemberAnnotationExpr()) { + annotation = new Annotation(ae.getNameAsString(), ae.asSingleMemberAnnotationExpr().getMemberValue().toString()); + } else { + annotation = new Annotation(ae.getNameAsString(), ""); + + } + + annotations.add(annotation); + } + + return annotations; + } }