The long way through Software Craftsmanship

Multiple return values in a Mockito Stub

Aug 7, 2015 - 1 minute read - Comments - mockitotipstubjava

I’ve been asked today how to return multiple return values from a Mockito Spy, effectively using the spy as a Stub, as well.

package com.example.spike;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import org.junit.Test;
import org.mockito.Spy;

public class DifferentReturnValues {

	@Spy
	private Spike1 spike1 = new Spike1();


	@Test
	public void spike1() {
		spike1 = spy(spike1);
		when(spike1.getBool()).thenReturn(false, true);

		assertThat(spike1.getBool(), is(false));
		assertThat(spike1.getBool(), is(true));

		assertThat(spike1.getBool(), is(true));
		assertThat(spike1.getBool(), is(true));
	}


	private class Spike1 {
		public boolean getBool() {
			return true;
		}
	}
}

The key line is:

when(spike1.getBool()).thenReturn(false, true);

this makes the stubbed function to return multiple values:

assertThat(spike1.getBool(), is(false));
assertThat(spike1.getBool(), is(true));

The last value is repeated after the last defined value:

@Test
public void spike1() {
	spike1 = spy(spike1);
	when(spike1.getBool()).thenReturn(false, true);

	assertThat(spike1.getBool(), is(false));
	assertThat(spike1.getBool(), is(true));

	assertThat(spike1.getBool(), is(true));
	assertThat(spike1.getBool(), is(true));
}

If you want to loop over the values, you can implement it with the doAnswer method:

@Test
public void spike1() {
	spike1 = spy(spike1);
	when(spike1.getBool()).thenReturn(false, true);

	final boolean[] value = {true};

	doAnswer(invocation -> {
		value[0] = !value[0];
		return value[0];
	}).when(spike1).getBool();

	assertThat(spike1.getBool(), is(false));
	assertThat(spike1.getBool(), is(true));

	assertThat(spike1.getBool(), is(false));
	assertThat(spike1.getBool(), is(true));
}

Brown-bag session and Dojo: Beginning Clojure

Aug 5, 2015 - 1 minute read - Comments - clientbrown-bag-sessiondojoclojurefacilitatorfacilitationclojure-for-java-developerreplguiderepository

At a client, today I’ve facilitated a brown-bag session introducing the Clojure language to a group of java programmers.

I’ve started hands-on, live coding on a REPL. To make things easier, I’ve created a maven project that imports the clojure jar and lets you obtain dependencies from clojars. The repo is here. This repo wouldn’t have been possible without the clojure maven plugin, written by Mark Derricutt.

I’ve more or less followed this guide

We’ve also practiced, using a dojo in clojure, the fizzbuzz kata.

There is a sample implementation in the same repo, this folder.

Self-Study in August 2015

Aug 2, 2015 - 12 minute read - Comments - 200720142015absence-of-bugsadvicealejandro-marcuanalysisanders-janmyrandraz-bajtarrayarticleaugustautobiographyawsbackground-mindblogbookbounded-contextbrett-wooldridgebroadcastby-examplebytecodecareercareer-changecarlos-blecarlos-villuendascheatsheetchristopher-maierclientclojureclojurescriptcmscompanycomparisoncompilercompletable-futurecompojurecomputer-scienceconcurrencyconsultancyconsultingcontinuous-deploymentcraig-larmandan-grossmandaniel-whittakerdatadave-ellimanddddesigndevopsdijkstraecuadoredoficedsger-dijkstraeric-normanderin-swenson-healeyessayewdexhaustive-testingexperience-samplingfeedbackffifixfizzbuzzflowframeworkfregefrungykingfunctorfuturegeorge-polyagerard-meszarosghcgistgithubgithub-pagesgoogle-developersguidehakyllhammock-driven-developmenthaskellhastehistory-of-programming-languages-conferencehotspothow-tohumble-programmerhypothesisihinfographicinformation-hidinginlineintroductionjaelle-scheuermanjames-cooperjan-stepienjava-8javascriptjeff-atwoodjoe-nelsonjoel-kaasinenjoey-hessjohn-cheejoseph-grennyjoy-of-clojurejustjustin-leitgebjustin-perryjvmkatakathy-phamkerodonlambdalambda-dayslatin-squarelazy-evaluationlearninglein-fregeclessonlightning-talklink-shortenerlisplong-waylovemacromanagementmanchestermaybemetametaprogrammingmicroservicemigrationmihaly-csikszentmihalyimonad-readermoocmooc-programming-languagesnamespacenate-wildermuthnear-exhaustive-testingnebojsa-stricevicnegative-feedbackneil-brownnietzschenon-blockingnon-strictnessnothingobjective-cocpopen-closedprincipleoptimizationoptionoptionalosconparallelismpassionpatternpaul-grahamperfectionismperformancephilosophyphppragmatic-programmerpreprocessorprivate-functionprivate-variableprofessionalprogrammingproject-managerprologprotected-variationpvquantificationquasi-quoterquotereactive-programmingreadreadingreplrereadrestrich-hickeyrobert-c-martinrobert-mandlroman-numeralrubyruby-on-railsrustryan-trinklesandro-mancusosantiago-valdarramascreencastsean-corfieldself-study-aggregationsharingsimon-peyton-jonessimonmarsimplicityslidesnapsocial-mediasolve-problemstack-buildersstrange-looptalktddtechnical-debttestingthoreauthoughtworkstime-lapsetomasz-nurkiewicztravis-ciubiquitous-languageuku-tahtvideowaking-mindweb-developmentweb-frameworkwebappwrite-onlywrocloverb

Humble Object

I’ve read the Humble Object list of patterns, by Gerard Meszaros, including:

  • humble dialog
  • humble executable
  • humble transaction controller
  • humble container adapter

Tags: pattern, gerard-meszaros

How you know

I’ve reread this small essay by Paul Graham on storing information in our heads, based on the context. On the importance of rereading, to add more contexts to the initial one.

Tags: paul-graham, reread, read

Protected Variation: The Importance of Being Closed

I’ve read this article by Craig Larman in the magazine IEEE Software > Design.

Discusses Information Hiding, Protected Variation and the Open-Closed Principle.

Tags: craig-larman, ocp, open-closed principle, design, information-hiding, ih, protected-variation, pv,

Lambda, Javascript Micro-Services on AWS

I’ve read this post explaining how to deploy a microservice on AWS on top of Lambda. Written by Anders Janmyr

The cool thing is that you get billed by milliseconds!

Tags: anders-janmyr, devops, microservice, lambda, aws

Migrating To Clojure. So Much Fn

I’ve watched this talk by Jan Stępień where he talks about his experience on migrating part of an application to clojure from ruby on rails

Tags: jan-stepien, clojure, migration, ruby-on-rails, talk, wrocloverb

Te recomiendo escribir en un blog

I’ve read this article from Carlos Blé in which he recommends writing a blog, on things we discover along the way. (Spanish)

Tags: carlos-ble, meta, blog, long-way

We need enough technical debt

I’ve read this article on why we need to accumulate a bit of technical debt, as a way of being less perfectionist and deliver more features / value to our stakeholders. Written by Santiago L. Valdarrama.

Tags: santiago-valdarrama, technical-debt, perfectionism

Escribe mejores tests en Javascript

I’ve read this list of tips to write better javascript tests (Spanish). By Carlos Villuendas

Tags: carlos-villuendas, javascript, test

Down the Rabbit Hole

I’ve read this wiki page on how to tune some aspects of the server hotspot JVM, by Brett Wooldridge

Tags: performance, jvm, hotspot, inline, bytecode, brett-wooldridge

XP Manchester Lightning Talks 2015 Live broadcast

I have watched the first two lightning talks from XP Manchester, edition number 61.

Tags: lightning-talk, broadcast, simplicity, manchester, 2015

Counting hash collisions with the birthday paradox

I’ve read this article on the birthday paradox, or the percentage of chance of people sharing the same birthday. This could be applied to hash function, as the title says, but is not included in the article. Written by Matt Might

Why I’m The Best Programmer In The World*

I’ve read this ironically-titled article by Jeff Atwood on humility and how to improve as a programmer.

Tags: jeff-atwood

The Key to Giving and Receiving Negative Feedback

I’ve read this article by Joseph Grenny about how to give and receive negative feedback. It is exemplified by the plant manager who was expecting another kind of feedback from his subordinates and was surprised by their negative feedback.

Tags: joseph-grenny, negative-feedback, feedback, by-example

Testing Clojure web applications with Kerodon

I’ve read this guide on how to test compojure-based applications with kerodon. Written by Nebojša Stričević

Tags: nebojsa-stricevic, compojure, kerodon, clojure, testing, guide

Using The Option Type Effectively

I’ve read this article on how to use the Option type (in rust), similar to Optional in java or Maybe in haskell. Written by Uku Taht.

Tags: uku-taht, option, maybe, optional, rust

When Should You Think?

I’ve read this post by Robert C Martin on when to think and the hypothesis that TDD does not allow you to think before coding.

Tags: tdd, robert-c-martin, design, hypothesis

Orthogonal latin squares: an application of experiment design to compiler testing

I’ve read this paper by Robert Mandl on techniques that can be applied to reduce the burden of exhaustive testing to compilers. Preferring “surely all relevant distinct combinations” we can reduce the amount of testing. This paper tries to formalize the concept of “near-exhaustive testing”

Tags: latin-square, robert-mandl, compiler, testing, near-exhaustive-testing, optimization, exhaustive-testing

##  Practical Type System Benefits

I’ve read this article by Neil Brown on benefits of the type system, with examples in haskell. He goes on to explore the benefits of involving the type-checker at compile time to remove type errors and ease the programming effort:

I find that several problems in Haskell can be solved by involving the type system further: The trick is to involve the type system in ways that provide a big benefit for the cost, but not going so far that the type system gets in the way of code that you know –but can’t easily prove to the compiler– is safe

I’ve also learned the concept of the quasi-quoter: similar to a preprocessor, executed at compile time and type-checked

Tags: neil-brown, quote, quasi-quoter, preprocessor, haskell, monad-reader

ClojureScript and the Blub Paradox

I’ve read this article comparing javascript and clojurescript using the fizzbuzz kata. There is a nice example of the pattern matching for the cases (i.e. “switch”) of the fizz, buzz. Written by Nate Wildermuth

Tags: clojurescript, javascript, fizzbuzz, kata, nate-wildermuth

No malinterpretes tu carrera

I’ve read this article on (not) changing careers from programmer to designer, analyst, team manager. About the programmer’s passion and why not to misinterpret your career. Written by Carlos Blé. In Spanish.

Tags: carlos-ble, career, programming, management, career-change

Just Haskell or Nothing

I’ve read these slides on packaging values in (Just a) or Nothing for possibly failed computations in haskell. Written by John Chee

Tags: john-chee, haskell, just, nothing, maybe, functor

Tracking Joy at Work

I’ve read this article about quantifying happiness at the office (or at work). Joe Nelson, the author of the article, uses the quantifying method described int the book Experience Sampling Method (wikipedia page) by Hektner, Schmidt, Csikszentmihalyi.

The last one, not coincidentally is the author of Flow: The Psychology of Optimal Experience.

Tags: mihaly-csikszentmihalyi, flow, quantification, data, experience-sampling, article, joe-nelson

Going “Write-Only”

I’ve read this article by Joe Nelson on going “write-only” on social media, blogs, news, etc, as opposed to read-only (only consuming media) or read-write (consuming and producing). He cites another programmer, Joey Hess and about the quality and durability of your work.

Tags: article, joe-nelson, write-only, social-media, reading, philosophy, thoreau, nietzsche, joey-hess

Create a static site with Hakyll, Github and Travis CI

I’ve read this article on how to create your blog with hakyll and travis ci. Written by Joe Nelson

Travis ci is used to do continuous integration and automatically deploy each commit to production (github pages).

Tags: hakyll, haskell, joe-nelson, meta, blog, continuous-deployment, github, github-pages, travis-ci

Falling in Love with Computer Science

I’ve watched this video by Kathy Pham, an American that discovered her passion, her love to computer science a few years ago. She tells the events around computer science from the beginning of college to the current moment.

Tags: kathy-pham, passion, love, computer-science, autobiography, time-lapse, google-developers

Duck Typing

I’ve watched this video by Dan Grossman, part of the “Programming Languages” MOOC, in which he explains what is a Duck Type and how to use (and abuse) them in ruby.

Tags: ruby, dan-grossman, mooc, mooc-programming-languages

Not-So-Private Clojure Functions

I’ve read this article explaining how to bypass private functions in clojure: either create a lambda or create a different namespace. Written by Christopher Maier

Tags: private-variable, clojure, testing, joy-of-clojure, christopher-maier, private-function, namespace

Frege (and Clojure)

I’ve read this article that tells the Pragmatic Programmer’s advice to learn a new language every year and the author (Sean Corfield) explains his trajectory learning them. He also cites the Lein plugin he’s built: lein-fregec and how to connect clojure and frege (a.k.a JVM’s haskell).

Tags: haskell, frege, jvm, clojure, ffi, pragmatic-programmer, advice, lein-fregec, sean-corfield, comparison

Two months early. 300k under budget

I’ve read the tale of a Thoughtworks Project Manager who convinced his client to start using clojure to deliver a custom-made CMS. Written by Dave Elliman.

Tags: dave-elliman, thoughtworks, project-manager, clojure, cms, microservice

Convince your boss to use Clojure

I’ve read this guide by Eric Normand on how to convince your boss or your company to start using clojure.

Tags: company, clojure, eric-normand, how-to, guide

A brief introduction to Prolog

I’ve read these slides about a brief introduction to prolog and how to do web development in it. By Jaelle Scheuerman

Tags: jaelle-scheuerman, prolog, web-development, rest, slide

Arrayzing - The JavaScript array cheatsheet

I’ve read this cheatsheet for javascript arrays. Written by Justin Perry

Tags: cheatsheet, array, javascript, gist, justin-perry

Some words on lazy evaluation and sharing

I’ve read this article comparing the fix definition with a hand-made one and its “sharing” problems. Written by Joel E. Kaasinen

Tags: joel-kaasinen, haskell, fix, sharing, non-strictness, lazy-evaluation

Hammock Driven Development

I’ve watched this video by Rich Hickey. A few notes:

A: identifying a problem that we’re trying to solve

D: assessing if the proposed solution solves the problem

the union of the features is not the purpose of the product / is not the product

proramming is not completing features

avoiding problems /= solving problems

book: how to solve it - G. Polya. Practices & techniques for solving mathematic problems

How to solve the problem

  • state the problem
  • understand the problem:
    • what do you know: facts, context, constraints.
    • what do you don’t know
    • are there related problems? solutions to related problems. find an analogy: starting from scratch vs incremental step
  • be discerning
  • more input, better output:
    • read about your domain problem, research papers (maybe ACM)
  • tradeoffs:
    • find at least two options (ups and downs) to compare before taking a decision

Focus time for programmers like alone time for kids. Hammock time is important mind’s eye time

Waking mind:

  • critical thinking
  • analysis
  • tactics
  • prone to local maxima
  • feed work to background mind
  • analyze its products

Background mind:

  • making connections
  • synthesis
  • strategy
  • abstracts, analogies
  • solves most non-trivial problems

loading it: work it hard in the waking mind to be included in the background mind

abstraction = software strategy

tip: small answer is better than big answer (occam’s razor?)​

Tags: rich-hickey, hammock-driven-development, background-mind, waking-mind, solve-problem, george-polya, book, analysis, design, talk

Parallelism /= Concurrency

I’ve read this article on the differences between parallelism and concurrency, with examples in the haskell compiler GHC. Written by simonmar

Tags: haskell, concurrency, parallelism, ghc, simonmar, comparison

Parallelism vs. Concurrency

I’ve read this page from the haskell wiki.

Tags: haskell, concurrency, parallelism, ghc, comparison

Writing code that writes code — with Hack Codegen

I’ve read this article about metaprogramming (code that generates code) in PHP. Written by Alejandro Marcu.

I’ve arrived to this from this tweet:

Tags: alejandro-marcu, metaprogramming, php, macro, lisp, clojure

How katas can help you learn

I’ve read this article on learning from katas, with the example of the Roman Numerals kata in Objective-C. Written by Sandro Mancuso.

Tags: sandro-mancuso, kata, learning, roman-numeral, objective-c

Java 8: Definitive guide to CompletableFuture

I’ve read this article about Java 8’s feature, the CompletableFeature. Written by Tomasz Nurkiewicz

Tags: tomasz-nurkiewicz, java-8, completable-future, future, non-blocking, reactive-programming

Are You Making These 10 DDD Mistakes?

I’ve read this infographic on 10 DDD common mistakes, by Daniel Whittaker.

Tags: ddd, daniel-whittaker, bounded-context, ubiquitous-language, infographic

Haste: Full-Stack Haskell for Non-PhD Candidates

I’ve watched this talk at the Strange Loop Conference 2014 about Haste language, a haskell version that compiles to javascript, therefore letting you write haskell in the browser. By Erin Swenson-Healey and James Cooper

In this talk they recommend the CIS194 course

Tags: haste, haskell, james-cooper, erin-swenson-healey, strange-loop, 2014, talk

A Year of Haskell

I’ve watched this talk about experiences using haskell professionally, by the American Justin Leitgeb in a Equatorian company in Quito, Stack Builders. He goes on to explain the haskell language in comparison to ruby on rails, what he mostly used with his clients.

Also explains what sells and doesn’t (mostly productivity, not security) and gives pointer for applying it to a company

Tags: justin-leitgeb, ecuador, haskell, stack-builders, lambda-days, 2015, client, professional, consulting, consultancy

A Taste of Haskell

I’ve watched a lesson by Simon Peyton-Jones, that introduces haskell to experienced programmers, not in the language but in programming in general. At the conference OSCON 2007

Links: part 1, part 2, slides

Tags: oscon, 2007, haskell, introduction, lesson, simon-peyton-jones

Programming - Why Haskell is Great

I’ve seen this video introducing some features of haskell. The most basic features, the REPL, function composition. By FrungyKing

Tags: haskell, introduction, video, screencast, repl, frungyking

History of Haskell: being lazy with class

I’ve watched this video by Simon Peyton-Jones at the “History of Programming Languages Conference III” which explains how the language was designed and written, a language timeline, what options they took, how they decided about monadic IO, etc.

Tags: simon-peyton-jones, 2007, history-of-programming-languages-conference, haskell, talk

The Humble Programmer

I’ve read this EWD about the necessary humbleness for being a programmer and the slow expansion of the trade, as compared to hardware engineers.

He cites six reasons for preferring to be a humble programmer:

  • the programmer only needs to manage simple programs
  • simple programs imply reducing the search space
  • construct a program from a mathematical proof instead of test its properties after building the software. see quote below
  • a good abstraction can hide much complexity and difficulty
  • the importance of the used tool in the final product
  • “the only problems we can satisfactory solve are the ones that admit a factored solution”

In this essay I’ve found the idea of

Testing shows the presence, not the absence of bugs

That Wikiquote attributes to an earlier paper (in 1969). In this paper, the verbatim quote is:

program testing can be a very effective way to show the presence of bugs, but is hopelessly inadequate for showing their absence.

This idea also connects to the “lower bounds” and “upper bounds” of program correctness expressed in the article An Old Article I Wrote which is about static types, by Cdsmith. Admittedly, much later, in 2008, so this idea could be inspired by the Dijkstra’s article.

Tags: edsger-dijkstra, ewd, dijkstra, humble-programmer, essay, comparison, absence-of-bugs, quote

I’ve seen this video of how to build a link shortener webapp in haskell with the framework snap. By Ryan Trinkle. Found it in the snap docs page.

Tags: snap, haskell, link-shortener, talk, webapp, framework, ryan-trinkle

Comparing Haskell Web Frameworks

I’ve read this comparison of haskell web frameworks, by Andraz Bajt (a.k.a, edofic)

Tags: andraz-bajt, edofic, haskell, web-framework, framework, comparison

Packing your own maven dependencies

Jul 30, 2015 - 2 minute read - Comments - mavenprotipjitpack

Lately, I’ve found myself repeating always the same dependencies for my pet projects and katas. Usually, I prefer maven to hold my java dependencies, organized in a java project.

This is how most of them look like:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.gmaur.legacycode</groupId>
  <artifactId>legacyutils</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
  
  	<dependency>
  		<groupId>org.hamcrest</groupId>
  		<artifactId>hamcrest-all</artifactId>
  		<version>1.3</version>
  		<scope>test</scope>
  	</dependency>
  
  	<dependency>
  		<groupId>junit</groupId>
  		<artifactId>junit</artifactId>
  		<version>4.11</version>
  		<scope>test</scope>
  	</dependency>

  	<dependency>
  		<groupId>org.mockito</groupId>
  		<artifactId>mockito-all</artifactId>
  		<scope>test</scope>
  		<version>2.0.2-beta</version>
	</dependency>
  </dependencies>

  <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <includes>
                        <include>**/**Test.java</include>
                        <include>**/**Should.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Note: the org.apache.maven.plugins:maven-surefire-plugin is to make maven execute the tests that end in *Should.

Now, the dependency versions and plugin configuration is repeated in all the projects, which is plenty of repetition.

Enter JitPack.io

With the tool JitPack.io, you can generate your own dependencies. A guide on how to use it can be found here

I’ve published my own java dependency (originally a github release)

A pom.xml in the new style is here:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example.spike</groupId>
    <artifactId>lambdatesting</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>com.github.alvarogarcia7</groupId>
        <artifactId>java-parent</artifactId>
        <version>v0.0.1</version>
    </parent>

    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.github.alvarogarcia7</groupId>
            <artifactId>java-parent</artifactId>
            <version>v0.0.1</version>
            <type>pom</type>
        </dependency>

        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

It does not need the plugin configuration nor the dependency versions. The downside is that it needs to be as the parent pom.

Acknowledgments

Thanks to Manuel for encouraging me to write this article

Tool to Find Duplicate values in Constants

Jul 30, 2015 - 2 minute read - Comments - sampletooljavaduplicategmaurlibrary

Imagine having this java class:

private static class RepeatedConstants {
    public static final String A = "A";
    public static final String A_1 = A;
    public static final String A_2 = "A";

    public static final Integer _3 = 3;
    public static final Integer THREE = 3;
}

I wanted to remove the repeated values in the constants, in an automatic way, because the file was big (> 4000 constants). A way of doing this is basing the differences on the values, directly. To do this, remove all text that is not a value:

find what, regex mode: ^.*=\s*(.*); replace with: $1

Explanation:

  • ^.*=\s*: any character before the equal sign. Any whitespace right after it.
  • (.*);: capture everything that is before the semicolon (;)

You end up with this:

"A"
A
"A"

3
3

Copy it to a.txt, sort it and uniq it (to remove duplication):

cat a.txt | sort | uniq > uniq.txt

then sort the non-uniq version:

cat a.txt | sort > sorted.txt

and diff among them, to find the repeated values:

diff sorted.txt uniq.txt

3,4d2
< "A"
< 3

These are the repeated values. The variable public static final String A_1 = A; was not caught by this as it only has the same value in execution, not staticly.

For this, at Gmaur, we have developed a small module to detect duplicates. The main code is this:

public class RepeatedFinder {

  private final Class aClass;

  public RepeatedFinder(Class aClass) {
    this.aClass = aClass;
  }

  public MultiMap findDuplicates() throws IllegalAccessException {
    return generateMapFromValueToVariables(aClass).valuesWithMoreThanOneVariable();
  }

  private ValueToNameRepository generateMapFromValueToVariables(Class<?> clazz) throws IllegalAccessException {
    ValueToNameRepository valueToNames = new ValueToNameRepository();
    Field[] fields = clazz.getDeclaredFields();
    Object object = new Object();
    for (Field field : fields) {
      Object value = field.get(object);
      valueToNames.add(value, field.getName());
    }
    return valueToNames;
  }

The tests and the source code can be found here: https://github.com/GMaur/java-duplicates-detector

Hope you enjoy it, as much as we have enjoyed writing it!

Any feedback is welcome.