This library is composed of five main components (see Implementation).
Here we show some examples about how to use them.
We start by describing how to use the Introspector, Intercessor and Tester classes, which can be used in an isolated way.
Then we explain how an implementation can exploit the whole functionality of the library.
This is done by using the Assessor and CodeAssess classes.
To do so, we will base our examples on the assessment of a source code that must satisfy the following criteria:
Criterion 1: Class Student only has one field and it is of type int: 3 points
Criterion 2: Class Student only implements one interface (Person): 3 points
Criterion 3: Class PhDStudent extends class Student: 4 points
Introspector
The Introspector class is used to obtain or check syntactical properties of the classes that are loaded using an internal class loader.
It is formed by 171 public methods, each of which uses Java Reflexion to access to the information of the classes.
import codeassess.javassessment.Introspector;
public class Example1 {
public double assess() {
String[] projectPaths = { "/path/project/" };
Introspector introspector = new Introspector(projectPaths);
Class> studentClass = introspector.getClass("Student");
Class> phdStudentClass = introspector.getClass("PhDStudent");
Class> personClass = introspector.getClass("Person");
// Criterion 1
boolean oneField = introspector.getDeclaredFieldCount(studentClass) == 1;
Field[] fields = introspector.getDeclaredFields(studentClass);
boolean isFieldInt = oneField && introspector.checkClass(fields[0], int.class, true);
// Criterion 2
boolean oneInterface = introspector.getInterfaceCount(studentClass) == 1;
boolean implementsPerson = introspector.checkImplements(studentClass, personClass);
// Criterion 3
boolean extendsStudent = introspector.checkSuperclass(phdStudentClass, studentClass);
// Mark
double mark = 0;
if (oneField) mark += 1.5;
if (isFieldInt) mark += 1.5;
if (oneInterface && implementsPerson) mark += 3;
if (extendsStudent) mark += 4;
return mark;
}
}
Intercessor
The Intercessor class is used to modify the source code of the classes that are loaded using an internal class loader.
It is formed by 40 public methods, each of which uses the JavaParser library to analyse and modify the source code.
The Tester class is used to execute the test cases that are stored in one Java class.
It is formed by 7 public methods that can be used to either execute all the test cases inside the class, or only execute a specific function of the class with specific arguments.
public TestCases {
public boolean testStudCardId(int studCardId) {
Student student = new Student(studCardId);
return student.getStudCardId() == studCardId;
}
public boolean testScholarship(int scholarship) {
Student student = new Student(scholarship);
return student.getScholarship() == scholarship;
}
}
The JavAssess library is implemented in such a way that the programmer can implement an assessment template (from here on just a Template).
In the Template class the programmer specify the criteria that the source code will be assessed with.
Finally, the CodeAssess class allows for executing the methods that are implemented in the Template.
Assessor
A Template can be easilly implemented by extending the Assessor class.
This class automatically provides the Introspector, Intercessor and Tester classes, so a Template can directly invoke their methods.
import codeassess.javassessment.Assessor;
public Template extends Assessor {
public List
CodeAssess
The CodeAssess class is used to assess a given program by using an Assessor object.
CodeAssess just automates the assessment by executing the appropriate methods of the assessor.
CodeAssess internally uses a class loader to load the classes of the project.
When these classes change, the refresh method can be used to update the current classes to reference the actual ones.