In my current project I am making extensive use of dependency injection using Guice. More specifically I am using member injection to inject objects into the member variables of a class. In java member variables should usually be private, but this is even more important when using injection as the whole purpose of injecting members is to remove explicit dependencies and make code more modular. However, try as I might I cannot remember to change all my protected injected variables to private. Therefore I ended up just creating a PMD rule to the same effect.

<rule name="PrivateInjections" message="Please make injected fields private"
		class="net.sourceforge.pmd.rules.XPathRule">

	<description>We don't take kindly to non private injected fields round
			these parts
	</description>
	<priority>1</priority>
	<properties>
		<property name="xpath">
			<value>
     			<![CDATA[//ClassOrInterfaceBodyDeclaration [ 
     				contains(Annotation//Name/@Image,'Inject')
     				and 
     				contains(FieldDeclaration/@Private,'false')]]
     			]]>
			</value>
		</property>
	</properties>
	<example>
		<![CDATA[
			@Inject
			public String myParameter; //is bad

			public String myParameter; //is better

			@Inject
			private String myParameter; //is best
    	]]>
	</example>
</rule>