Project

General

Profile

CheckForEach.java

Constantin Asofiei, 01/05/2023 04:22 AM

Download (2.92 KB)

 
1
package spoonp2j;
2

    
3
import java.lang.reflect.Method;
4
import java.util.*;
5

    
6
import spoon.*;
7
import spoon.processing.*;
8
import spoon.reflect.code.*;
9
import spoon.reflect.cu.SourcePosition;
10
import spoon.reflect.declaration.*;
11
import spoon.reflect.reference.*;
12
import spoon.reflect.visitor.chain.*;
13
import spoon.support.reflect.code.CtVariableReadImpl;
14

    
15
public class CheckForEach
16
{
17
   public static void main(String[] args)
18
   {
19
      Launcher spoon = new Launcher();
20
      spoon.addProcessor(new TypeFactoryProcessor());
21
      spoon.getEnvironment().setIgnoreSyntaxErrors(true);
22
      spoon.getEnvironment().setSourceClasspath(new String[] { "/home/ca/workspace/p2j4sync/build/lib/p2j.jar" });
23
      // spoon.addInputResource("/working/workspace/p2j4sync/src/com/goldencode/ast/AnnotatedAst.java");
24
      spoon.addInputResource("/working/workspace/p2j4sync/src/com/goldencode/");
25
      spoon.run();
26
   }
27

    
28
   static class TypeFactoryProcessor
29
   extends AbstractProcessor<CtMethod>
30
   {
31
      @Override
32
      public void process(CtMethod m)
33
      {
34
         CtBlock body = m.getBody();
35
         if (body == null)
36
         {
37
            return;
38
         }
39
         
40
         if (m.getParent() instanceof CtClass)
41
         {
42
            CtClass parent = (CtClass) m.getParent();
43
            if (parent.getSuperclass() != null && parent.getSuperclass().getSimpleName().equals("ContextLocal"))
44
            {
45
               return;
46
            }
47
         }
48
         else if (m.getParent() instanceof CtInterface)
49
         {
50
            // System.out.println(m.getParent());
51
         }
52
         
53
         Map<String, String> params = new HashMap<>();
54
         for (int i = 0; i < m.getParameters().size(); i++)
55
         {
56
            CtParameter p = (CtParameter) m.getParameters().get(i);
57
            params.put(p.getSimpleName(), p.getType().getSimpleName());
58
         }
59
         String type = m.getDeclaringType().getQualifiedName();
60
         String mthd = type + ":" + m.getSimpleName();
61
         checkStatement(mthd, body, params);
62
      }
63

    
64
      private void checkStatement(String mthd, CtStatement st, Map<String, String> params)
65
      {
66
         Iterator<CtElement> iter = st.asIterable().iterator();
67
         while (iter.hasNext())
68
         {
69
            CtElement element = iter.next();
70
            if (element instanceof CtForEach)
71
            {
72
               CtForEach f = (CtForEach) element;
73
               SourcePosition pos = f.getPosition();
74
               CtExpression expr = f.getExpression();
75
               CtTypeReference exprType = expr.getType();
76
               CtType type = exprType.getTypeDeclaration();
77
               String qtype = type == null ? exprType.toString() : type.getQualifiedName();
78
               if (qtype.equals(ArrayList.class.getName()) || qtype.equals(List.class.getName()) || qtype.endsWith("[]"))
79
               {
80
                  System.out.println(mthd + " " + exprType + " " + pos.getLine());
81
               }
82
            }
83
         }
84
      }
85

    
86
   }
87
}