Idiom.java :  » Code-Analyzer » Spoon » spoon » vsuite » findbugs » am » Java Open Source

Java Open Source » Code Analyzer » Spoon 
Spoon » spoon » vsuite » findbugs » am » Idiom.java
package spoon.vsuite.findbugs.am;

import spoon.processing.AbstractProblemFixer;
import spoon.processing.AbstractProcessor;
import spoon.processing.Property;
import spoon.processing.Severity;
import spoon.reflect.Changes;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.reference.CtTypeReference;
import spoon.template.Substitution;
import spoon.vsuite.findbugs.template.CloneTemplate;

/**
 * CN: Class implements Cloneable but does not define or use clone method
 * (CN_IDIOM). Class implements Cloneable but does not define or use the clone
 * method.
 * 
 * @author Nicolas Petitprez
 */
public class Idiom extends AbstractProcessor<CtClass<?>> {

  public class Fix1 extends AbstractProblemFixer<CtClass<?>> {

    public String getDescription() {
      return getLabel();
    }

    public String getLabel() {
      return "Add empty clone method";
    }

    public Changes run(CtClass<?> arg0) {

      try {
        CloneTemplate tmp = new CloneTemplate();
        Substitution.insertAll(arg0, tmp);

        Changes lst = new Changes();
        lst.getModified().add(arg0);
        return lst;
      } catch (Throwable e) {
        e.printStackTrace();
      }
      return null;
    }

  }

  @Property
  Severity level = Severity.WARNING;

  public void process(CtClass<?> arg0) {
    if (arg0.isSubtypeOf(getFactory().Type().createReference(
        Cloneable.class))) {
      CtMethod<?> m = arg0.getMethod("clone", new CtTypeReference[0]);
      if (m == null) {
        getFactory()
            .getEnvironment()
            .report(
                this,
                level,
                arg0,
                "Class implements Cloneable but does not define or use clone method",
                new Fix1());
      }
    }

  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.