1. Summary

  • Maven

  • Exceptions

  • Classes with Self References

  • Classes with Mutual References

Work through the lab sections in the order that they appear. The lab is designed to provide extra questions for you to practise and as such you are not expected to finish the lab during our lab session. We recommend that you do eventually complete the lab on your own time.

2. Maven

Maven is a tool that allows us to manage our source code in order to [1]

  • compile our code

  • compile our test code

  • run our test code

  • analyze our source code

  • generate reports about our source code

  • package our software

  • deploy/share our software

All code created as part of this class must use maven and the maven configuration covered in this lab

2.1. Install Maven

2.1.1. Through your IDE

The latest versions of IntelliJ and Eclipse come with Maven as part of the IDE so you do not have to install anything if you are going to use Maven from within your IDE.

2.1.2. Through your command line

If you would like to use Maven on the command line you will have to install it. Follow the instructions on the Maven site.

  1. Download. Make sure you download version 3.3.9 or higher.

  2. Install.

  3. Run.

2.2. Maven with IntelliJ

IntelliJ has a specific project type for Maven projects.

  1. Clone your lab3 repo in a location on your computer. We will use this location later when we setup our project.

  2. Start IntelliJ and create a new project. see Lab1 for instructions.

  3. In the New Project selection window, in the left hand pane, instead of selecting Java select Maven.

  4. Once you select Maven in the left hand pane the right hand pane populates with archetypes.

  5. Go to the top of the right hand pane and check the box with the title Crete from archetype.

  6. Then click on the button Add Archetype…​. We have created a custom archetype for CS5004 that we need you to import into IntelliJ. We will import the CS5004 archetype now and you will have it available for the rest of the semester. As soon as you click the button the Add Archetype window appears. Copy paste the following information in each box in this order

    1. GroupId : edu.neu.ccs.cs5004

    2. ArtifactId : assignment

    3. Version : 1.0

    4. Repository (optional) : https://raw.github.com/therapon/cs5004/mvn-repo

  7. Click OK. This will add the class archetype into list of archetypes known to IntelliJ [2]

  8. Find the newly added archetype in IntelliJ’s the list of archetypes. The name of the archetype should be edu.neu.ccs.cs5004:assignment.

  9. Select it with your mouse and click Next

  10. In the next window we get to define the group and artifactId for your project will in the following information

    1. GroupId : edu.neu.ccs.cs5004. This is the value is the same for all projects for this class.

    2. ArtifactId : lab3. This is the value that you can alter for each assignment and lab. So if you are creating your assingment 3 project you can write in this box assignment3. We are doing this for lab3 so we named it lab3

    3. Leave Version intact.

  11. Click Next

  12. The next window will show all the information set up to this point. Nothing to do here so click Next

  13. The next window is something you have seen before. Here you can give

    1. A name to the IntelliJ project. This can be any name that you prefer.

    2. Set the folder that IntelliJ will use to store all files in this IntelliJ project. This should point to the folder on your computer where you cloned your repo.

  14. Click Next. IntelliJ will open the new project and will download all the configurations that we have set in place for CS5004. IntelliJ will open the file called pom.xml. While IntelliJ is downloading the CS5004 config the contents of this file will start getting updated. You will also see messages in IntelliJ’s console window. Do not click or type anything until you see similar lines to the ones below at the bottom of your IntelliJ’s console window.

    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 2.174s
    [INFO] Finished at: Thu May 19 14:42:47 PDT 2016
    [INFO] Final Memory: 10M/245M
    [INFO] ------------------------------------------------------------------------
    [INFO] Maven execution finished

    Once you see these lines in your IntelliJ’s console window, IntelliJ and Maven are setup and configured based on our class' settings.

  15. Go to your Project Explorer window and expand the node with your IntelliJ’s project name.

  16. Expand the folder named src.

  17. You should see two subfolders.

    1. main this is the folder that you must use to store all your Java sources. If you expand the folder named main you will see a folder structure that maps to the groupId that we entered as part of the project creation steps. The folder structure follows the Java conventions for Java packages. You will be creating more nested packages under this location for your labs and assignments.

    2. test this is the folder that you must use to store all your JUnit Test classes. If you expand the folder named test you will see the same folder structure as the one in main. Maven allows us to separate our tests from our code while still keeping test classes in the same Java package as our Java classes.

  18. We need to tell IntelliJ which folders contain Java source code.

  19. We need to mark the folder named java under main as the Sources Root

    1. Navigate to the folder java under the folder main

    2. Select the folder named java.

    3. Right click on the folder named java and from the pop-up menu select Mark Directory As → Sources Root

    4. Notice that the color of the folder in your Package Explorer Window is now blue.

  20. We need to mark the folder named java under test as the Test Sources Root

    1. Navigate to the folder java under the folder test

    2. Select the folder named java.

    3. Right click on the folder named java and from the pop-up menu select Mark Directory As → Test Sources Root

    4. Notice that the color of the folder in your Package Explorer Window is now green.

  21. IntelliJ has a dedicated window that allows us to manage and use Maven. On IntelliJ’s main menu, oo to View → Tool Windows → Maven Projects. This should pop-up the dedicated Maven window located in the top right corner of your IntelliJ window.

    1. NOTE when you minimize the Maven Window IntelliJ places a vertical tab in the right margin of the IntelliJ window with the tab title Maven Projects

    2. Use your mouse to click on the vertical tab named Maven Projects. The Maven Projects window should maximize.

  22. The menu of the Maven Projects window has the same icon we use to run our tests; a green "play button". Click it. This starts the configured build process and will take some time to complete. The configured build process causes maven to

    1. Build your code

    2. Build your tests

    3. Run your tests

    4. Run our reports

    5. Generate HTML reports including

      1. Javadocs

      2. Code coverage reports

      3. Checkstyle reports

      4. Findbug reports

      5. PMD reports

        Pay attention to IntelliJ’s console window. You will see a lot of messages and information. The process should end with lines similar to the ones below

        [INFO] ------------------------------------------------------------------------
        [INFO] BUILD SUCCESS
        [INFO] ------------------------------------------------------------------------
        [INFO] Total time: 18.778s
        [INFO] Finished at: Thu May 19 14:55:56 PDT 2016
        [INFO] Final Memory: 56M/776M
        [INFO] ------------------------------------------------------------------------
        
        Process finished with exit code 0

        This indicates that the build was Successful.

  23. Go back to your Project Explorer window. You should be able to see a new folder named target. Maven places all build output inside this folder. [3]

  24. Expand the folder named target, and then expand the sub-folder named site.

  25. Inside the folder named site you should see a file named index.html. Use your mouse to right-click on index.html and select Open in Browser → Default to open this file in your web browser. [4]

  26. Your web browser should open and render the file index.html. The web page should have a menu in on the left hand side of the window. Locate the link titled Project Reports and click on it.

  27. You should see the following links

    1. JaCoCo Tests. This reports contains your tests' code coverage.

    2. Source Xref. This reports contains all your source code as HTML files with links.

    3. Checkstyle. This report contains violations of the Google Java Style guide found in your source.

    4. FindbBugs. This report contains violations to other rules that we have setup for you.

    5. JavaDocs. This is all your generated Javadocs.

  28. There is one more report, the PMD report, that will appear once we add more code to our project.

3. Maven reports.

  1. Create a package under your lab3 IntelliJ project with the name edu.neu.ccs.cs5004.lab3.shapes

  2. Bring in the source code for Shapes that we developed in Lecture 2 and place it under your new package. Do not bring in any Test code.

  3. Make sure you fix any errors that IntelliJ detects.

  4. Run maven to build your code and generate all of its reports.

  5. Open the index.html file to inspect all of your reports.

  6. Click on Project Reports

3.1. JaCoCo Test

This report shows your test code coverage. Since we have no tests, your code coverage should be 0.

Practise Exercises
  • 1) Create a corresponding package with the same name under your project’s test folder and add tests for Posn. Re-generate your maven reports by running maven again and check to see how your code coverage has changed.

  • 2) Add enough tests for your Posn class so that your code coverage is 80%.

3.2. Source Xref

This report shows your code as HTML with links embedded so you can browse your code using the Web Browser.

3.3. Test Source Xref

Similar to Source Xref but for your JUnit code.

3.4. Checkstyle

This report shows any violations that you might have based on the Google Java Codestyle. The code should have some violations already.

  1. Click to see the report.

  2. You should see the following sections

    1. Summary, contains the files and types of violations as a summary

    2. Files, contains links to each source file that we have violations

    3. Rules, contains the codestyle rules that are violated

    4. Details, contains one sub-section for each file and lists the

      1. Severity, Category, Rule, Message and a link to the line number in the source file that the violation occurred

Practise Exercises
  • 3) Fix all checkstyle violations in the class AbstractShape

  • 4) Fix all checkstyle violations in your project. You may skip this step during the lab session

3.5. FindBugs

This report shows any violations that have to do with, style, performance, security and possible bugs. Our Shapes code should not generate any FindBugs violations. In case that your code does generate FindBugs violations you should address those. Assignments will explicitly tell you which FindBugs violations you should address and which FindBugs violations you can safely ignore.

3.6. PMD

This is another report that checks for style violations, design issues and other possible bugs. You may see some violations for our Shapes project. It will depend on the issues you will have already fixed in your checkstyle report.

4. Exceptions

In our last lecture we talked about Exceptions in Java and we wrote one for Circle.

Practise Exercises
  • 5) Update Square so that the value provided for the length of the side of the square is not 0 or less than 0.

  • 6) Update Rectangle so that the value provided for the length of the width or height of the rectangle is not 0 or less than 0.

  • 7) Write tests to show that your implementation appropriately deals with cases where the values provided for side, width or height work as expected.

5. Recursive Data Definitions

Recall our implementation of Lists from last lecture.

Practise Exercises
  • 8) Create a new package in your project under the folder named main with the name edu.neu.ccs.cs5004.lab3.lists and place all the relevant code for lists inside this package.

  • 9) Create a corresponding package under the folder named test to add tests for lists.

  • 10) Implement the method contains(Integer element) on lists that returns true if element is in the list and false otherwise.

  • 11) Implement the method elementAt(Integer index) that returns the element found at index in the list. Your code should deal with the situations where the value of index provided is outside the bounds of your list.

5.1. List of Posn

We would like to design a list whose elements are Posn 's and provide some operations on this list that are specific to Posn 's.

Practise Exercises
  • 12) Provide your UML Class diagram for a list of Posn.

  • 13) Design the following methods for the list of Posn

    • size() returns the number of Posn elements in the list.

    • add(Posn element) adds element at the end of the list.

    • draw the Sequence Diagram for your implementation of the add method. Use an example of a list of Posn that contains at least 3 elements to which we add a 4th element using the add method.

    • empty() returns true if the list is empty, false otherwise

    • contains(Posn element) returns true if element is found in the list, false otherwise

    • remove(Posn element) returns an updated list with element removed. If the list does not contain element return the list intact.

    • filterBelow(Posn element) returns a list that contains all the element whose y coordinate is less than the y coordinate of element

    • filterAbove(Posn element) returns a list that contains all the element whose y coordinate is greater than the y coordinate of element

    • filterLeft(Posn element) returns a list that contains all the element whose x coordinate is less than the x coordinate of element

    • filterRight(Posn element) returns a list that contains all the element whose x coordinate is greater than the x coordinate of element

    • design a method on lists of Posn that consumes another list of Posn and returns true if the two lists contain the same elements regardless of their order in the list, return false otherwise. You may assume that the two lists have no duplicate elements.

    • design a method on lists of Posn that consumes another list of Posn called otherList and removes all elements from the original list that are also in otherList. You may assume that the two lists have no duplicate elements.

6. IntelliJ tips

6.1. Indentation

You can configure IntelliJ in order to use 2 spaces instead of 4 for each indentation level

Go to Preferences → Code Style → Java. Make sure the following items are set to these numbers

  • Tab size : 2

  • Indent : 2

  • Continuation Indent : 4

6.2. Adding curly braces to all if blocks

Use one of the methods described in the IntelliJ documentation to configure your code inspections.

  1. When the inspection configuration menu pops up locate the search box, the text box with an icon of a magnifying glass.

  2. Type the following in the search box Control flow statement without braces. It should filter all inspections and leave you with the one named Control flow statement without braces.

  3. Select the checkbox at the right of the text Control flow statement without braces that appears below the search box.

  4. Click OK

  5. Go back to your editor window. In the main IntelliJ menu select Analyze → Inspect Code. This will run all IntelliJ inspections and report issues.

  6. The issues from your inspection will pop-up a window at the bottom of the IntelliJ window.

  7. Expand Code style Issues

  8. Double click on the issue with the text 'if' without braces. IntelliJ will open the line that violates the rule in your editor so that you can see it.

  9. Right click on 'if' without braces and select Add Braces

    1. Some times instead of Add Braces it will say something close to Apply fix "Add Braces"

6.3. Renaming fields, classes, variables, arguments etc.

6.4. More Videos from IntelliJ


1. There are alternatives to Maven, i.e, Gradle, and you will generally have to use a build automation tool when working on projects.
2. Instructions to remove an archetype from IntelliJ’s list of known archetypes. It appears that IntelliJ does not provide a way to delete an archetype from the list using their UI.
3. Do not add the target folder to your repos.
4. If IntelliJ does not properly show the reports, use your Web Browser’s File → Open menu and locate the file index.html on your computer to open it manually.