Skip to content

Commit

Permalink
Annotations implementation IR
Browse files Browse the repository at this point in the history
  • Loading branch information
g-goulis committed Apr 30, 2024
1 parent baa5ce7 commit 6a8b5d8
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 26 deletions.
35 changes: 35 additions & 0 deletions src/main/java/edu/university/ecs/lab/common/models/Annotation.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
38 changes: 23 additions & 15 deletions src/main/java/edu/university/ecs/lab/common/models/JClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public class JClass implements JsonSerializable {
@SerializedName("variables")
protected List<Field> fields;

/** Class level annotations **/
protected List<Annotation> annotations;

protected List<MethodCall> methodCalls;

/** The associated microservice object for this class */
Expand All @@ -50,16 +53,20 @@ public JClass(String className,
ClassRole classRole,
List<Method> methods,
List<Field> fields,
List<Annotation> annotations,
List<MethodCall> 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);

}

/**
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public JController(JClass jClass) {
jClass.getClassRole(),
jClass.getMethods(),
jClass.getFields(),
jClass.getAnnotations(),
jClass.getMethodCalls(),
jClass.getMsId()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public JService(@NonNull JClass jClass) {
jClass.getClassRole(),
jClass.getMethods(),
jClass.getFields(),
jClass.getAnnotations(),
jClass.getMethodCalls(),
jClass.getMsId()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,6 +29,9 @@ public class Method implements JsonSerializable {

protected String returnType;

/** Method definition level annotations **/
protected List<Annotation> annotations;

@Override
public JsonObject toJsonObject() {
return createBuilder().build();
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

Expand Down Expand Up @@ -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<RestCall> parseRestCalls(File sourceFile, String msId) throws IOException {
public static List<RestCall> parseRestCalls(CompilationUnit cu, String msId) throws IOException {
List<RestCall> restCalls = new ArrayList<>();
CompilationUnit cu = StaticJavaParser.parse(sourceFile);

// loop through class declarations
for (ClassOrInterfaceDeclaration cid : cu.findAll(ClassOrInterfaceDeclaration.class)) {
Expand Down Expand Up @@ -272,8 +275,7 @@ public static List<RestCall> parseRestCalls(File sourceFile, String msId) throws
return restCalls;
}

public static List<MethodCall> parseMethodCalls(File sourceFile, String msId) throws IOException {
CompilationUnit cu = StaticJavaParser.parse(sourceFile);
public static List<MethodCall> parseMethodCalls(CompilationUnit cu, String msId) throws IOException {
List<MethodCall> methodCalls = new ArrayList<>();

// loop through class declarations
Expand Down Expand Up @@ -306,11 +308,9 @@ public static List<MethodCall> parseMethodCalls(File sourceFile, String msId) th
return methodCalls;
}

private static List<Field> parseFields(File sourceFile) throws IOException {
private static List<Field> parseFields(CompilationUnit cu) throws IOException {
List<Field> 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)) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -447,4 +448,34 @@ private static String formatURL(StringLiteralExpr stringLiteralExpr) {

return str;
}

private static List<Annotation> parseAnnotations(Optional<ClassOrInterfaceDeclaration> cid) {
if(cid.isEmpty()) {
return new ArrayList<>();
}

return parseAnnotations(cid.get().getAnnotations());
}

private static List<Annotation> parseAnnotations(NodeList<AnnotationExpr> annotationExprs) {
List<Annotation> 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;
}
}

0 comments on commit 6a8b5d8

Please sign in to comment.