本文整理汇总了Java中lombok.ast.ForwardingAstVisitor类的典型用法代码示例。如果您正苦于以下问题:Java ForwardingAstVisitor类的具体用法?Java ForwardingAstVisitor怎么用?Java ForwardingAstVisitor使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ForwardingAstVisitor类属于lombok.ast包,在下文中一共展示了ForwardingAstVisitor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createJavaVisitor
import lombok.ast.ForwardingAstVisitor; //导入依赖的package包/类
@Override
public AstVisitor createJavaVisitor(final @NonNull JavaContext context) {
return new ForwardingAstVisitor() {
@Override
public boolean visitConstructorInvocation(ConstructorInvocation node) {
TypeReference reference = node.astTypeReference();
String typeName = reference.astParts().last().astIdentifier().astValue();
// TODO: Should we handle factory method constructions of HashMaps as well,
// e.g. via Guava? This is a bit trickier since we need to infer the type
// arguments from the calling context.
if (typeName.equals(HASH_MAP)) {
checkHashMap(context, node, reference);
}
return super.visitConstructorInvocation(node);
}
};
}
示例2: createJavaVisitor
import lombok.ast.ForwardingAstVisitor; //导入依赖的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);
}
};
}
示例3: testBasicTypesMatch
import lombok.ast.ForwardingAstVisitor; //导入依赖的package包/类
@Test
public void testBasicTypesMatch() {
final AtomicInteger hit = new AtomicInteger();
Source s = new Source(SIMPLE_SOURCE, "SimpleTypesMatchTest.java");
s.parseCompilationUnit();
s.getNodes().get(0).accept(new ForwardingAstVisitor() {
@Override public boolean visitMethodDeclaration(MethodDeclaration node) {
assertTrue("typesMatch with star import should match but fails:" + node.astReturnTypeReference(),
new Resolver().typesMatch("java.util.List", node.astReturnTypeReference()));
assertFalse("typesMatch with no relevant imports should fail but matches",
new Resolver().typesMatch("java.awt.List", node.astReturnTypeReference()));
hit.incrementAndGet();
return true;
}
});
assertEquals("expected 1 hit on MethodDeclaration", 1, hit.get());
}
示例4: testMethodLocalMaskingTypesMatch
import lombok.ast.ForwardingAstVisitor; //导入依赖的package包/类
@Test
public void testMethodLocalMaskingTypesMatch() {
Source s = new Source(METHOD_LOCAL_MASKING_SOURCE, "MaskingTypesMatchTest.java");
s.parseCompilationUnit();
final AtomicInteger hit1 = new AtomicInteger();
final AtomicInteger hit2 = new AtomicInteger();
s.getNodes().get(0).accept(new ForwardingAstVisitor() {
@Override public boolean visitVariableDefinitionEntry(VariableDefinitionEntry node) {
if ("thisIsAUtilList".equals(node.astName().astValue())) {
assertTrue("thisIsAUtilList isn't matched to java.util.List, but should.",
new Resolver().typesMatch("java.util.List<?>", node.upToVariableDefinition().astTypeReference()));
hit1.incrementAndGet();
}
if ("butThisIsnt".equals(node.astName().astValue())) {
assertFalse("butThisIsnt is matched to java.util.List, but shouldn't be.",
new Resolver().typesMatch("java.util.List<?>", node.upToVariableDefinition().astTypeReference()));
hit2.incrementAndGet();
}
return true;
}
});
assertEquals("expected 1 hit on VarDefEntry 'thisIsAUtilList'", 1, hit1.get());
assertEquals("expected 1 hit on VarDefEntry 'butThisIsnt'", 1, hit2.get());
}
示例5: testArrayTypesMatch
import lombok.ast.ForwardingAstVisitor; //导入依赖的package包/类
@Test
public void testArrayTypesMatch() {
Source s = new Source(ARRAYS_SOURCE, "ArrayTypesMatchTest.java");
s.parseCompilationUnit();
final AtomicInteger hit = new AtomicInteger();
s.getNodes().get(0).accept(new ForwardingAstVisitor() {
@Override public boolean visitMethodDeclaration(MethodDeclaration node) {
assertTrue("typesMatch with String[] should match java.lang.String[] but doesn't.",
new Resolver().typesMatch("java.lang.String[]", node.astReturnTypeReference()));
assertFalse("typesMatch with String[] should NOT match java.lang.String[][] but does.",
new Resolver().typesMatch("java.lang.String[][]", node.astReturnTypeReference()));
assertFalse("typesMatch with String[] should NOT match java.lang.String but does.",
new Resolver().typesMatch("java.lang.String", node.astReturnTypeReference()));
assertTrue("typesMatch with String should match java.lang.String but doesn't.",
new Resolver().typesMatch("java.lang.String", node.astParameters().first().astTypeReference()));
assertFalse("typesMatch with String should NOT match java.lang.String[] but does.",
new Resolver().typesMatch("java.lang.String[]", node.astParameters().first().astTypeReference()));
hit.incrementAndGet();
return true;
}
});
assertEquals("expected 1 hit on MethodDeclaration", 1, hit.get());
}
示例6: testImportedTypesMatch
import lombok.ast.ForwardingAstVisitor; //导入依赖的package包/类
@Test
public void testImportedTypesMatch() {
Source s = new Source(IMPORTS_SOURCE, "ImportsTest.java");
s.parseCompilationUnit();
final AtomicInteger hit = new AtomicInteger();
s.getNodes().get(0).accept(new ForwardingAstVisitor() {
@Override public boolean visitMethodDeclaration(MethodDeclaration node) {
assertFalse("typesMatch with java.awt.List+java.util.* imported should NOT match java.util.List",
new Resolver().typesMatch("java.util.List", node.astReturnTypeReference()));
assertTrue("typesMatch with java.awt.List+java.util.* imported SHOULD match java.awt.List",
new Resolver().typesMatch("java.awt.List", node.astReturnTypeReference()));
hit.incrementAndGet();
return true;
}
});
assertEquals("expected 1 hit on MethodDeclaration", 1, hit.get());
}
示例7: foldStringConcats
import lombok.ast.ForwardingAstVisitor; //导入依赖的package包/类
private static void foldStringConcats(Node tree) {
tree.accept(new ForwardingAstVisitor() {
@Override public boolean visitBinaryExpression(BinaryExpression node) {
if (node.rawLeft() != null) node.rawLeft().accept(this);
if (node.rawRight() != null) node.rawRight().accept(this);
String left = null, right = null;
if (node.astOperator() != BinaryOperator.PLUS || node.getParent() == null) return true;
boolean leftIsChar = false;
if (node.rawLeft() instanceof StringLiteral) left = ((StringLiteral) node.rawLeft()).astValue();
if (node.rawLeft() instanceof CharLiteral) {
left = "" + ((CharLiteral) node.rawLeft()).astValue();
leftIsChar = true;
}
if (node.rawRight() instanceof StringLiteral) right = ((StringLiteral) node.rawRight()).astValue();
if (!leftIsChar && node.rawRight() instanceof CharLiteral) right = "" + ((CharLiteral) node.rawRight()).astValue();
if (left == null || right == null) return true;
int start = node.rawLeft().getPosition().getStart();
int end = node.rawRight().getPosition().getEnd();
node.getParent().replaceChild(node, new StringLiteral().astValue(left + right).setPosition(new Position(start, end)));
return true;
}
});
}
示例8: foldStringConcats
import lombok.ast.ForwardingAstVisitor; //导入依赖的package包/类
private static void foldStringConcats(Node tree) {
tree.accept(new ForwardingAstVisitor() {
@Override public boolean visitBinaryExpression(BinaryExpression node) {
if (node.rawLeft() != null) node.rawLeft().accept(this);
if (node.rawRight() != null) node.rawRight().accept(this);
if (
node.rawLeft() instanceof StringLiteral && node.rawRight() instanceof StringLiteral &&
node.astOperator() == BinaryOperator.PLUS) {
String left = ((StringLiteral) node.rawLeft()).astValue();
String right = ((StringLiteral) node.rawRight()).astValue();
int start = node.rawLeft().getPosition().getStart();
int end = node.rawRight().getPosition().getEnd();
if (left != null && right != null && node.getParent() != null) {
node.getParent().replaceChild(node, new StringLiteral().astValue(left + right).setPosition(new Position(start, end)));
}
}
return true;
}
});
}
示例9: createJavaVisitor
import lombok.ast.ForwardingAstVisitor; //导入依赖的package包/类
@Override
public AstVisitor createJavaVisitor(@NonNull final JavaContext context) {
return new ForwardingAstVisitor() {
@Override
public boolean visitVariableDeclaration(VariableDeclaration node) {
if (hasClassParent(node) && !PackageManager.isGenerated(context, node)) {
Node classDeclaration = node.getParent();
String nodeString = node.toString();
VariableDefinitionEntry variableDefinition = node.astDefinition().astVariables().first();
String name = variableDefinition.astName().astValue();
if (!isStaticOrFinal(node) && !isModel(context, classDeclaration) && !isWidget(nodeString)) {
if (!instanceVariableCorrectFormat(name)) {
context.report(ISSUE_INSTANCE_VARIABLE_NAME, context.getLocation(node),
"Expecting " + name + " to begin with 'm' and be written in camelCase.");
}
} else if (isStaticAndFinal(node)) {
if (!staticFinalCorrectFormat(name)) {
context.report(ISSUE_CLASS_CONSTANT_NAME, context.getLocation(node),
"Expecting " + name + " to be named using UPPER_SNAKE_CASE.");
}
}
}
return super.visitVariableDeclaration(node);
}
};
}
示例10: createJavaVisitor
import lombok.ast.ForwardingAstVisitor; //导入依赖的package包/类
@Nullable
@Override
public AstVisitor createJavaVisitor(@NonNull JavaContext context) {
if (mApiDatabase == null) {
return new ForwardingAstVisitor() {
};
}
return new ApiVisitor(context);
}
示例11: createJavaVisitor
import lombok.ast.ForwardingAstVisitor; //导入依赖的package包/类
@Override
public AstVisitor createJavaVisitor(@NonNull JavaContext context) {
if (rtlApplies(context)) {
return new IdentifierChecker(context);
}
return new ForwardingAstVisitor() { };
}
示例12: createJavaVisitor
import lombok.ast.ForwardingAstVisitor; //导入依赖的package包/类
@Override
public AstVisitor createJavaVisitor(@NonNull final JavaContext context) {
return new ForwardingAstVisitor() {
@Override
public boolean visitAssert(Assert node) {
if (!context.getMainProject().isAndroidProject()) {
return true;
}
Expression assertion = node.astAssertion();
// Allow "assert true"; it's basically a no-op
if (assertion instanceof BooleanLiteral) {
Boolean b = ((BooleanLiteral) assertion).astValue();
if (b != null && b) {
return false;
}
} else {
// Allow assertions of the form "assert foo != null" because they are often used
// to make statements to tools about known nullness properties. For example,
// findViewById() may technically return null in some cases, but a developer
// may know that it won't be when it's called correctly, so the assertion helps
// to clear nullness warnings.
if (isNullCheck(assertion)) {
return false;
}
}
String message
= "Assertions are unreliable. Use `BuildConfig.DEBUG` conditional checks instead.";
context.report(ISSUE, node, context.getLocation(node), message);
return false;
}
};
}
示例13: createJavaVisitor
import lombok.ast.ForwardingAstVisitor; //导入依赖的package包/类
@Override
public AstVisitor createJavaVisitor(@NonNull final JavaContext context) {
return new ForwardingAstVisitor() {
@Override
public boolean visitMethodDeclaration(MethodDeclaration node) {
ResolvedNode resolved = context.resolve(node);
if (resolved instanceof ResolvedMethod) {
ResolvedMethod method = (ResolvedMethod) resolved;
checkCallSuper(context, node, method);
}
return false;
}
};
}
示例14: check
import lombok.ast.ForwardingAstVisitor; //导入依赖的package包/类
private static void check(Object expected, String source, final String targetVariable) {
JavaContext context = LintUtilsTest.parse(source, new File("src/test/pkg/Test.java"));
assertNotNull(context);
CompilationUnit unit = (CompilationUnit) context.getCompilationUnit();
assertNotNull(unit);
// Find the expression
final AtomicReference<Expression> reference = new AtomicReference<Expression>();
unit.accept(new ForwardingAstVisitor() {
@Override
public boolean visitVariableDefinitionEntry(VariableDefinitionEntry node) {
if (node.astName().astValue().equals(targetVariable)) {
reference.set(node.astInitializer());
}
return super.visitVariableDefinitionEntry(node);
}
});
Expression expression = reference.get();
Object actual = ConstantEvaluator.evaluate(context, expression);
if (expected == null) {
assertNull(actual);
} else {
assertNotNull("Couldn't compute value for " + source + ", expected " + expected,
actual);
assertEquals(expected.getClass(), actual.getClass());
assertEquals(expected.toString(), actual.toString());
}
assertEquals(expected, actual);
if (expected instanceof String) {
assertEquals(expected, ConstantEvaluator.evaluateString(context, expression,
false));
}
}
示例15: createJavaVisitor
import lombok.ast.ForwardingAstVisitor; //导入依赖的package包/类
@Nullable
@Override
public AstVisitor createJavaVisitor(@NonNull final JavaContext context) {
return new ForwardingAstVisitor() {
@Override
public boolean visitCompilationUnit(CompilationUnit node) {
check(context);
return true;
}
};
}