本文整理汇总了Java中com.google.errorprone.VisitorState.getSymbolFromString方法的典型用法代码示例。如果您正苦于以下问题:Java VisitorState.getSymbolFromString方法的具体用法?Java VisitorState.getSymbolFromString怎么用?Java VisitorState.getSymbolFromString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.errorprone.VisitorState
的用法示例。
在下文中一共展示了VisitorState.getSymbolFromString方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: matches
import com.google.errorprone.VisitorState; //导入方法依赖的package包/类
@Override
public boolean matches(ExpressionTree t, VisitorState state) {
Type type = ((JCTree) t).type;
// Expect a class type.
if (!(type instanceof ClassType)) {
return false;
}
// Expect one type argument, the type of the JUnit class runner to use.
com.sun.tools.javac.util.List<Type> typeArgs = ((ClassType) type).getTypeArguments();
if (typeArgs.size() != 1) {
return false;
}
Type runnerType = typeArgs.get(0);
for (String testRunner : TEST_RUNNERS) {
Symbol parent = state.getSymbolFromString(testRunner);
if (parent == null) {
continue;
}
if (runnerType.tsym.isSubClass(parent, state.getTypes())) {
return true;
}
}
return false;
}
示例2: linkedListFix
import com.google.errorprone.VisitorState; //导入方法依赖的package包/类
private static Optional<Fix> linkedListFix(Tree tree, VisitorState state) {
Type type = getTargetType(state);
if (type == null) {
return Optional.empty();
}
Types types = state.getTypes();
for (String replacement : ImmutableList.of("java.util.ArrayList", "java.util.ArrayDeque")) {
Symbol sym = state.getSymbolFromString(replacement);
if (sym == null) {
continue;
}
if (types.isAssignable(types.erasure(sym.asType()), types.erasure(type))) {
SuggestedFix.Builder fix = SuggestedFix.builder();
while (tree instanceof ParameterizedTypeTree) {
tree = ((ParameterizedTypeTree) tree).getType();
}
fix.replace(tree, SuggestedFixes.qualifyType(state, fix, sym));
return Optional.of(fix.build());
}
}
return Optional.empty();
}
示例3: matches
import com.google.errorprone.VisitorState; //导入方法依赖的package包/类
@Override
public boolean matches(Tree tree, VisitorState state) {
Symbol sym = state.getSymbolFromString(clazz);
if (sym == null) {
return false;
}
Type type = ASTHelpers.getType(tree);
if (!ASTHelpers.isSubtype(type, sym.type, state)) {
return false;
}
Types types = state.getTypes();
Type superType = types.asSuper(type, sym);
if (superType == null) {
return false;
}
List<Type> typeArguments = superType.getTypeArguments();
if (typeArguments.isEmpty()) {
return false;
}
return ASTHelpers.isSameType(
typeArguments.get(typeArgumentIndex), state.getTypeFromString(URL_CLASS), state);
}
示例4: hasAnnotation
import com.google.errorprone.VisitorState; //导入方法依赖的package包/类
/**
* Determines whether an expression has an annotation of the given type.
* This includes annotations inherited from superclasses due to @Inherited.
*
* @param annotationType The type of the annotation to look for (e.g, "javax.annotation.Nullable")
*/
public static <T extends Tree> Matcher<T> hasAnnotation(final String annotationType) {
return new Matcher<T>() {
@Override
public boolean matches (T tree, VisitorState state) {
Symbol sym = ASTHelpers.getSymbol(tree);
Symbol annotationSym = state.getSymbolFromString(annotationType);
Symbol inheritedSym = state.getSymtab().inheritedType.tsym;
if ((sym == null) || (annotationSym == null)) {
return false;
}
if ((sym instanceof ClassSymbol) && (annotationSym.attribute(inheritedSym) != null)) {
while (sym != null) {
if (sym.attribute(annotationSym) != null) {
return true;
}
sym = ((ClassSymbol) sym).getSuperclass().tsym;
}
return false;
} else {
return sym.attribute(annotationSym) != null;
}
}
};
}
示例5: predicateType
import com.google.errorprone.VisitorState; //导入方法依赖的package包/类
private static Type predicateType(Type type, VisitorState state) {
Symbol predicate = state.getSymbolFromString(java.util.function.Predicate.class.getName());
if (predicate == null) {
return null;
}
Type asPredicate = state.getTypes().asSuper(type, predicate);
if (asPredicate == null) {
return null;
}
return getOnlyElement(asPredicate.getTypeArguments(), null);
}
示例6: matchClass
import com.google.errorprone.VisitorState; //导入方法依赖的package包/类
@Override
public Description matchClass(ClassTree classTree, VisitorState visitorState) {
Symbol throwableClass = visitorState.getSymbolFromString("java.lang.Throwable");
if (Objects.equals(ASTHelpers.getSymbol(classTree.getExtendsClause()), throwableClass)) {
Optional<? extends Tree> methodTree =
classTree
.getMembers()
.stream()
.filter(
m ->
m instanceof MethodTree
&& ((MethodTree) m).getName().contentEquals("toString"))
.findFirst();
if (methodTree.isPresent()) {
SuggestedFix.Builder builder = SuggestedFix.builder();
MethodTree tree = (MethodTree) methodTree.get();
if (!tree.getParameters().isEmpty()) {
return Description.NO_MATCH;
}
String newTree =
tree.getModifiers().toString().replaceAll("@Override[(][)]", "@Override")
+ "String getMessage()\n"
+ visitorState.getSourceForNode(tree.getBody());
builder.replace(tree, newTree);
return describeMatch(classTree, builder.build());
}
}
return Description.NO_MATCH;
}
示例7: isRWLock
import com.google.errorprone.VisitorState; //导入方法依赖的package包/类
/**
* Returns true if the lock expression corresponds to a {@code
* java.util.concurrent.locks.ReadWriteLock}.
*/
private static boolean isRWLock(GuardedByExpression guard, VisitorState state) {
Type guardType = guard.type();
if (guardType == null) {
return false;
}
Symbol rwLockSymbol = state.getSymbolFromString(JUC_READ_WRITE_LOCK);
if (rwLockSymbol == null) {
return false;
}
return state.getTypes().isSubtype(guardType, rwLockSymbol.type);
}
示例8: matchCompilationUnit
import com.google.errorprone.VisitorState; //导入方法依赖的package包/类
@Override
public Description matchCompilationUnit(CompilationUnitTree tree, final VisitorState state) {
Symbol mockitoSym = state.getSymbolFromString(MOCKITO_CLASS);
if (mockitoSym == null) {
// fast path if mockito isn't being used
return Description.NO_MATCH;
}
// collect variable symbols for standard Answer constants that don't support generics
final Set<Symbol> badAnswers = new LinkedHashSet<>();
for (Symbol member : mockitoSym.members().getSymbols(LookupKind.NON_RECURSIVE)) {
if (member.getKind() != ElementKind.FIELD) {
continue;
}
if (BAD_ANSWER_STRATEGIES.contains(member.getSimpleName().toString())) {
badAnswers.add(member);
}
}
// collect mocks that are initialized in this compilation unit using a bad answer strategy
final Set<VarSymbol> mockVariables = MockInitializationScanner.scan(state, badAnswers);
// check for when(...) calls on mocks using a bad answer strategy
new WhenNeedsCastScanner(mockVariables, state).scan(state.getPath(), null);
// errors are reported in WhenNeedsCastScanner
return Description.NO_MATCH;
}
示例9: hasAttribute
import com.google.errorprone.VisitorState; //导入方法依赖的package包/类
private static boolean hasAttribute(Symbol symbol, String name, VisitorState state) {
Symbol annotation = state.getSymbolFromString(name);
// If we can't look up the annotation in the current VisitorState, then presumably it couldn't
// be present on a Symbol we're inspecting.
return annotation != null && symbol.attribute(annotation) != null;
}