org.jetbrains.uast.UMethod Java Examples

The following examples show how to use org.jetbrains.uast.UMethod. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CallNeedsPermissionDetector.java    From PermissionsDispatcher with Apache License 2.0 6 votes vote down vote up
@Override
public boolean visitMethod(@NotNull UMethod node) {
    if (isGeneratedFiles(context)) {
        return super.visitMethod(node);
    }
    UAnnotation annotation = node.findAnnotation("permissions.dispatcher.NeedsPermission");
    if (annotation == null) {
        return super.visitMethod(node);
    }
    String methodIdentifier = methodIdentifier(node);
    if (methodIdentifier == null) {
        return super.visitMethod(node);
    }
    annotatedMethods.add(methodIdentifier);
    return super.visitMethod(node);
}
 
Example #2
Source File: PropertyDetector.java    From data-mediator with Apache License 2.0 6 votes vote down vote up
@Override
public void visitClass(UClass uClass) {
    //only check interface
    if(!uClass.isInterface()){
        return;
    }
    Set<PropInfo> infos = getPropInfoWithSupers(uClass);
    if(infos.isEmpty()){
        return;
    }
   //check method is relative of any field
    for(UMethod method: uClass.getMethods()){
        PsiModifierList list = method.getModifierList();
        PsiAnnotation pa_keep = list.findAnnotation(NAME_KEEP);
        PsiAnnotation pa_impl = list.findAnnotation(NAME_IMPL_METHOD);
        if (pa_keep == null && pa_impl == null) {
            if(!hasPropInfo(infos, method.getName())){
                report(method);
            }
        }
    }
}
 
Example #3
Source File: PropertyDetector.java    From data-mediator with Apache License 2.0 6 votes vote down vote up
@Override
public void visitClass(UClass uClass) {
    //only check interface
    if(!uClass.isInterface()){
        return;
    }
    Set<PropInfo> infos = getPropInfoWithSupers(uClass);
    if(infos.isEmpty()){
        return;
    }
   //check method is relative of any field
    for(UMethod method: uClass.getMethods()){
        PsiModifierList list = method.getModifierList();
        PsiAnnotation pa_keep = list.findAnnotation(NAME_KEEP);
        PsiAnnotation pa_impl = list.findAnnotation(NAME_IMPL_METHOD);
        if (pa_keep == null && pa_impl == null) {
            if(!hasPropInfo(infos, method.getName())){
                report(method);
            }
        }
    }
}
 
Example #4
Source File: InvalidConfigTypeDetector.java    From aircon with MIT License 6 votes vote down vote up
@Override
protected void visitConfigTypeAnnotation(final UAnnotation node, final UClass owner) {
	final UMethod defaultValueMethod = getDefaultValueMethod(owner);
	if (defaultValueMethod == null) {
		return;
	}

	final PsiType type = defaultValueMethod.getReturnType();
	if (isOneOfTypes(type, String.class, Float.class, Integer.class, Long.class, Boolean.class)) {
		return;
	}

	final String typeName = type.getCanonicalText();
	if (typeName.equals(PRIMITIVE_FLOAT) || typeName.equals(PRIMITIVE_INT) || typeName.equals(PRIMITIVE_LONG) || typeName.equals(PRIMITIVE_BOOLEAN)) {
		return;
	}

	log(typeName);

	reportPsi(owner.getNameIdentifier());
}
 
Example #5
Source File: MissingDefaultValueDetector.java    From aircon with MIT License 5 votes vote down vote up
@Override
public void visit(final UMethod node) {
	final UAnnotation defaultValueProviderAnnotation = ConfigElementsUtils.getDefaultValueProviderAnnotation(node);
	if (defaultValueProviderAnnotation == null) {
		return;
	}

	final PsiElement referencedConfig = defaultValueProviderAnnotation.findAttributeValue(null)
	                                                                  .getJavaPsi();
	final PsiField referencedField = ElementUtils.getReferencedField(referencedConfig);

	mConfigFieldsWithDefaultValues.add(referencedField);
}
 
Example #6
Source File: CallOnRequestPermissionsResultDetector.java    From PermissionsDispatcher with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visitMethod(UMethod node) {
    if (!hasRuntimePermissionAnnotation) {
        return true;
    }
    if (!"onRequestPermissionsResult".equals(node.getName())) {
        return true;
    }
    if (hasRuntimePermissionAnnotation && !isGeneratedMethodCalled(node, className, isKotlin)) {
        context.report(ISSUE, context.getLocation(node), "Generated onRequestPermissionsResult method not called");
    }
    return true;
}
 
Example #7
Source File: CallNeedsPermissionDetector.java    From PermissionsDispatcher with Apache License 2.0 5 votes vote down vote up
/**
 * Generate method identifier from method information.
 *
 * @param node UMethod
 * @return className + methodName + parametersType
 */
@Nullable
private static String methodIdentifier(@NotNull UMethod node) {
    UElement parent = node.getUastParent();
    if (!(parent instanceof UClass)) {
        return null;
    }
    UClass uClass = (UClass) parent;
    return uClass.getName() + COLON + node.getName();
}
 
Example #8
Source File: NoDelegateOnResumeDetector.java    From PermissionsDispatcher with Apache License 2.0 5 votes vote down vote up
/**
 * @return return true if visiting end (if lint does not visit inside the method), false otherwise
 */
@Override
public boolean visitMethod(UMethod node) {
    super.visitMethod(node);
    return !"onResume".equalsIgnoreCase(node.getName())
            || node.getReturnType() != PsiType.VOID
            || node.getUastParameters().size() != 0
            || !isPublicOrProtected(node);
}
 
Example #9
Source File: NoDelegateOnResumeDetector.java    From PermissionsDispatcher with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visitMethod(UMethod node) {
    super.visitMethod(node);
    if (node.findAnnotation("permissions.dispatcher.NeedsPermission") != null) {
        needPermissionMethodName = node.getName();
    }
    return true;
}
 
Example #10
Source File: PropertyDetector.java    From data-mediator with Apache License 2.0 5 votes vote down vote up
private void report(UMethod method){
    String prop = getProp(method.getName());
    context.report(ISSUE_PROP,
            context.getLocation(method.getPsi()), "this method '"+ method.getName()
                    +"' is not generate for any 'propName' value of @Field,\n you should either add annotation " +
                    "@Keep/@ImplMethod or add @Field(prop= \""+ prop+"\") for it.**");
}
 
Example #11
Source File: PropertyDetector.java    From data-mediator with Apache License 2.0 5 votes vote down vote up
private void report(UMethod method){
    String prop = getProp(method.getName());
    context.report(ISSUE_PROP,
            context.getLocation(method.getPsi()), "this method '"+ method.getName()
                    +"' is not generate for any 'propName' value of @Field,\n you should either add annotation " +
                    "@Keep/@ImplMethod or add @Field(prop= \""+ prop+"\") for it.**");
}
 
Example #12
Source File: InternalFolivoraApiDetector.java    From Folivora with Apache License 2.0 5 votes vote down vote up
@Override
public void visitMethod(@NotNull JavaContext context,
                        @NotNull UCallExpression call,
                        @NotNull PsiMethod method) {
  JavaEvaluator evaluator = context.getEvaluator();

  //check Folivora.applyDrawableToView() call
  String methodName = method.getName();
  if (!methodName.equals(APPLY_METHOD) || !evaluator.isMemberInClass(method, FOLIVORA_CLASS)) return;

  PsiClass viewClass = evaluator.findClass(VIEW_CLASS);
  PsiClass currentClass = UastUtils.getContainingClass(call);
  if(currentClass == null || viewClass == null) return;
  if (!currentClass.isInheritor(viewClass, true/*deep check*/)) {
    report(context, call);
    return;
  }

  UMethod uMethod = UastUtils.getParentOfType(call, UMethod.class, false);
  if(uMethod == null) return;

  //check it is a view's constructor
  if (!uMethod.isConstructor()) {
    report(context, call);
    return;
  }

  MethodSignature signature = uMethod.getSignature(PsiSubstitutor.EMPTY);
  PsiType[] types = signature.getParameterTypes();
  if (types.length != 2) {
    report(context, call);
    return;
  }

  if (!types[0].equalsToText(CONTEXT_CLASS)
    || !types[1].equalsToText(ATTRS_CLASS)) {
    report(context, call);
  }
}
 
Example #13
Source File: InstalledBeforeSuperDetector.java    From Folivora with Apache License 2.0 5 votes vote down vote up
@Override
public void visitMethod(JavaContext context,
                        UCallExpression call,
                        PsiMethod method) {
  JavaEvaluator evaluator = context.getEvaluator();

  //check Folivora.installViewFactory() call
  String methodName = method.getName();
  if (!methodName.equals(INSTALL_METHOD) || !evaluator.isMemberInClass(method, TYPE_FOLIVORA))
    return;

  //check current class is decent of AppCompatActivity
  PsiClass legacyCompatActClass = evaluator.findClass(LEGACY_COMPAT_ACTIVITY);
  PsiClass compatActClass = evaluator.findClass(COMPAT_ACTIVITY);
  PsiClass c = UastUtils.getContainingClass(call);
  boolean isAppCompatActivity = false;
  if (c != null) {
    isAppCompatActivity = (legacyCompatActClass != null && c.isInheritor(legacyCompatActClass,
      true/*deep check*/))
      || compatActClass != null && c.isInheritor(compatActClass, true/*deep check*/);
  }
  if (!isAppCompatActivity) return;

  //check current method is onCreate
  @SuppressWarnings("unchecked")
  UMethod uMethod = UastUtils.getParentOfType(call, true, UMethod.class);
  if (uMethod == null || !"onCreate".equals(uMethod.getName())) return;

  SuperOnCreateFinder finder = new SuperOnCreateFinder(call);
  uMethod.accept(finder);
  if (!finder.isSuperOnCreateCalled()) {
    context.report(ISSUE, call, context.getLocation(call),
      "calling `Folivora.installViewFactory()` before super.onCreate can cause AppCompatViews unavailable");
  }
}
 
Example #14
Source File: ElementUtils.java    From aircon with MIT License 5 votes vote down vote up
public static UMethod[] getConstructors(final UClass node) {
	final List<UMethod> constructors = new ArrayList<>();
	final UMethod[] methods = node.getMethods();
	for (UMethod method : methods) {
		if (method.isConstructor()) {
			constructors.add(method);
		}
	}
	return constructors.toArray(new UMethod[0]);
}
 
Example #15
Source File: ElementUtils.java    From aircon with MIT License 5 votes vote down vote up
public static JvmModifier getVisibilityModifier(UMethod method) {
	for (JvmModifier jvmModifier : method.getModifiers()) {
		switch (jvmModifier) {
			case PUBLIC:
			case PACKAGE_LOCAL:
			case PROTECTED:
			case PRIVATE:
				return jvmModifier;
		}
	}
	return method.getContainingClass()
	             .isInterface() ? JvmModifier.PUBLIC : JvmModifier.PACKAGE_LOCAL;
}
 
Example #16
Source File: ConfigElementsUtils.java    From aircon with MIT License 5 votes vote down vote up
public static UAnnotation getDefaultValueProviderAnnotation(UMethod method) {
	for (UAnnotation annotation : ((UAnnotated) method).getAnnotations()) {
		if (ElementUtils.isOfType(annotation.getJavaPsi(), ConfigDefaultValueProvider.class)) {
			return annotation;
		}
	}
	return null;
}
 
Example #17
Source File: ConfigTypeAnnotationIssueDetector.java    From aircon with MIT License 5 votes vote down vote up
protected UMethod getDefaultValueMethod(UClass owner) {
	for (final UMethod method : owner.getMethods()) {
		if (method.getName()
		          .equals(ATTRIBUTE_DEFAULT_VALUE)) {
			return method;
		}
	}
	return null;
}
 
Example #18
Source File: AirConUsageDetector.java    From aircon with MIT License 5 votes vote down vote up
@Override
public boolean visitMethod(@NotNull final UMethod node) {
	for (IssueDetector issueDetector : mIssueDetectors) {
		issueDetector.visit(node);
	}
	return super.visitMethod(node);
}
 
Example #19
Source File: NonMatchingConfigResolverDetector.java    From aircon with MIT License 5 votes vote down vote up
@Override
protected void visitConfigTypeAnnotation(final UAnnotation node, final UClass owner) {
	final PsiClass resolverClass = ((PsiImmediateClassType) node.getAttributeValues()
	                                                            .get(0)
	                                                            .getExpression()
	                                                            .evaluate()).resolve();

	final PsiClassType resolverClassType = getConfigTypeResolverClassType(resolverClass);
	if (resolverClassType == null) {
		return;
	}

	final PsiType annotationType = getAnnotationType(resolverClassType);
	final PsiType rawType = getRawType(resolverClassType);

	if (!annotationType.getCanonicalText()
	                   .equals(owner.getQualifiedName())) {
		report(node);
		return;
	}

	final UMethod defaultValueMethod = getDefaultValueMethod(owner);
	if (defaultValueMethod == null) {
		return;
	}

	if (!rawType.equals(defaultValueMethod.getReturnType())) {
		report(node);
	}
}
 
Example #20
Source File: AuxMethodDetector.java    From aircon with MIT License 5 votes vote down vote up
@Override
public void visit(final UMethod node) {
	if (!ConfigElementsUtils.isConfigAuxMethod(node)) {
		return;
	}
	visitAuxMethod(node);
}
 
Example #21
Source File: ConfigValidatorDetector.java    From aircon with MIT License 5 votes vote down vote up
@Override
public void visit(final UMethod node) {
	if (!ElementUtils.hasAnnotation(node, ConfigValidator.class)) {
		return;
	}

	visitConfigValidatorMethod(node);
}
 
Example #22
Source File: ConfigValidatorInvalidSignature.java    From aircon with MIT License 5 votes vote down vote up
@Override
protected void visitConfigValidatorMethod(final UMethod node) {
	final PsiType returnType = node.getReturnType();
	if (!ElementUtils.isBoolean(returnType)) {
		report(node, getFix(node));
	}
}
 
Example #23
Source File: AuxMethodInvalidSignatureDetector.java    From aircon with MIT License 5 votes vote down vote up
private LintFix getFix(final UMethod node, final boolean staticMethod, JvmModifier visibilityModifier) {
	final MethodFixBuilder methodFixBuilder = new MethodFixBuilder(mContext, node, "Change to " + getModifierName(visibilityModifier) + " static method").setVisibility(visibilityModifier);
	if (!staticMethod) {
		methodFixBuilder.addModifier(JvmModifier.STATIC);
	}
	return methodFixBuilder.build();
}
 
Example #24
Source File: AuxMethodInvalidSignatureDetector.java    From aircon with MIT License 5 votes vote down vote up
private LintFix getFix(final UMethod node, final boolean staticMethod) {
	final LintFix.GroupBuilder builder = LintFix.create()
	                                            .group();
	builder.add(getFix(node, staticMethod, JvmModifier.PACKAGE_LOCAL));
	builder.add(getFix(node, staticMethod, JvmModifier.PUBLIC));
	return builder.build();
}
 
Example #25
Source File: AuxMethodInvalidSignatureDetector.java    From aircon with MIT License 5 votes vote down vote up
@Override
protected void visitAuxMethod(final UMethod node) {
	final JvmModifier visibilityModifier = ElementUtils.getVisibilityModifier(node);
	final boolean staticMethod = ElementUtils.isStatic(node);
	if (visibilityModifier.equals(JvmModifier.PRIVATE) || !staticMethod) {
		report(node, getFix(node, staticMethod));
	}
}
 
Example #26
Source File: CallOnRequestPermissionsResultDetector.java    From PermissionsDispatcher with Apache License 2.0 4 votes vote down vote up
private static boolean isGeneratedMethodCalled(UMethod method, String className, boolean isKotlin) {
    UExpression methodBody = method.getUastBody();
    if (methodBody == null) {
        return false;
    }
    if (methodBody instanceof UBlockExpression) {
        UBlockExpression methodBodyExpression = (UBlockExpression) methodBody;
        List<UExpression> expressions = methodBodyExpression.getExpressions();
        for (UExpression expression : expressions) {
            if (isKotlin && expression instanceof KotlinUFunctionCallExpression) {
                KotlinUFunctionCallExpression functionalExpression = (KotlinUFunctionCallExpression) expression;
                if ("onRequestPermissionsResult".equals(functionalExpression.getMethodName())) {
                    return true;
                }
            }

            if (!(expression instanceof UQualifiedReferenceExpression)) {
                continue;
            }

            UQualifiedReferenceExpression referenceExpression = (UQualifiedReferenceExpression) expression;
            UExpression receiverExpression = referenceExpression.getReceiver();
            PsiElement receiverPsi = receiverExpression.getPsi();
            if (receiverPsi == null) {
                continue; // can this case be happened?
            }
            String receiverName = receiverPsi.getText();
            if ("super".equals(receiverName)) {
                // skip super method call
                continue;
            }

            if (isKotlin && referenceExpression instanceof KotlinUQualifiedReferenceExpression) {
                if ("onRequestPermissionsResult".equals(referenceExpression.getResolvedName())) {
                    return true;
                }
            } else {
                String targetClassName = className + "PermissionsDispatcher";
                if (targetClassName.equals(receiverName) && "onRequestPermissionsResult".equals(referenceExpression.getResolvedName())) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example #27
Source File: IssueDetector.java    From aircon with MIT License 4 votes vote down vote up
public void visit(final UMethod node) {
	// No-op, to be overridden
}
 
Example #28
Source File: NoDelegateOnResumeDetector.java    From PermissionsDispatcher with Apache License 2.0 4 votes vote down vote up
private boolean isPublicOrProtected(UMethod node) {
    UastVisibility visibility = node.getVisibility();
    return visibility == UastVisibility.PUBLIC || visibility == UastVisibility.PROTECTED;
}
 
Example #29
Source File: ConfigMockProtectionDetector.java    From aircon with MIT License 4 votes vote down vote up
@Override
protected void visitAuxMethod(final UMethod node) {
	if (ElementUtils.hasAnnotation(node, ConfigMock.class)) {
		report(node);
	}
}
 
Example #30
Source File: ConfigValidatorInvalidSignature.java    From aircon with MIT License 4 votes vote down vote up
private LintFix getFix(final UMethod node) {
	return new MethodFixBuilder(mContext, node, "Change return type to boolean").setReturnType("boolean")
	                                                                            .build();
}