Skip to content

Commit

Permalink
adding metadata and messages for public visibility and public constru…
Browse files Browse the repository at this point in the history
…ctor visibility detectors
  • Loading branch information
heuermh committed Feb 8, 2012
1 parent bb497cb commit e43d33d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
Expand Up @@ -37,11 +37,12 @@ public void sawOpcode(final int seen) {
if (isCallingTo()) { if (isCallingTo()) {
OpcodeStack.Item stackItem = stack.getStackItem(0); OpcodeStack.Item stackItem = stack.getStackItem(0);
try { try {
String implementationClassName = (String) stackItem.getConstant(); String slashedClassName = (String) stackItem.getConstant();
Class<?> implementationClass = Class.forName(implementationClassName.replace("/", ".")); String dottedClassName = slashedClassName.replace("/", ".");
Class<?> implementationClass = Class.forName(dottedClassName);
for (Constructor<?> constructor : implementationClass.getDeclaredConstructors()) { for (Constructor<?> constructor : implementationClass.getDeclaredConstructors()) {
if (Modifier.isPublic(constructor.getModifiers())) { if (Modifier.isPublic(constructor.getModifiers())) {
bugReporter.reportBug(new BugInstance(this, "GUICE_PUBLIC_IMPLEMENTATION_CLASS_CONSTRUCTOR", NORMAL_PRIORITY).addClassAndMethod(this)); bugReporter.reportBug(new BugInstance(this, "GUICE_PUBLIC_IMPLEMENTATION_CLASS_CONSTRUCTOR", NORMAL_PRIORITY).addClassAndMethod(this).addTypeOfNamedClass(dottedClassName));
} }
} }
} }
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/uk/me/tom_fitzhenry/findbugs/guice/PublicImplementationClassDetector.java 100644 → 100755
Expand Up @@ -36,10 +36,11 @@ public void sawOpcode(final int seen) {
if (isCallingTo()) { if (isCallingTo()) {
OpcodeStack.Item stackItem = stack.getStackItem(0); OpcodeStack.Item stackItem = stack.getStackItem(0);
try { try {
String implementationClassName = (String) stackItem.getConstant(); String slashedClassName = (String) stackItem.getConstant();
Class<?> implementationClass = Class.forName(implementationClassName.replace("/", ".")); String dottedClassName = slashedClassName.replace("/", ".");
Class<?> implementationClass = Class.forName(dottedClassName);
if (Modifier.isPublic(implementationClass.getModifiers())) { if (Modifier.isPublic(implementationClass.getModifiers())) {
bugReporter.reportBug(new BugInstance(this, "GUICE_PUBLIC_IMPLEMENTATION_CLASS", NORMAL_PRIORITY).addClassAndMethod(this)); bugReporter.reportBug(new BugInstance(this, "GUICE_PUBLIC_IMPLEMENTATION_CLASS", NORMAL_PRIORITY).addClassAndMethod(this).addTypeOfNamedClass(dottedClassName));
} }
} }
catch (Exception e) { catch (Exception e) {
Expand Down
20 changes: 20 additions & 0 deletions src/main/resources/findbugs.xml 100644 → 100755
Expand Up @@ -24,6 +24,16 @@
speed="fast" speed="fast"
reports="GUICE_FINAL_FIELD_INJECTION" reports="GUICE_FINAL_FIELD_INJECTION"
/> />

<Detector class="uk.me.tom_fitzhenry.findbugs.guice.PublicImplementationClassDetector"
speed="fast"
reports="GUICE_PUBLIC_IMPLEMENTATION_CLASS"
/>

<Detector class="uk.me.tom_fitzhenry.findbugs.guice.PublicImplementationClassConstructorDetector"
speed="fast"
reports="GUICE_PUBLIC_IMPLEMENTATION_CLASS_CONSTRUCTOR"
/>


<BugPattern type="GUICE_SCOPE_ON_INTERFACE" <BugPattern type="GUICE_SCOPE_ON_INTERFACE"
category="CORRECTNESS" category="CORRECTNESS"
Expand All @@ -44,5 +54,15 @@
category="CORRECTNESS" category="CORRECTNESS"
abbrev="GUICE" abbrev="GUICE"
/> />

<BugPattern type="GUICE_PUBLIC_IMPLEMENTATION_CLASS"
category="CORRECTNESS"
abbrev="GUICE"
/>

<BugPattern type="GUICE_PUBLIC_IMPLEMENTATION_CLASS_CONSTRUCTOR"
category="CORRECTNESS"
abbrev="GUICE"
/>


</FindbugsPlugin> </FindbugsPlugin>
50 changes: 50 additions & 0 deletions src/main/resources/messages.xml 100644 → 100755
Expand Up @@ -31,6 +31,18 @@
</Details> </Details>
</Detector> </Detector>


<Detector class="uk.me.tom_fitzhenry.findbugs.guice.PublicImplementationClassDetector">
<Details>
Finds bindings to implementation classes with public visibility.
</Details>
</Detector>

<Detector class="uk.me.tom_fitzhenry.findbugs.guice.PublicImplementationClassConstructorDetector">
<Details>
Finds bindings to implementation classes with public constructor visibility.
</Details>
</Detector>

<BugPattern type="GUICE_SCOPE_ON_INTERFACE"> <BugPattern type="GUICE_SCOPE_ON_INTERFACE">
<ShortDescription>Scope annotation on interfaces</ShortDescription> <ShortDescription>Scope annotation on interfaces</ShortDescription>
<LongDescription>Interface {0} is annotated with a scope, {1}</LongDescription> <LongDescription>Interface {0} is annotated with a scope, {1}</LongDescription>
Expand Down Expand Up @@ -135,6 +147,44 @@


</Details> </Details>
</BugPattern> </BugPattern>

<BugPattern type="GUICE_PUBLIC_IMPLEMENTATION_CLASS">
<ShortDescription>Binding to implementation class with public visibility</ShortDescription>
<LongDescription>Module {0} binds to an implementation class {3} with public visibility.</LongDescription>
<Details>
<![CDATA[
Guice recommends keeping constructors on Guice-instantiated classes as hidden as possible.
<p><a href="http://code.google.com/p/google-guice/wiki/KeepConstructorsHidden">Guice best practices : KeepConstructorsHidden</a></p>
<p>Recommended correction:<br/>
Limit the visibility of both your implementation classes and their constructors. Typically package private is preferred for both, as this facilitates:
<ul>
<li>binding the class within a Module in the same package</li>
<li>unit testing the class through means of direct instantiation</li>
</ul></p>
]]>
</Details>
</BugPattern>

<BugPattern type="GUICE_PUBLIC_IMPLEMENTATION_CLASS_CONSTRUCTOR">
<ShortDescription>Binding to implementation class with public constructor visibility</ShortDescription>
<LongDescription>Module {0} binds to an implementation class {3} that has one or more constructor(s) with public visibility.</LongDescription>
<Details>
<![CDATA[
Guice recommends keeping constructors on Guice-instantiated classes as hidden as possible.
<p><a href="http://code.google.com/p/google-guice/wiki/KeepConstructorsHidden">Guice best practices : KeepConstructorsHidden</a></p>
<p>Recommended correction:<br/>
Limit the visibility of both your implementation classes and their constructors. Typically package private is preferred for both, as this facilitates:
<ul>
<li>binding the class within a Module in the same package</li>
<li>unit testing the class through means of direct instantiation</li>
</ul></p>
]]>
</Details>
</BugPattern>


<BugCode abbrev="GUICE">Guice bugcode abbreviation</BugCode> <BugCode abbrev="GUICE">Guice bugcode abbreviation</BugCode>


Expand Down

0 comments on commit e43d33d

Please sign in to comment.