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


Java JavaContext.resolve方法代码示例

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


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

示例1: isAddReplaceOpen

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
private static boolean isAddReplaceOpen(@NonNull JavaContext context, @NonNull MethodInvocation node) {
    String methodName = node.astName().astValue();

    if (ADD_TRANSACTION.equals(methodName) || OPEN_TRANSACTION.equals(methodName) || REPLACE_TRANSACTION.equals(methodName)) {
        JavaParser.ResolvedNode resolved = context.resolve(node);
        if (resolved instanceof JavaParser.ResolvedMethod) {
            JavaParser.ResolvedMethod method = (JavaParser.ResolvedMethod) resolved;
            JavaParser.ResolvedClass containingClass = method.getContainingClass();
            if (containingClass.isSubclassOf(ACTIVITY_MANAGER, false)) {
                return true;
            }
        }
    }

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

示例2: createJavaVisitor

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
@Override
public AstVisitor createJavaVisitor(final JavaContext context) {
    return new ForwardingAstVisitor() {
        @Override
        public boolean visitMethodInvocation(MethodInvocation node) {
            JavaParser.ResolvedNode resolve = context.resolve(node);
            if (resolve instanceof JavaParser.ResolvedMethod) {
                JavaParser.ResolvedMethod method = (JavaParser.ResolvedMethod) resolve;
                // 方法所在的类校验
                JavaParser.ResolvedClass containingClass = method.getContainingClass();
                if (containingClass.matches("android.util.Log")) {
                    context.report(ISSUE, node, context.getLocation(node),
                                   "请使用Ln,避免使用Log");
                    return true;
                }
                if (node.toString().startsWith("System.out.println")) {
                    context.report(ISSUE, node, context.getLocation(node),
                                   "请使用Ln,避免使用System.out.println");
                    return true;
                }
            }
            return super.visitMethodInvocation(node);
        }
    };
}
 
开发者ID:GavinCT,项目名称:MeituanLintDemo,代码行数:26,代码来源:LogDetector.java

示例3: parseResolvedMethod

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
public static JavaParser.ResolvedMethod parseResolvedMethod(
        @NonNull JavaContext context, @NonNull Node node) {
    if (null == context || null == node) {
        return null;
    }

    JavaParser.ResolvedNode resolvedNode = null;
    try {
        resolvedNode = context.resolve(node);
    } catch (Exception e) {
        // TODO
    }

    return (resolvedNode instanceof JavaParser.ResolvedMethod) ?
            (JavaParser.ResolvedMethod)resolvedNode : null;
}
 
开发者ID:squirrel-explorer,项目名称:eagleeye-android,代码行数:17,代码来源:NodeUtils.java

示例4: isThisInstanceOfActivity_ForActivity

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
/**
 * If setContentView is called by 'this' instance,
 * this method will check if 'this' is an instance of a Class inherit from android.app.Activity, for eaxmple AppCompatActivity or FragmentActivity, and so on.
 */
private boolean isThisInstanceOfActivity_ForActivity(@NonNull JavaContext context, @NonNull MethodInvocation node) {
    Node currentNode = node.getParent();

    JavaParser.ResolvedNode resolved = context.resolve(JavaContext.findSurroundingClass(node));
    JavaParser.ResolvedClass sorroundingClass = (JavaParser.ResolvedClass) resolved;
    while (sorroundingClass != null) {
        //System.out.println("sorroundingClass = " + sorroundingClass);
        if ("android.app.Activity".equals(sorroundingClass.getName())) {
            return true;
        } else {
            sorroundingClass = sorroundingClass.getSuperClass();
        }
    }


    return false;


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

示例5: isThisMethodHasLayoutAnnotation_ForActivity

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
/**
 * As there are more than one methods overload "setContentView",
 * we have to identify the one we want to check, whose param has an Annotation of "@LayoutRes".
 * In fact, {public void setContentView(@LayoutRes int layoutResID)} is the one we are looking for.
 */
private boolean isThisMethodHasLayoutAnnotation_ForActivity(@NonNull JavaContext context, @NonNull MethodInvocation node) {
    JavaParser.ResolvedNode resolved = context.resolve(node);
    JavaParser.ResolvedMethod method = (JavaParser.ResolvedMethod) resolved;

    if (node.astArguments().size() != 1) {
        return false;
    }

    Iterable<JavaParser.ResolvedAnnotation> annotations = method.getParameterAnnotations(0);
    for (JavaParser.ResolvedAnnotation annotation : annotations) {
        if ("android.support.annotation.LayoutRes".equals(annotation.getName())) {
            return true;
        }
    }

    return false;

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

示例6: isAppBarActivityCall

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
private static boolean isAppBarActivityCall(@NonNull JavaContext context,
        @NonNull MethodInvocation node) {
    ResolvedNode resolved = context.resolve(node);
    if (resolved instanceof ResolvedMethod) {
        ResolvedMethod method = (ResolvedMethod) resolved;
        ResolvedClass containingClass = method.getContainingClass();
        if (containingClass.isSubclassOf(CLASS_ACTIVITY, false)) {
            // Make sure that the calling context is a subclass of ActionBarActivity;
            // we don't want to flag these calls if they are in non-appcompat activities
            // such as PreferenceActivity (see b.android.com/58512)
            ClassDeclaration surroundingClass = JavaContext.findSurroundingClass(node);
            if (surroundingClass != null) {
                ResolvedNode clz = context.resolve(surroundingClass);
                return clz instanceof ResolvedClass &&
                        ((ResolvedClass)clz).isSubclassOf(
                                "android.support.v7.app.ActionBarActivity",
                                false);
            }
        }
    }
    return false;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:23,代码来源:AppCompatCallDetector.java

示例7: getParamWithLayoutAnnotation_ForFragment

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
/**
 * There are more than one methods overloading in the name of "inflate()" in android.view.LayoutInflater.<br>
 * We only care about those having an param with `@LayoutRes` annotation,
 * for example {public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)}.<br>
 * This method will find out the resource param with an `@LayoutRes` annotation in String format, for example `R.layout.fragment_blank` .<br>
 * If no such param exists, <B>null</B> will be returned.
 */
private String getParamWithLayoutAnnotation_ForFragment(@NonNull JavaContext context, @NonNull MethodInvocation node) {
    Iterator<Expression> arguments = node.astArguments().iterator();
    Expression argument = arguments.next();

    JavaParser.ResolvedNode resolved = context.resolve(node);
    JavaParser.ResolvedMethod method = (JavaParser.ResolvedMethod) resolved;

    JavaParser.ResolvedAnnotation layoutParamAnnotation = method.getParameterAnnotation("android.support.annotation.LayoutRes", 0);
    if (layoutParamAnnotation != null) {
        return argument.toString();
    } else {
        return null;
    }

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

示例8: visitMethod

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
@Override
public void visitMethod(@NonNull JavaContext context, AstVisitor visitor, @NonNull MethodInvocation node) {
    JavaParser.ResolvedClass sorroundingClass = (JavaParser.ResolvedClass)context.resolve(JavaContext.findSurroundingClass(node));
    JavaParser.ResolvedMethod sorroundingMethod = (JavaParser.ResolvedMethod)context.resolve(JavaContext.findSurroundingMethod(node));

    if (sorroundingMethod.getName().equals("onCreateViewHolder")
            && sorroundingClass.isSubclassOf("android.support.v7.widget.RecyclerView.Adapter", false)){


        String layoutString = getParamWithLayoutAnnotation(context, node);
        if (layoutString == null){
            return;
        }

        if (!isFileStringStartWithPrefix(layoutString, "item_")) {
            context.report(ISSUE,
                    node,
                    context.getLocation(node),
                    "Layout resource file in ViewHolder must be named with prefix `item_`.");
        }

    }


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

示例9: getParamWithLayoutAnnotation

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
/**
 * There are more than one methods overloading in the name of "inflate()" in android.view.LayoutInflater.<br>
 * We only care about those having an param with `@LayoutRes` annotation,
 * for example {public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)}.<br>
 * This method will find out the resource param with an `@LayoutRes` annotation in String format, for example `R.layout.fragment_blank` .<br>
 * If no such param exists, <B>null</B> will be returned.
 */
private String getParamWithLayoutAnnotation(@NonNull JavaContext context, @NonNull MethodInvocation node) {
    Iterator<Expression> arguments = node.astArguments().iterator();
    Expression argument = arguments.next();

    JavaParser.ResolvedNode resolved = context.resolve(node);
    JavaParser.ResolvedMethod method = (JavaParser.ResolvedMethod) resolved;

    JavaParser.ResolvedAnnotation layoutParamAnnotation = method.getParameterAnnotation("android.support.annotation.LayoutRes", 0);
    if (layoutParamAnnotation != null) {
        return argument.toString();
    } else {
        return null;
    }

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

示例10: report

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
private static void report(JavaContext context, MethodInvocation node) {
    // Make sure the call is on a view
    JavaParser.ResolvedNode resolved = context.resolve(node);
    if (resolved instanceof JavaParser.ResolvedMethod) {
        JavaParser.ResolvedMethod method = (JavaParser.ResolvedMethod) resolved;
        JavaParser.ResolvedClass containingClass = method.getContainingClass();
        if (!containingClass.isSubclassOf(CLASS_VIEW, false)) {
            return;
        }
    }

    String name = node.astName().astValue();
    String suggestion = Character.toLowerCase(name.charAt(2)) + name.substring(3);
    String message = String.format(
            // Keep in sync with {@link #getOldValue} and {@link #getNewValue} below!
            "Suspicious method call; should probably call \"`%1$s`\" rather than \"`%2$s`\"",
            suggestion, name);
    context.report(ISSUE, node, context.getLocation(node.astName()), message);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:20,代码来源:WrongCallDetector.java

示例11: 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

示例12: visitMethod

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
@Override
public void visitMethod(@NonNull JavaContext context, @Nullable AstVisitor visitor,
        @NonNull MethodInvocation node) {
    ResolvedNode resolved = context.resolve(node);

    if (!(resolved instanceof ResolvedMethod) ||
            !((ResolvedMethod) resolved).getContainingClass()
                    .isSubclassOf(PACKAGE_MANAGER_CLASS, false)) {
        return;
    }
    StrictListAccessor<Expression, MethodInvocation> argumentList = node.astArguments();

    // Ignore if the method doesn't fit our description.
    if (argumentList != null && argumentList.size() == 2) {
        TypeDescriptor firstParameterType = context.getType(argumentList.first());
        if (firstParameterType != null
            && firstParameterType.matchesSignature(JavaParser.TYPE_STRING)) {
            maybeReportIssue(calculateValue(context, argumentList.last()), context, node);
        }
    }
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:22,代码来源:GetSignaturesDetector.java

示例13: isStringParameter

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
private static boolean isStringParameter(
        @NonNull Expression expression, @NonNull JavaContext context) {
    if (expression instanceof StringLiteral) {
        return true;
    } else {
        ResolvedNode resolvedNode = context.resolve(expression);
        if (resolvedNode instanceof ResolvedField) {
            if (((ResolvedField) resolvedNode).getValue() instanceof String) {
                return true;
            }
        }
    }
    return false;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:15,代码来源:GetSignaturesDetector.java

示例14: isInsideDialogFragment

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
private boolean isInsideDialogFragment(JavaContext context, MethodInvocation node) {
    Node parent = node.getParent();
    while (parent != null) {
        Object resolvedNode = context.resolve(parent);
        if (resolvedNode instanceof JavaParser.ResolvedMethod) {
            JavaParser.ResolvedMethod method = (JavaParser.ResolvedMethod) resolvedNode;
            if (isDialogFragment(method.getContainingClass())) {
                return true;
            }
        }
        parent = parent.getParent();
    }
    return false;
}
 
开发者ID:Piasy,项目名称:SafelyAndroid,代码行数:15,代码来源:UnsafeAndroidDetector.java

示例15: isMethodOnFragmentClass

import com.android.tools.lint.detector.api.JavaContext; //导入方法依赖的package包/类
private static boolean isMethodOnFragmentClass(
        @NonNull JavaContext context,
        @NonNull MethodInvocation call,
        @NonNull String fragmentClass,
        @NonNull String v4FragmentClass) {
    ResolvedNode resolved = context.resolve(call);
    if (resolved instanceof ResolvedMethod) {
        ResolvedClass containingClass = ((ResolvedMethod) resolved).getContainingClass();
        return containingClass.isSubclassOf(fragmentClass, false) ||
                containingClass.isSubclassOf(v4FragmentClass, false);
    }

    return false;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:15,代码来源:CleanupDetector.java


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