org.jetbrains.uast.UClass Java Examples

The following examples show how to use org.jetbrains.uast.UClass. 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: 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 #2
Source File: InvalidR2UsageDetector.java    From butterknife with Apache License 2.0 6 votes vote down vote up
private static void detectR2(JavaContext context, UElement node) {
  UFile sourceFile = context.getUastFile();
  List<UClass> classes = sourceFile.getClasses();
  if (!classes.isEmpty() && classes.get(0).getName() != null) {
    String qualifiedName = classes.get(0).getName();
    if (qualifiedName.contains("_ViewBinder")
        || qualifiedName.contains("_ViewBinding")
        || qualifiedName.equals(R2)) {
      // skip generated files and R2
      return;
    }
  }
  boolean isR2 = isR2Expression(node);
  if (isR2 && !context.isSuppressedWithComment(node, ISSUE)) {
    context.report(ISSUE, node, context.getLocation(node), LINT_ERROR_BODY);
  }
}
 
Example #3
Source File: CallNeedsPermissionDetector.java    From PermissionsDispatcher with Apache License 2.0 6 votes vote down vote up
/**
 * Generate method identifier from method information.
 *
 * @param node UCallExpression
 * @return className + methodName + parametersType
 */
@Nullable
private static String methodIdentifier(@NotNull UCallExpression node) {
    UElement element = node.getUastParent();
    while (element != null) {
        if (element instanceof UClass) {
            break;
        }
        element = element.getUastParent();
    }
    UClass uClass = (UClass) element;
    if (uClass == null || node.getMethodName() == null) {
        return null;
    }
    return uClass.getName() + COLON + node.getMethodName();
}
 
Example #4
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 #5
Source File: NonFieldTargetDetector.java    From aircon with MIT License 6 votes vote down vote up
@Override
protected void visitConfigTypeAnnotation(final UAnnotation node, final UClass owner) {
	final List<UAnnotation> annotations = ((UAnnotated) owner).getAnnotations();
	for (UAnnotation annotation : annotations) {
		if (!isTargetAnnotation(annotation)) {
			continue;
		}

		final ElementType[] annotationTargets = getTargetValue(annotation);
		if (annotationTargets.length == 1 && annotationTargets[0] == ElementType.FIELD) {
			return;
		}
	}

	reportPsi(owner.getNameIdentifier());
}
 
Example #6
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 #7
Source File: WrongRetentionDetector.java    From aircon with MIT License 6 votes vote down vote up
@Override
protected void visitConfigTypeAnnotation(final UAnnotation node, final UClass owner) {
	final List<UAnnotation> annotations = ((UAnnotated) owner).getAnnotations();
	for (UAnnotation annotation : annotations) {
		if (!isRetentionAnnotation(annotation)) {
			continue;
		}

		final RetentionPolicy retentionPolicy = getRetentionPolicyValue(annotation);
		if (retentionPolicy == RetentionPolicy.RUNTIME) {
			return;
		}
	}

	reportPsi(owner.getNameIdentifier());
}
 
Example #8
Source File: WrongRetentionDetector.java    From dagger-reflect with Apache License 2.0 6 votes vote down vote up
private static void reportMissingRetention(
    @NotNull JavaContext context,
    boolean isKotlin,
    @NotNull UClass node,
    @NotNull UAnnotation reflectRelatedAnnotation) {
  context.report(
      ISSUE_WRONG_RETENTION,
      node,
      context.getNameLocation(node),
      "Annotation used by Dagger Reflect must be annotated with `@Retention(RUNTIME)`.",
      LintFix.create()
          .replace()
          .name("Add: `@Retention(RUNTIME)`")
          .range(context.getLocation(reflectRelatedAnnotation))
          .beginning()
          .with(isKotlin ? FIX_ANNOTATION_RETENTION_KOTLIN : FIX_ANNOTATION_RETENTION_JAVA)
          .reformat(true)
          .shortenNames()
          .build());
}
 
Example #9
Source File: NoCorrespondingNeedsPermissionDetector.java    From PermissionsDispatcher with Apache License 2.0 5 votes vote down vote up
@Override
public void afterVisitClass(UClass node) {
    // If there is OnShowRationale, find corresponding NeedsPermission
    for (UAnnotation onShowRationaleAnnotation : onShowRationaleAnnotations) {
        boolean found = false;
        for (UAnnotation needsPermissionAnnotation : needsPermissionAnnotations) {
            if (hasSameNodes(onShowRationaleAnnotation.getAttributeValues(), needsPermissionAnnotation.getAttributeValues())) {
                found = true;
            }
        }
        if (!found) {
            context.report(ISSUE, context.getLocation(onShowRationaleAnnotation), "Useless @OnShowRationale declaration");
        }
    }
}
 
Example #10
Source File: AbstractDetector.java    From aircon with MIT License 5 votes vote down vote up
@Override
public UElementHandler createUastHandler(@Nonnull final JavaContext context) {
	return new UElementHandler() {
		@Override
		public void visitClass(@Nonnull UClass node) {
			final UastVisitor uastVisitor = getUastVisitor(context);
			node.accept(uastVisitor);
			uastVisitor.onClassVisited(node);
		}
	};
}
 
Example #11
Source File: DialogExtendLintDetector.java    From SimpleDialogFragments with Apache License 2.0 5 votes vote down vote up
@Override
public void visitClass(JavaContext context, UClass declaration) {
    PsiModifierList classModifiers = declaration.getModifierList();
    if (classModifiers == null || !classModifiers.hasModifierProperty("abstract")) {
        // check for static build method
        boolean hasBuildMethod = false;
        for (PsiMethod method : declaration.getMethods()) {
            if ("build".equals(method.getName()) && method.getModifierList()
                    .hasModifierProperty("static")) {
                hasBuildMethod = true;
                break;
            }
        }
        if (!hasBuildMethod){
            context.report(BUILD_OVERWRITE, context.getLocation(declaration.getExtendsList()),
                    BUILD_OVERWRITE_MESSAGE);
        }

        // check for public static String TAG
        boolean hasTag = false;
        for (UField field : declaration.getFields()) {
            PsiModifierList modifiers = field.getModifierList();
            if ("TAG".equals(field.getName()) && LintUtils.isString(field.getType()) &&
                    modifiers != null && modifiers.hasModifierProperty("public") &&
                    modifiers.hasModifierProperty("static")) {
                hasTag = true;
                break;
            }
        }
        if (!hasTag) {
            context.report(TAG, context.getLocation(declaration.getExtendsList()), TAG_MESSAGE);
        }

    }
}
 
Example #12
Source File: DialogExtendLintDetector.java    From SimpleDialogFragments with Apache License 2.0 5 votes vote down vote up
@Override
public void visitClass(JavaContext context, UClass declaration) {
    PsiModifierList classModifiers = declaration.getModifierList();
    if (classModifiers == null || !classModifiers.hasModifierProperty("abstract")) {
        // check for static build method
        boolean hasBuildMethod = false;
        for (PsiMethod method : declaration.getMethods()) {
            if ("build".equals(method.getName()) && method.getModifierList()
                    .hasModifierProperty("static")) {
                hasBuildMethod = true;
                break;
            }
        }
        if (!hasBuildMethod){
            context.report(BUILD_OVERWRITE, context.getLocation(declaration.getExtendsList()),
                    BUILD_OVERWRITE_MESSAGE);
        }

        // check for public static String TAG
        boolean hasTag = false;
        for (UField field : declaration.getFields()) {
            PsiModifierList modifiers = field.getModifierList();
            if ("TAG".equals(field.getName()) && LintUtils.isString(field.getType()) &&
                    modifiers != null && modifiers.hasModifierProperty("public") &&
                    modifiers.hasModifierProperty("static")) {
                hasTag = true;
                break;
            }
        }
        if (!hasTag) {
            context.report(TAG, context.getLocation(declaration.getExtendsList()), TAG_MESSAGE);
        }

    }
}
 
Example #13
Source File: InvalidSQLiteOperatorUsageDetector.java    From HighLite with Apache License 2.0 5 votes vote down vote up
@Override
public UElementHandler createUastHandler(final JavaContext context) {
    return new UElementHandler() {
        @Override
        public void visitClass(UClass uClass) {
            uClass.accept(new FromMethodVisitor(context));
        }
    };
}
 
Example #14
Source File: NoDelegateOnResumeDetector.java    From PermissionsDispatcher with Apache License 2.0 5 votes vote down vote up
@Override
public UElementHandler createUastHandler(final JavaContext context) {
    return new UElementHandler() {
        @Override
        public void visitClass(UClass node) {
            if (node.findAnnotation("permissions.dispatcher.RuntimePermissions") != null) {
                node.accept(new Checker(context));
            }
        }
    };
}
 
Example #15
Source File: NoDelegateOnResumeDetector.java    From PermissionsDispatcher with Apache License 2.0 5 votes vote down vote up
@Override
public void afterVisitClass(UClass node) {
    super.afterVisitClass(node);
    if (needPermissionMethodName != null) {
        node.accept(new OnResumeChecker(context, node, needPermissionMethodName));
    }
}
 
Example #16
Source File: NoCorrespondingNeedsPermissionDetector.java    From PermissionsDispatcher with Apache License 2.0 5 votes vote down vote up
@Override
public UElementHandler createUastHandler(final JavaContext context) {
    return new UElementHandler() {
        @Override public void visitClass(UClass node) {
            node.accept(new AnnotationChecker(context));
        }
    };
}
 
Example #17
Source File: AirConUsageDetector.java    From aircon with MIT License 5 votes vote down vote up
@Override
public boolean visitClass(@Nonnull final UClass node) {
	for (IssueDetector issueDetector : mIssueDetectors) {
		issueDetector.visit(node);
	}
	return super.visitClass(node);
}
 
Example #18
Source File: CallNeedsPermissionDetector.java    From PermissionsDispatcher with Apache License 2.0 5 votes vote down vote up
@Override
public UElementHandler createUastHandler(@NotNull final JavaContext context) {
    return new UElementHandler() {
        @Override
        public void visitClass(@NotNull UClass node) {
            node.accept(new AnnotationChecker(context));
        }
    };
}
 
Example #19
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 #20
Source File: CallNeedsPermissionDetector.java    From PermissionsDispatcher with Apache License 2.0 5 votes vote down vote up
private static boolean isGeneratedFiles(JavaContext context) {
    UFile sourceFile = context.getUastFile();
    if (sourceFile == null) {
        return false;
    }
    List<UClass> classes = sourceFile.getClasses();
    if (classes.isEmpty()) {
        return false;
    }
    String qualifiedName = classes.get(0).getName();
    return qualifiedName != null && qualifiedName.contains("PermissionsDispatcher");
}
 
Example #21
Source File: CallOnRequestPermissionsResultDetector.java    From PermissionsDispatcher with Apache License 2.0 5 votes vote down vote up
@Override
public UElementHandler createUastHandler(final JavaContext context) {
    return new UElementHandler() {
        @Override
        public void visitClass(UClass node) {
            node.accept(new OnRequestPermissionsResultChecker(context, node));
        }
    };
}
 
Example #22
Source File: InjectedFieldInJobNotTransientDetector.java    From cathode with Apache License 2.0 5 votes vote down vote up
@Override public UElementHandler createUastHandler(final JavaContext context) {
  return new UElementHandler() {
    @Override public void visitClass(UClass node) {
      node.accept(new JobVisitor(context));
    }
  };
}
 
Example #23
Source File: InvalidR2UsageDetector.java    From butterknife with Apache License 2.0 5 votes vote down vote up
@Override public UElementHandler createUastHandler(final JavaContext context) {
  return new UElementHandler() {
    @Override public void visitClass(UClass node) {
      node.accept(new R2UsageVisitor(context));
    }
  };
}
 
Example #24
Source File: WrongRetentionDetector.java    From dagger-reflect with Apache License 2.0 5 votes vote down vote up
@Override
public UElementHandler createUastHandler(@NotNull JavaContext context) {
  return new UElementHandler() {
    @Override
    public void visitClass(@NotNull UClass node) {
      if (!node.isAnnotationType()) {
        return;
      }

      final UAnnotation qualifierAnnotation = node.findAnnotation(ANNOTATION_QUALIFIER);
      final UAnnotation mapKeyAnnotation = node.findAnnotation(ANNOTATION_MAP_KEY);
      if (qualifierAnnotation == null && mapKeyAnnotation == null) {
        return;
      }

      final boolean isKotlin = Lint.isKotlin(node);
      final UAnnotation retentionAnnotation =
          node.findAnnotation(isKotlin ? ANNOTATION_RETENTION_KOTLIN : ANNOTATION_RETENTION_JAVA);
      if (retentionAnnotation == null) {
        final UAnnotation reflectRelatedAnnotation =
            qualifierAnnotation != null ? qualifierAnnotation : mapKeyAnnotation;
        reportMissingRetention(context, isKotlin, node, reflectRelatedAnnotation);
      } else {
        final String retentionPolicy = getRetentionPolicy(context, isKotlin, retentionAnnotation);
        if (!"RUNTIME".equals(retentionPolicy)) {
          reportWrongRetentionType(context, isKotlin, retentionAnnotation, retentionPolicy);
        }
      }
    }
  };
}
 
Example #25
Source File: MissingDefaultValueDetector.java    From aircon with MIT License 5 votes vote down vote up
@Override
public void onClassVisited(final UClass node) {
	if (ConfigElementsUtils.hasFeatureRemoteConfigAnnotation(node)) {
		for (PsiField configElement : mConfigElements) {
			if (!containsConfig(configElement.getName(), mConfigFieldsWithDefaultValues)) {
				reportPsi(configElement);
			}
		}

		mConfigFieldsWithDefaultValues.clear();
		mConfigElements.clear();
	}
}
 
Example #26
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 #27
Source File: DuplicateFeatureRemoteDetector.java    From aircon with MIT License 5 votes vote down vote up
@Override
public void visit(final UClass node) {
	if (!ConfigElementsUtils.hasFeatureRemoteConfigAnnotation(node)) {
		return;
	}

	final UClass featureRemoteConfig = mFeatureRemoteConfigs.get(node.getName());
	if (isAlreadyDefined(node, featureRemoteConfig)) {
		reportPsi(node.getNameIdentifier(), String.format(DESCRIPTION_FORMAT, featureRemoteConfig.getQualifiedName()));
		return;
	}

	mFeatureRemoteConfigs.put(node.getName(), node);
}
 
Example #28
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 #29
Source File: ElementUtils.java    From aircon with MIT License 5 votes vote down vote up
public static UClass getContainingClass(UElement element) {
	if (element instanceof UClass) {
		return (UClass) element;
	}
	if (element == null) {
		return null;
	}

	return getContainingClass(element.getUastParent());
}
 
Example #30
Source File: ConfigTypeAnnotationIssueDetector.java    From aircon with MIT License 5 votes vote down vote up
@Override
public final void visit(final UAnnotation node) {
	if (!ElementUtils.isOfType(node.getJavaPsi(), ConfigType.class)) {
		return;
	}

	visitConfigTypeAnnotation(node, (UClass) node.getUastParent());
}