01: package spoon.vsuite.findbugs.dmi;
02:
03: import java.io.File;
04:
05: import spoon.processing.AbstractProcessor;
06: import spoon.processing.Property;
07: import spoon.processing.Severity;
08: import spoon.reflect.Factory;
09: import spoon.reflect.code.CtCodeElement;
10: import spoon.reflect.code.CtLiteral;
11: import spoon.reflect.code.CtNewClass;
12: import spoon.reflect.eval.PartialEvaluator;
13: import spoon.reflect.reference.CtExecutableReference;
14:
15: /**
16: *
17: * DMI: Code contains a hard coded reference to an absolute pathname
18: * (DMI_HARDCODED_ABSOLUTE_FILENAME) This code constructs a File object using a
19: * hard coded to an absolute pathname (e.g., new
20: * File("/home/dannyc/workspace/j2ee/src/share/com/sun/enterprise/deployment");
21: *
22: * @author Nicolas Petitprez
23: */
24: public class HardcodedAbsoluteFilename extends
25: AbstractProcessor<CtNewClass<?>> {
26:
27: @Property
28: static Severity level = Severity.WARNING;
29:
30: private static CtExecutableReference<?> exefileref;
31:
32: public static CtExecutableReference<?> getExeFileRef(Factory f) {
33: if (exefileref == null) {
34: exefileref = f.Constructor().createReference(
35: f.Type().createReference(File.class),
36: f.Type().createReference(File.class), "<init>",
37: f.Type().createReference(String.class));
38: }
39: return exefileref;
40: }
41:
42: @SuppressWarnings("unchecked")
43: public void process(CtNewClass<?> arg0) {
44: if (arg0.getExecutable().equals(getExeFileRef(getFactory()))) {
45: CtCodeElement element = arg0.getArguments().get(0);
46: try {
47: PartialEvaluator eval = getFactory().Eval()
48: .createPartialEvaluator();
49: element = eval.evaluate(arg0, element);
50: } catch (Throwable e) {
51: // Evaluator could throw exception
52: e.printStackTrace();
53: }
54:
55: if (element instanceof CtLiteral) {
56: String value = ((CtLiteral<String>) element).getValue();
57:
58: for (File root : File.listRoots()) {
59: if (value.startsWith(root.toString())) {
60: getEnvironment().report(
61: this ,
62: level,
63: arg0,
64: "Code contains a hard coded reference to \""
65: + value + "\"");
66: }
67:
68: }
69:
70: }
71:
72: }
73: }
74: }
|