1package annotations.tests.executable;
2
3/*>>>
4import org.checkerframework.checker.nullness.qual.NonNull;
5import org.checkerframework.checker.tainting.qual.Tainted;
6*/
7
8import java.io.*;
9import java.util.*;
10
11import annotations.*;
12import annotations.el.*;
13import annotations.io.*;
14
15/**
16 * Prints information about Tainted and NonNull annotations on a given class.
17 * Invoke as:
18 * <pre>
19 * java Example <i>input.jaif</i> <i>ClassToProcess</i> <i>output.jaif</i>
20 * </pre>
21 */
22public class Example {
23  public static void main(String [] args) {
24    AScene scene;
25
26    if (! new File(args[0]).exists()) {
27      try {
28        throw new Error(String.format("Cannot find file %s in directory %s",
29                                    args[0], new File(".").getCanonicalPath()));
30      } catch (IOException e) {
31        throw new Error("This can't happen: ", e);
32      }
33    }
34
35    // System.out.println("Reading in " + args[0]);
36    try {
37      scene = new AScene();
38      IndexFileParser.parseFile(args[0], scene);
39    } catch (IOException e) {
40      e.printStackTrace(System.err);
41      return;
42    }
43
44    System.out.println("Processing class " + args[1]);
45    // Get a handle on the class
46    AClass clazz1 = scene.classes.get(args[1]);
47    if (clazz1 == null) {
48      System.out.println("Class " + args[1] + " is not mentioned in annotation file " + args[0]);
49      return;
50    }
51    AClass clazz = (AClass) clazz1;
52
53    for (Map.Entry<String, AMethod> me : clazz.methods.entrySet()) {
54      AMethod method = me.getValue();
55
56      Annotation rro = method.receiver.type.lookup("Tainted");
57      if (rro == null) {
58        System.out.println("Method " + me.getKey()
59            + " might modify the receiver");
60      } else {
61        System.out.println("Method " + me.getKey()
62            + " must not modify the receiver");
63      }
64
65      ATypeElement paramType1 = method.parameters.vivify(0).type;
66      Annotation p1nn = paramType1.lookup("NonNull");
67      if (p1nn == null) {
68        System.out.println("Annotating type of first parameter of "
69            + me.getKey() + " nonnull");
70
71        paramType1.tlAnnotationsHere.add(Annotations.aNonNull);
72      }
73    }
74
75    // System.out.println("Writing out " + args[2]);
76    try {
77      IndexFileWriter.write(scene, new FileWriter(args[2]));
78    } catch (IOException e) {
79      e.printStackTrace(System.err);
80      return;
81    } catch (DefException e) {
82      e.printStackTrace(System.err);
83      return;
84    }
85
86    System.out.println("Success.");
87  }
88}
89