sonar plugins and why i had to fork the sonar web plugin to add a rule

Last weekend, a few of the CodeRanch moderators were discussing a Trac item that we’d like to get rid of the I18N messages from our HTML. It’s there at all because open source JForum had it. We haven’t been updating it and have been using it inconsistently. Also, we do want to externalize the ones in Java which is why we can’t just get rid of the file wholesale – someone could add more references from HTML. I suggested that this would be a good thing to set up a SonarQube rule and volunteered to create the rule. This was a far harder and more interesting task than I expected it to be when I said I’d do it.
webProps

The web plugin scans HTML files and has a bunch of pre-defined rules and templates for them. You can set any file extensions you’d like so I had set “.html,.html,.ftl” in Administration > Configuration > Web to hit both our HTML and Freemarker code. Alternatively, you can set this in your Sonar properties file.

Looking through the rules and rule templates in the web plugin, there wasn’t one for what I wanted – plain String or regular expression search. Nuts! There are a few existing rules that do what I want, however I couldn’t use them:

Rule Why can’t use
Some Java packages or classes should not be used in JSP files In web plugin, but only for jsp/jsp files
Disallowed methods should not be allowed In Java plugin so only works for Java code
XPath rule Only works for XML files
The text plugin regular expression rule So close! This does exactly what I need. Except (see next section)

Great, the text plugin meets my needs. But alas. You can only associate a file extension with one plugin for each Sonar run. The HTML/Freemarker files are already considered web files.

So what’s wrong with the text plugin?

The text plugin has a rule called “Simple Regex Match”. When configured with the proper regular expression, it does exactly what I want. Except for one huge problem. In order to use it, I have to associate the text plugin with HTML/Freemarker files. This is a two step procedure:

  • In the browser, Administration > Configuration > Web – set the web extensions to “foo”. Leaving it blank is insufficient as it defaults to include HTML and Sonar does not let the same file extension be associated with multiple plugins.
  • In sonar-project.properties, add the line: sonar-text-plugin.file.suffixes=.htm,.html,.ftl

You also have to run sonar-runner twice. Once with the HTML/Freemarker files associated with the web plugin to get those results and again with them associate with the text plugin to get the rule I’m trying to write. This means you need to have the code show up as two projects within SonarQube. (otherwise Sonar will tell you the rule violations were fixed for whichever analysis you run last.)

Extending the web plugin

Ok. I clearly need to extend the web plugin so that I can have a regular expression rule associated with the web plugin. The docs say the web plugin doesn’t support extending it with Java or XPath rules as does this post. I tried anyway hoping this was out of date. I was able to get my new rule to show up in Sonar, but not to run. Everything I’d need to do in order to get it to run was considered a duplicate and prevented Sonar from even starting up. For example, “org.sonar.api.utils.SonarException: Can not add the same measure twice” or “duplicate keys not allowed”. So I concede this path is a non-starter. It would have been nice to have my custom rule in a separate Maven project rather than having to fork the existing one.

Forking the web plugin

Sigh. Last resort. Time to fork. This newsgroup post pointed to a commit that shows how to add a rule to the web plugin. I wanted to add a rule template rather than a rule, but the idea is the same. It wasn’t hard to make the change within the project. It’s just that forking isn’t good in the long run as I’ll have to keep re-merging. What I needed to change in my fork:

  1. Add custom rule template class to org.sonar.plugins.web.checks.coding package. The code was pretty similar to the rule for illegal spaces:
    package org.sonar.plugins.web.checks.coding;
    
    import java.io.IOException;
    import java.nio.charset.Charset;
    import java.util.List;
    
    import org.sonar.api.utils.SonarException;
    import org.sonar.check.Priority;
    import org.sonar.check.Rule;
    import org.sonar.check.RuleProperty;
    import org.sonar.plugins.web.checks.AbstractPageCheck;
    import org.sonar.plugins.web.checks.RuleTags;
    import org.sonar.plugins.web.node.Node;
    import org.sonar.plugins.web.visitor.CharsetAwareVisitor;
    import org.sonar.squidbridge.annotations.NoSqale;
    import org.sonar.squidbridge.annotations.RuleTemplate;
    
    import com.google.common.io.Files;
    
    @Rule(
      key = "RegularExpressionNotAllowedOnLineCheck", 
      name = "Regular expression \"regex\" not allowed on web page", 
      priority = Priority.MAJOR, tags = {
      RuleTags.CONVENTION })
    @RuleTemplate
    @NoSqale
    public final class RegularExpressionNotAllowedOnLineCheck extends AbstractPageCheck implements CharsetAwareVisitor {
    
    	@RuleProperty(
          key = "regex", 
          description = "Single line regular expression to prohibit on web pages")
    	public String regex = "";
    	
    	private Charset charset;
    
    	@Override
    	public void setCharset(Charset charset) {
    	   this.charset = charset;
    	}
    
    	@Override
    	public void startDocument(List<Node> nodes) {
    		List<String> lines;
    		try {
    			lines = Files.readLines(getWebSourceCode().inputFile().file(), charset);
    		} catch (IOException e) {
    			throw new SonarException(e);
    		}
    		for (int i = 0; i < lines.size(); i++) {
    			// only support matching text within a single line to facilitate identifying line number
    			if (lines.get(i).matches("^.*" + regex + ".*$")) {
    				createViolation(i + 1, "Replace all instances of regular expression: " + regex);
    			}
    		}
    	}
    
    }
    
  2. Add the rule template to org.sonar.plugins.web.rules.CheckClasses so the web plugin knows about.
  3. Create a new HTML file in the resources localization directory.
  4. Write/update tests. I like that the sonar-web-plugin project uses JUnit tests to enforce some of there conventions like rules ending in “Check”.

Where is the code?

I’ve placed my fork on Github. It’s 2.5-SNAPSHOT because I didn’t see any need to version it. I also created a pull request for the sonar-web-plugin in hopes they are interested in merging it. The plugin was last released a year ago so I imagine I’ll be using my fork for along the foreseeable future in any case. Which is fine. CodeRanch is a volunteer run website so no rules against using forked software.

java 8 jshell mock questions

I gave a presentation showing how to make mock exam questions. We used Java 9 JShell as the example. The idea was to pick a feature nobody knew much about. We started by picking key points from this blog. Then we made questions. This post is the results. (Edited for formatting, to alphabetize answers, after confirming, etc). Good job all!

Question 1

What does REPL stand for?

  1. Random Enterprise Process Logic
  2. Rapid Enterprise Process Line
  3. Rapid Execution of Programming Logic
  4. Read Evaluate Print Loop
  5. Read Evaluate Print Line
  6. Read Execute Print Loop

Question 2

When typing into JShell interactively, it uses ______.

  1. Nashorn
  2. JRE
  3. JS
  4. JVM
  5. REPL
  6. RPEL
  7. Streams

Question 3

Which of the following compiles when typed into JShell? (Choose all that apply)

  1. Class.forName(“String.class”)
  2. Class.forName(“String.class”);
  3. Thread.sleep(1000)
  4. Thread.sleep(1000);
  5. System.out.println(“hello”)
  6. System.out.println(“hello”);

Question 4

Which of the following compiles when typed into JShell? (Choose all that apply)

  1. void method() { throw new Exception(); }
  2. void method() { throw new RuntimeException(); }
  3. void method() throw Exception { throw new Exception(); }
  4. void method() throw Exception { throws new Exception(); }
  5. void method() throws Exception { throw new Exception(); }
  6. void method() throw Exception { throws new Exception(); }

Question 5

Which of the following compiles when typed into JShell? (Choose all that apply)

  1. System.out.println(“Hello Java”)
  2. System.out.println(“Hello Java”);
  3. public void method() { throw new Exception(); }
  4. public void method() { throw new Exception(); };
  5. public void method() {System.out.println(“Hello Java”)}
  6. public void method() {System.out.println(“Hello Java”);}
  7. public void method() {System.out.println(“Hello Java”);};

Question 6

What are valid statement terminators in JShell? (Choose all that apply)

  1. ;
  2. :
  3. \n
  4. None (just press enter)
  5. None of the above

Question 7

Which of the following lines will compile and display a value if it’s the first line you type after launching JShell? (Choose all that apply)

  1. 5 + 6
  2. a + b
  3. System.out.println(“Hello”);
  4. System.out.println(“Hello”)
  5. out.println(“Hello”)
  6. out.println(“Hello”);
  7. { System.out.println(7) }
  8. { System.out.println(7); }
  9. 6 + 6; 10
  10. None of the above

Question 8

Which of the following is valid code in JShell? (Choose all that apply)

  1. system.out.println(“hello world”);
  2. system.out.println(hello world);
  3. Println(“hello world”);
  4. “hello word”
  5. hello world
  6. Answer: 5+3
  7. 5f+3
  8. new String()

Question 9

What is the output of the code below if executed in JShell?(Check all that apply)

Integer a = 5;

Integer c = a + 10;

Integer a = 7

a

  1. Compiler Error
  2. Runtime error
  3. 7
  4. Error: Duplicate variable declaration
  5. None of the above

Question 10

Which of the following commands invokes the help command in Jshell?  Choose all that apply.

  1. /h
  2. /help
  3. /?
  4. ?
  5. f1
  6. ctrl-f1
  7. help

Question 11

How would you invoke help on a particular Jshell command?  Choose all that apply.

  1. /help=command
  2. /help:command
  3. /help command
  4. /he command
  5. /h command

Question 12

double area (double radius) {

return 3.14 * square(radius);

}

area(2)

What is the correct output?

  1. 6.28
  2. 6
  3. 7
  4. Attempted to call area(double) which cannot be invoked
  5. Attempted to call square which cannot be invoked

Answers

  1. Choice 4 – Read Evaluate Print Loop
  2. Choice 5 – REPL
  3. All 6 choices are correct. The first two throw an exception, but do compile.  (This question had public in the user group lab. I wrote this question and removed it after)
  4. Choices 2 and 5. The semicolon is only optional at the end of the statement; not within a block
  5. Choices 1, 2, 6 and 7. The last two give a warning about public not being allowed, but work. The others fail because of an exception or missing semicolon within a method.
  6. Choices 1 and 4. Semicolon terminates a statement or you can omit the terminator
  7. Choices 1, 3, 4, 8 and 9. The last one is tricky. It gets executed as two statements.
  8. Choices 4, 7 and 8. JShell is case sensitive; just like Java
  9. Choice 3. (In the user group lab, there was another choice about a warning for a duplicate variable. I couldn’t reproduce the warning so removed that choice to avoid ambiguity.)
  10. Choices 2 and 3. Isn’t memorization fun?
  11. Choices 3 and 4. More memorizing
  12. Choice 4. Creating the method works, but gives a warning that square(double) must be declared. Then calling area gives “attempted to call method area(double) which cannot be invoked until method square(double) is declared”

 

 

 

using docker on mac to run java 9

Last week, I was trying to install Java 9 when I messed up my Mac and had to recover using Time Machine. I didn’t have difficulty using the Java 8 preview. Anyway, I realized I should be using a VM for Java 9 so this doesn’t happen again.

Conveniently, I was reading a book about Docker on the plane when I was traveling right after that. Which meant I had a use case for installing Docker on my home machine. It was easy and I went from not having Docker installed to being able to use it for Java 9 in under 30 minutes.

Installing

Since Mac isn’t Linux, the first step is to install the Docker Toolbox for OS X. This uses half a gig of disk space. Which is fair since it is running a VM.  Overall, the Docker getting started docs for Mac are excellent. It took a few minutes the first time I ran the Docker Quickstart Client as I saw:

Creating CA: /Users/nyjeanne/.docker/machine/certs/ca.pem
Creating client certificate: /Users/nyjeanne/.docker/machine/certs/cert.pem
Running pre-create checks...
Creating machine...
(default) Copying /Users/nyjeanne/.docker/machine/cache/boot2docker.iso to /Users/nyjeanne/.docker/machine/machines/default/boot2docker.iso...
(default) Creating VirtualBox VM...
(default) Creating SSH key...
(default) Starting the VM...
(default) Check network to re-create if needed...
(default) Found a new host-only adapter: "vboxnet1"
(default) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: /usr/local/bin/docker-machine env default
##         .
## ## ##        ==
## ## ## ## ##    ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
\______ o           __/
\    \         __/
\____\_______/
docker is configured to use the default machine with IP 192.168.99.100
For help getting started, check out the docs at https://docs.docker.com

Initial test

I ran:

docker run hello-world

It worked as expected. Pretty easy.

Java 9 beta container

Finding the relevant library on DockerHub was pretty easy. There is an official Java library.

First I accidentally ran docker with only the tag:

docker run openjdk-9

It probably isn’t a surprise that I got an error. It just wasn’t the error I expected:

docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.

Luckily, a github issue helped. I needed to set the environment variables. The issue said to run:

eval $(docker-machine env default)

I ran the docker-machine command first so I knew what I was evaluating. It was harmless

export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/nyjeanne/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell:
# eval $(docker-machine env default)

Then I got the message I was expecting:

Unable to find image 'openjdk-9:latest' locally
Pulling repository docker.io/library/openjdk-9
docker: Error: image library/openjdk-9 not found.
See 'docker run --help'.

Running the command correctly was far more productive:

docker run java:openjdk-9

It started downloading the container. While it was downloading, it looked like:

52e20300f877: Downloading 20.54 MB/52.71 MB
a3ed95caeb02: Download complete
fb4c7723b752: Download complete
45c1fd7a2514: Download complete
d12ad3d1f060: Download complete
80f5aeb42fe5: Download complete
a6fac263fca7: Download complete
8cb87a5d8c56: Downloading 15.12 MB/215.8 MB

And when it was done, I had:

Unable to find image 'java:openjdk-9' locally
openjdk-9: Pulling from library/java
52e20300f877: Pull complete
a3ed95caeb02: Pull complete
fb4c7723b752: Pull complete
45c1fd7a2514: Pull complete
d12ad3d1f060: Pull complete
80f5aeb42fe5: Pull complete
a6fac263fca7: Pull complete
8cb87a5d8c56: Pull complete
Digest: sha256:49a691b3e64950a45a45dbac6fde08636bded81acf4a36807d12208d3d3af293
Status: Downloaded newer image for java:openjdk-9

Then I ran it interactive mode so I could test jshell:

docker run -i java:openjdk-9

By default, there is no prompt, but you can type unix commands. I typed

jshell

To get out of jshell, you type /exit and to get of the container, you type exit.

End to end success

This is pretty easy when you aren’t doing it wrong:

nyjeanne$ docker run -i java:openjdk-9
javac -version
javac 9-internal
jshell
May 02, 2016 12:17:02 AM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.
|  Welcome to JShell -- Version 9-internal
|  For an introduction type: /help intro
jshell> 1+2
1+2
$1 ==> 3
jshell> new File("abc")
new File("abc")
$2 ==> abc
 
jshell> java.util.stream.Stream.of(11, 2, 16).sorted().findFirst()
java.util.stream.Stream.of(11, 2, 16).sorted().findFirst()
$3 ==> Optional[2]
 
jshell> /exit
/exit
|  Goodbye
exit

Initial impressions of JShell

Granted it isn’t released yet, but I’m not more impressed with JShell than I was with Nashorn’s jjs. There’s still no tab autocompleted or up arrow support. And while there are a couple imports included by default, NIO and Streams packages are not in that list.