private static boolean checkRunWith(Symbol.TypeSymbol symbol, String... runnerClasses) { List<SymbolMetadata.AnnotationValue> annotationValues = symbol.metadata().valuesForAnnotation("org.junit.runner.RunWith"); if (annotationValues != null && annotationValues.size() == 1) { Object value = annotationValues.get(0).value(); if (value instanceof MemberSelectExpressionTree) { String runnerParam = ExpressionsHelper.concatenate((ExpressionTree) value); for (String runnerClass : runnerClasses) { if (runnerParam.endsWith(runnerClass)) { return true; } } } } return false; }
private static String getAnnotationAttributeAsString(List<AnnotationValue> values, String attributeName, String defaultValue) { return values.stream() .filter(annotationValue -> annotationValue.name().equals(attributeName)) .map(AnnotationValue::value) .filter(Tree.class::isInstance) .map(Tree.class::cast) .map(tree -> tree.is(Tree.Kind.MEMBER_SELECT) ? ((MemberSelectExpressionTree) tree).identifier() : tree) .filter(tree -> tree.is(Tree.Kind.IDENTIFIER)) .map(IdentifierTree.class::cast) .map(IdentifierTree::name) .findFirst() .orElse(defaultValue); }
private static boolean inheritRequestMethod(Symbol.TypeSymbol symbol) { List<SymbolMetadata.AnnotationValue> annotationValues = symbol.metadata().valuesForAnnotation(REQUEST_MAPPING_CLASS); if (annotationValues != null && annotationValues.stream().anyMatch(value -> REQUEST_METHOD.equals(value.name()))) { return true; } Type superClass = symbol.superClass(); if (superClass != null && inheritRequestMethod(superClass.symbol())) { return true; } for (Type type : symbol.interfaces()) { if (inheritRequestMethod(type.symbol())) { return true; } } return false; } }
private static boolean inheritRequestMethod(Symbol.TypeSymbol symbol) { List<SymbolMetadata.AnnotationValue> annotationValues = symbol.metadata().valuesForAnnotation(REQUEST_MAPPING_CLASS); if (annotationValues != null && annotationValues.stream().anyMatch(value -> REQUEST_METHOD.equals(value.name()))) { return true; } Type superClass = symbol.superClass(); if (superClass != null && inheritRequestMethod(superClass.symbol())) { return true; } for (Type type : symbol.interfaces()) { if (inheritRequestMethod(type.symbol())) { return true; } } return false; } }
private static boolean checkRunWith(Symbol.TypeSymbol symbol, String... runnerClasses) { List<SymbolMetadata.AnnotationValue> annotationValues = symbol.metadata().valuesForAnnotation("org.junit.runner.RunWith"); if (annotationValues != null && annotationValues.size() == 1) { Object value = annotationValues.get(0).value(); if (value instanceof MemberSelectExpressionTree) { String runnerParam = ExpressionsHelper.concatenate((ExpressionTree) value); for (String runnerClass : runnerClasses) { if (runnerParam.endsWith(runnerClass)) { return true; } } } } return false; }
private void addToScannedPackages(SymbolMetadata.AnnotationValue annotationValue) { if (COMPONENT_SCAN_ARGUMENTS.contains(annotationValue.name()) && annotationValue.value() instanceof ExpressionTree) { ExpressionTree values = (ExpressionTree) annotationValue.value(); if (values.is(Tree.Kind.STRING_LITERAL)) { String packageName = ConstantUtils.resolveAsStringConstant(values); packagesScannedBySpring.add(packageName); } else if (values.is(Tree.Kind.NEW_ARRAY)) { for (ExpressionTree p : ((NewArrayTree) values).initializers()) { String packageName = ConstantUtils.resolveAsStringConstant(p); packagesScannedBySpring.add(packageName); } } } }
private static boolean isNotRuntimeAnnotation(Type symbolType) { List<SymbolMetadata.AnnotationValue> valuesFor = symbolType.symbol().metadata().valuesForAnnotation("java.lang.annotation.Retention"); // default policy is CLASS if (valuesFor == null) { return true; } String retentionValue = getRetentionValue(valuesFor.get(0).value()); return !"RUNTIME".equals(retentionValue); }
private boolean hasSuppressWarningAnnotation(TypeJavaSymbol symbol) { List<SymbolMetadata.AnnotationValue> annotations = symbol.metadata().valuesForAnnotation("java.lang.SuppressWarnings"); if (annotations != null) { for (SymbolMetadata.AnnotationValue annotationValue : annotations) { if ("serial".equals(stringLiteralValue(annotationValue.value()))) { return true; } } } return false; }
private void addToScannedPackages(SymbolMetadata.AnnotationValue annotationValue) { if (COMPONENT_SCAN_ARGUMENTS.contains(annotationValue.name()) && annotationValue.value() instanceof ExpressionTree) { ExpressionTree values = (ExpressionTree) annotationValue.value(); if (values.is(Tree.Kind.STRING_LITERAL)) { String packageName = ConstantUtils.resolveAsStringConstant(values); packagesScannedBySpring.add(packageName); } else if (values.is(Tree.Kind.NEW_ARRAY)) { for (ExpressionTree p : ((NewArrayTree) values).initializers()) { String packageName = ConstantUtils.resolveAsStringConstant(p); packagesScannedBySpring.add(packageName); } } } }
private static boolean isScopeSingleton(SymbolMetadata clazzMeta) { List<AnnotationValue> values = clazzMeta.valuesForAnnotation(SCOPE_ANNOTATION_FQN); for (AnnotationValue annotationValue : values) { if (("value".equals(annotationValue.name()) || "scopeName".equals(annotationValue.name())) && annotationValue.value() instanceof LiteralTree && !"\"singleton\"".equals(((LiteralTree) annotationValue.value()).value())) { return false; } } return true; }
private static boolean checkAnnotationParameter(List<SymbolMetadata.AnnotationValue> valuesForAnnotation, String fieldName, String expectedValue) { return valuesForAnnotation.stream() .filter(annotationValue -> fieldName.equals(annotationValue.name())) .anyMatch(annotationValue -> isExpectedValue(annotationValue.value(), expectedValue)); }
private static boolean hasSuppressWarningAnnotation(TypeJavaSymbol symbol) { List<SymbolMetadata.AnnotationValue> annotations = symbol.metadata().valuesForAnnotation("java.lang.SuppressWarnings"); if (annotations != null) { for (SymbolMetadata.AnnotationValue annotationValue : annotations) { if ("serial".equals(stringLiteralValue(annotationValue.value()))) { return true; } } } return false; }
private static boolean checkAnnotationParameter(List<SymbolMetadata.AnnotationValue> valuesForAnnotation, String fieldName, String expectedValue) { return valuesForAnnotation.stream() .filter(annotationValue -> fieldName.equals(annotationValue.name())) .anyMatch(annotationValue -> isExpectedValue(annotationValue.value(), expectedValue)); }
private static String getAnnotationAttributeAsString(List<AnnotationValue> values, String attributeName, String defaultValue) { return values.stream() .filter(annotationValue -> annotationValue.name().equals(attributeName)) .map(AnnotationValue::value) .filter(Tree.class::isInstance) .map(Tree.class::cast) .map(tree -> tree.is(Tree.Kind.MEMBER_SELECT) ? ((MemberSelectExpressionTree) tree).identifier() : tree) .filter(tree -> tree.is(Tree.Kind.IDENTIFIER)) .map(IdentifierTree.class::cast) .map(IdentifierTree::name) .findFirst() .orElse(defaultValue); }
private static boolean hasSuppressWarningAnnotation(TypeJavaSymbol symbol) { List<SymbolMetadata.AnnotationValue> annotations = symbol.metadata().valuesForAnnotation("java.lang.SuppressWarnings"); if (annotations != null) { for (SymbolMetadata.AnnotationValue annotationValue : annotations) { if ("serial".equals(stringLiteralValue(annotationValue.value()))) { return true; } } } return false; }
private boolean isNotRuntimeAnnotation(Type symbolType) { List<SymbolMetadata.AnnotationValue> valuesFor = symbolType.symbol().metadata().valuesForAnnotation("java.lang.annotation.Retention"); // default policy is CLASS if (valuesFor == null) { return true; } String retentionValue = getRetentionValue(valuesFor.get(0).value()); return !"RUNTIME".equals(retentionValue); }
private static boolean expectAssertion(MethodTree methodTree) { List<SymbolMetadata.AnnotationValue> annotationValues = methodTree.symbol().metadata().valuesForAnnotation("org.junit.Test"); if (annotationValues != null) { for (SymbolMetadata.AnnotationValue annotationValue : annotationValues) { if ("expected".equals(annotationValue.name())) { return true; } } } return false; }
private static boolean expectAssertion(MethodTree methodTree) { List<SymbolMetadata.AnnotationValue> annotationValues = methodTree.symbol().metadata().valuesForAnnotation("org.junit.Test"); if (annotationValues != null) { for (SymbolMetadata.AnnotationValue annotationValue : annotationValues) { if ("expected".equals(annotationValue.name())) { return true; } } } return false; }