当前位置: 首页>>代码示例>>Java>>正文


Java JavaContext.findSurroundingMethod方法代码示例

本文整理汇总了Java中com.android.tools.lint.detector.api.JavaContext.findSurroundingMethod方法的典型用法代码示例。如果您正苦于以下问题:Java JavaContext.findSurroundingMethod方法的具体用法?Java JavaContext.findSurroundingMethod怎么用?Java JavaContext.findSurroundingMethod使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.android.tools.lint.detector.api.JavaContext的用法示例。


在下文中一共展示了JavaContext.findSurroundingMethod方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: isInflateCalledInOnCreateView_ForFragment

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
/**
 * inflater.inflate() can be called from anywhere.
 * We only care about the one call in {public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)}.
 * This method whill check that.
 */
private boolean isInflateCalledInOnCreateView_ForFragment(@NonNull JavaContext context, @NonNull MethodInvocation node) {
    Node surroundingNode = JavaContext.findSurroundingMethod(node);
    JavaParser.ResolvedNode resolvedNode = context.resolve(surroundingNode);

    try {
        String resolvedNodeName = resolvedNode.getName();
        if ("onCreateView".equals(resolvedNodeName)) {
            return true;
        }
    } catch (Exception e) {
        return false;
    }
    return false;

}
 
开发者ID:ljfxyj2008,项目名称:CustomLintDemo,代码行数:21,代码来源:ActivityFragmentLayoutNameDetector.java

示例2: addLocalPermissions

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
@NonNull
private static PermissionHolder addLocalPermissions(
        @NonNull JavaContext context,
        @NonNull PermissionHolder permissions,
        @NonNull Node node) {
    // Accumulate @RequirePermissions available in the local context
    Node methodNode = JavaContext.findSurroundingMethod(node);
    if (methodNode == null) {
        return permissions;
    }
    ResolvedNode resolved = context.resolve(methodNode);
    if (!(resolved instanceof ResolvedMethod)) {
        return permissions;
    }
    ResolvedMethod method = (ResolvedMethod) resolved;
    ResolvedAnnotation annotation = method.getAnnotation(PERMISSION_ANNOTATION);
    permissions = mergeAnnotationPermissions(context, permissions, annotation);
    annotation = method.getContainingClass().getAnnotation(PERMISSION_ANNOTATION);
    permissions = mergeAnnotationPermissions(context, permissions, annotation);
    return permissions;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:22,代码来源:SupportAnnotationDetector.java

示例3: visitMethodInvocation

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
@Override
public boolean visitMethodInvocation(MethodInvocation node) {
     JavaParser.ResolvedMethod resolvedMethod = NodeUtils.parseResolvedMethod(mContext, node);
     if (null == resolvedMethod) {
          return  super.visitMethodInvocation(node);
     }
     //判断是不是zipEntry.getName()函数
     if ("java.util.zip.ZipEntry".equals(resolvedMethod.getContainingClass().getName()) && "public java.lang.String getName() ".equals(resolvedMethod.getSignature())) {
          //找到当前调用所在的方法定义边界
          boolean isSafe = false;
          Node surroundingNode = JavaContext.findSurroundingMethod(node);
          Node parent = node.getParent();
          while (surroundingNode != parent) {
               //判断有没有做保护
               String judgeState = ".contains(\"../\")";
               if (parent.toString().contains(judgeState)) {
                    isSafe = true;
                    break;
               }
               parent = parent.getParent();
          }
          if (!isSafe) {
               mContext.report(
                         ZipEntryDetector.ISSUE,
                         mContext.getLocation(node),
                         "请检查zipEntry.getName()的返回值中是否含有字符串../,如果有,则是有危险的Zip包(可抛异常等操作"
               );
          }
     }
     return super.visitMethodInvocation(node);
}
 
开发者ID:squirrel-explorer,项目名称:eagleeye-android,代码行数:32,代码来源:ZipEntryAstVisitor.java

示例4: visitConstructorInvocation

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
@Override
public boolean visitConstructorInvocation(ConstructorInvocation node) {
    Node containingMethod = JavaContext.findSurroundingMethod(node);
    if (null == containingMethod) {
        return super.visitConstructorInvocation(node);
    }

    JavaParser.ResolvedMethod resolvedMethod = NodeUtils.parseResolvedMethod(mContext, containingMethod);
    if (null == resolvedMethod) {
        return super.visitConstructorInvocation(node);
    }
    JavaParser.ResolvedClass resolvedClass = resolvedMethod.getContainingClass();
    if (null == resolvedClass) {
        return super.visitConstructorInvocation(node);
    }

    for (String observedClass : OBSERVED_METHODS.keySet()) {
        if (resolvedClass.isSubclassOf(observedClass, false) &&
                OBSERVED_METHODS.get(observedClass).contains(resolvedMethod.getName())) {
            mContext.report(
                    WrongAllocationDetector.ISSUE,
                    mContext.getLocation(node),
                    String.format("Please don't create new objects in %s : %s", resolvedClass.getName(), resolvedMethod.getName())
            );
        }
    }

    return super.visitConstructorInvocation(node);
}
 
开发者ID:squirrel-explorer,项目名称:eagleeye-android,代码行数:30,代码来源:WrongAllocationAstVisitor.java

示例5: checkSingleThreadIterating

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
private boolean checkSingleThreadIterating(MethodInvocation node) {
    boolean ret = false;

    if (!modCountChangeable(node)) {
        return ret;
    }

    // 获取Method的operand,此处应为一个Collection实例
    String operand = node.astOperand().toString();

    // 获取当前调用所在的方法定义,作为检查的边界
    Node surroundingMethod = JavaContext.findSurroundingMethod(node);
    Node parent = node.getParent();
    // 试图寻找,可能改变operand modCount的方法,是否运行在operand的iterating中
    while (surroundingMethod != parent) {
        if (parent instanceof ForEach &&
                operand.equals(((ForEach)parent).astIterable().toString())) {
            ret = true;
            mContext.report(
                    ConcurrentModificationDetector.ISSUE,
                    mContext.getLocation(node),
                    "Please DO NOT modify elements DIRECTLY when iterating \"" + operand + "\".");
            break;
        }
        parent = parent.getParent();
    }

    return ret;
}
 
开发者ID:squirrel-explorer,项目名称:eagleeye-android,代码行数:30,代码来源:ConcurrentModificationAstVisitor.java

示例6: isCommittedInChainedCalls

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
/**
 * @return true if exists a commit or a getIntent
 */
private static boolean isCommittedInChainedCalls(@NonNull JavaContext context, @NonNull MethodInvocation node) {
    JavaParser.ResolvedVariable boundVariable = getVariable(context, node);
    if (boundVariable == null) {
        Node parent = node.getParent();
        while (parent instanceof MethodInvocation) {
            MethodInvocation methodInvocation = (MethodInvocation) parent;
            if (isTransactionCommitMethodCall(methodInvocation)) {
                return true;
            }

            parent = parent.getParent();
        }
    }

    if (boundVariable != null) {
        Node method = JavaContext.findSurroundingMethod(node);
        if (method == null) {
            return true;
        }

        FinishVisitor commitVisitor = new FinishVisitor(context, boundVariable) {
            @Override
            protected boolean isCleanupCall(@NonNull MethodInvocation call) {
                if (isTransactionCommitMethodCall(call)) {
                    Expression operand = call.astOperand();
                    if (operand != null) {
                        JavaParser.ResolvedNode resolved = mContext.resolve(operand);
                        //noinspection SuspiciousMethodCalls
                        if (resolved != null && mVariables.contains(resolved)) {
                            return true;
                        } else if (resolved instanceof JavaParser.ResolvedMethod
                                && operand instanceof MethodInvocation
                                && isCommittedInChainedCalls(mContext, (MethodInvocation) operand)) {
                            // Check that the target of the committed chains is the
                            // right variable!
                            while (operand instanceof MethodInvocation) {
                                operand = ((MethodInvocation) operand).astOperand();
                            }
                            if (operand instanceof VariableReference) {
                                resolved = mContext.resolve(operand);
                                //noinspection SuspiciousMethodCalls
                                if (resolved != null && mVariables.contains(resolved)) {
                                    return true;
                                }
                            }
                        }
                    }
                }
                return false;
            }
        };

        method.accept(commitVisitor);
        if (commitVisitor.isCleanedUp()) {
            return true;
        }
    }

    return false;
}
 
开发者ID:massivedisaster,项目名称:ActivityFragmentManager,代码行数:64,代码来源:CommitTransaction.java

示例7: visitConstructorInvocation

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
@Override
public boolean visitConstructorInvocation(ConstructorInvocation node) {
    // 寻找new Thread()的节点
    JavaParser.ResolvedClass typeClass = NodeUtils.parseContainingClass(mContext, node);
    if (null == typeClass || !typeClass.isSubclassOf("java.lang.Thread", false)) {
        return super.visitConstructorInvocation(node);
    }

    // 获取new Thread()的赋值对象
    Node operand = findAssignOperand(node);
    // 获取new Thread()所在方法
    Node containingMethod = JavaContext.findSurroundingMethod(node);
    if (null == containingMethod) {
        return super.visitConstructorInvocation(node);
    }

    // 获取containingMethod中所有的operand.setPriority()节点的列表
    ArrayList<MethodInvocation> setPriorityNodes = new ArrayList<MethodInvocation>();
    searchSetPriorityNodes(operand, containingMethod, setPriorityNodes);
    // 找出最后一个operand.setPriority()节点
    MethodInvocation lastSetPriorityNode = setPriorityNodes.isEmpty() ? null : setPriorityNodes.get(setPriorityNodes.size() - 1);
    if (null == lastSetPriorityNode) {
        // 在Android里,new Thread()创建的线程,缺省与当前线程具有相同优先级,所以
        // 如果没有显式降低优先级,我们认为是有问题的情形
        mContext.report(
                ThreadPriorityDetector.ISSUE,
                mContext.getLocation(node),
                "Please set lower priority for new thread");
    } else {
        // 如果最后一次调用operand.setPriority()没有设置较低优先级,则报警
        if (!validateSetLowerPriority(lastSetPriorityNode)) {
            mContext.report(
                    ThreadPriorityDetector.ISSUE,
                    mContext.getLocation(lastSetPriorityNode),
                    "Please set lower priority for new thread");
        }
    }
    setPriorityNodes.clear();

    return super.visitConstructorInvocation(node);
}
 
开发者ID:squirrel-explorer,项目名称:eagleeye-android,代码行数:42,代码来源:ThreadPriorityAstVisitor.java

示例8: visitMethod

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
@Override
public void visitMethod(@NonNull JavaContext context, @Nullable AstVisitor visitor,
        @NonNull MethodInvocation call) {
    String lhs = getLhs(call);
    if (lhs == null) {
        return;
    }

    Node method = JavaContext.findSurroundingMethod(call);
    if (method == null) {
        return;
    } else if (method != mLastMethod) {
        mIds = Maps.newHashMap();
        mLhs = Maps.newHashMap();
        mCallOperands = Maps.newHashMap();
        mLastMethod = method;
    }

    String callOperand = call.astOperand() != null ? call.astOperand().toString() : "";

    Expression first = call.astArguments().first();
    if (first instanceof Select) {
        Select select = (Select) first;
        String id = select.astIdentifier().astValue();
        Expression operand = select.astOperand();
        if (operand instanceof Select) {
            Select type = (Select) operand;
            if (type.astIdentifier().astValue().equals(RESOURCE_CLZ_ID)) {
                if (mIds.containsKey(id)) {
                    if (lhs.equals(mLhs.get(id))) {
                        return;
                    }
                    if (!callOperand.equals(mCallOperands.get(id))) {
                        return;
                    }
                    MethodInvocation earlierCall = mIds.get(id);
                    if (!isReachableFrom(method, earlierCall, call)) {
                        return;
                    }
                    Location location = context.getLocation(call);
                    Location secondary = context.getLocation(earlierCall);
                    secondary.setMessage("First usage here");
                    location.setSecondary(secondary);
                    context.report(ISSUE, call, location, String.format(
                        "The id `%1$s` has already been looked up in this method; possible " +
                        "cut & paste error?", first.toString()));
                } else {
                    mIds.put(id, call);
                    mLhs.put(id, lhs);
                    mCallOperands.put(id, callOperand);
                }
            }
        }
    }
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:56,代码来源:CutPasteDetector.java

示例9: checkRecycled

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
private static void checkRecycled(@NonNull final JavaContext context, @NonNull Node node,
        @NonNull final String recycleType, @NonNull final String recycleName) {
    ResolvedVariable boundVariable = getVariable(context, node);
    if (boundVariable == null) {
        return;
    }

    Node method = JavaContext.findSurroundingMethod(node);
    if (method == null) {
        return;
    }

    FinishVisitor visitor = new FinishVisitor(context, boundVariable) {
        @Override
        protected boolean isCleanupCall(@NonNull MethodInvocation call) {
            String methodName = call.astName().astValue();
            if (!recycleName.equals(methodName)) {
                return false;
            }
            ResolvedNode resolved = mContext.resolve(call);
            if (resolved instanceof ResolvedMethod) {
                ResolvedClass containingClass = ((ResolvedMethod) resolved).getContainingClass();
                if (containingClass.isSubclassOf(recycleType, false)) {
                    // Yes, called the right recycle() method; now make sure
                    // we're calling it on the right variable
                    Expression operand = call.astOperand();
                    if (operand != null) {
                        resolved = mContext.resolve(operand);
                        //noinspection SuspiciousMethodCalls
                        if (resolved != null && mVariables.contains(resolved)) {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
    };

    method.accept(visitor);
    if (visitor.isCleanedUp() || visitor.variableEscapes()) {
        return;
    }

    String className = recycleType.substring(recycleType.lastIndexOf('.') + 1);
    String message;
    if (RECYCLE.equals(recycleName)) {
        message = String.format(
                "This `%1$s` should be recycled after use with `#recycle()`", className);
    } else {
        message = String.format(
                "This `%1$s` should be freed up after use with `#%2$s()`", className,
                recycleName);
    }
    Node locationNode = node instanceof MethodInvocation ?
            ((MethodInvocation) node).astName() : node;
    Location location = context.getLocation(locationNode);
    context.report(RECYCLE_RESOURCE, node, location, message);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:60,代码来源:CleanupDetector.java


注:本文中的com.android.tools.lint.detector.api.JavaContext.findSurroundingMethod方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。