package com.squirrel_explorer.eagleeye.lint.rules.performance.view;

import com.android.tools.lint.client.api.JavaParser;
import com.android.tools.lint.detector.api.JavaContext;
import com.squirrel_explorer.eagleeye.types.base.BaseAstVisitor;
import com.squirrel_explorer.eagleeye.utils.NodeUtils;

import java.util.Arrays;
import java.util.List;

import lombok.ast.ConstructorInvocation;

/**
 * Created by squirrel-explorer on 16/02/22.
 */
public class WrongAllocationAstVisitor extends BaseAstVisitor {
    public WrongAllocationAstVisitor(JavaContext context) {
        super(context);
    }

    // Methods with the limit on creating new objects
    // (currently only on some methods of View)
    private static final List<String> methodsObserved = Arrays.asList(
            "onMeasure",
            "onLayout",
            "onDraw"
    );

    @Override
    public boolean visitConstructorInvocation(ConstructorInvocation node) {
        JavaParser.ResolvedMethod method = NodeUtils.findEnclosingMethod(mContext, node);
        if (null != method) {
            JavaParser.ResolvedClass clazz = method.getContainingClass();
            if (null != clazz && clazz.isSubclassOf("android.view.View", false) &&
                    methodsObserved.contains(method.getName())) {
                mContext.report(
                        WrongAllocationDetector.ISSUE,
                        mContext.getLocation(node),
                        String.format("Please don't create new objects in %s : %s", clazz.getName(), method.getName())
                );
            }
        }

        return super.visitConstructorInvocation(node);
    }
}

@@ -240,64 +240,34 @@
 or;%0A
+%0A
 import 
-com.squirrel_explorer.eagleeye.utils.NodeUtil
+java.util.Array
 s;%0A
-%0A
 impo
@@ -271,38 +271,39 @@
 mport java.util.
-Arrays
+HashMap
 ;%0Aimport java.ut
@@ -305,19 +305,22 @@
 va.util.
-Lis
+HashSe
 t;%0A%0Aimpo
@@ -355,16 +355,40 @@
 ocation;
+%0Aimport lombok.ast.Node;
 %0A%0A/**%0A *
@@ -619,17 +619,19 @@
 e limit 
-o
+whe
 n creati
@@ -653,108 +653,184 @@
     
-// (currently only on some methods of View)%0A    private static final List%3CString%3E methodsObserved = 
+private static final HashMap%3CString, HashSet%3CString%3E%3E OBSERVED_METHODS = new HashMap%3C%3E();%0A    %7B%0A        OBSERVED_METHODS.put(%22android.view.View%22,%0A                new HashSet%3C%3E(
 Arra
@@ -852,16 +852,28 @@
         
+            
 %22onMeasu
@@ -889,16 +889,28 @@
         
+            
 %22onLayou
@@ -929,16 +929,28 @@
     
+            
 %22onDraw%22
 %0A   
@@ -949,231 +949,707 @@
 raw%22
+,
 %0A    
-);%0A%0A    @Override%0A    public boolean visitConstructorInvocation(ConstructorInvocation node) %7B%0A        JavaParser.ResolvedMethod method = NodeUtils.findEnclosingMethod(mContext, node);%0A        if (null != method) %7B%0A    
+                    %22dispatchDraw%22,%0A                        %22dispatchKeyEvent%22%0A                )));%0A    %7D%0A%0A    @Override%0A    public boolean visitConstructorInvocation(ConstructorInvocation node) %7B%0A        Node containingMethod = JavaContext.findSurroundingMethod(node);%0A        if (null == containingMethod) %7B%0A            return super.visitConstructorInvocation(node);%0A        %7D%0A%0A        JavaParser.ResolvedNode resolvedNode = mContext.resolve(containingMethod);%0A        if (!(resolvedNode instanceof JavaParser.ResolvedMethod)) %7B%0A            return super.visitConstructorInvocation(node);%0A        %7D%0A        JavaParser.ResolvedMethod resolvedMethod = (JavaParser.ResolvedMethod)resolvedNode;%0A%0A
     
@@ -1681,17 +1681,33 @@
 ass 
-clazz = m
+resolvedClass = resolvedM
 etho
@@ -1734,28 +1734,24 @@
 ();%0A        
-    
 if (null != 
@@ -1751,58 +1751,219 @@
 ull 
-!= clazz && clazz.isSubclassOf(%22android.view.View%22
+== resolvedClass) %7B%0A            return super.visitConstructorInvocation(node);%0A        %7D%0A%0A        for (String observedClass : OBSERVED_METHODS.keySet()) %7B%0A            if (resolvedClass.isSubclassOf(observedClass
 , fa
@@ -1994,23 +1994,43 @@
     
-methodsO
+OBSERVED_METHODS.get(o
 bserved
+Class)
 .con
@@ -2035,17 +2035,25 @@
 ontains(
-m
+resolvedM
 ethod.ge
@@ -2292,13 +2292,21 @@
 s%22, 
-clazz
+resolvedClass
 .get
@@ -2313,17 +2313,25 @@
 Name(), 
-m
+resolvedM
 ethod.ge

更新WrongAllocationDetector规则，提升检测准确性