How to use getParameterType method of org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType class

Best Mockito code snippet using org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType.getParameterType

Source:AbstractMockitoAnyForPrimitiveType.java Github

copy

Full Screen

...61 if (argumentIndex == -1) {62 throw new IllegalStateException(63 "Cannot find argument " + tree + " in argument list from " + parentTree);64 }65 Type parameterType = getParameterType(parentMethod, argumentIndex);66 TypeKind parameterTypeKind = parameterType.getKind();67 if (parameterTypeKind.isPrimitive() && parameterTypeKind != matcherType.getKind()) {68 String expectedTypeAsString = parameterType.toString();69 String replacementName =70 "any"71 + Character.toUpperCase(expectedTypeAsString.charAt(0))72 + expectedTypeAsString.substring(1);73 String message = formatMessage(expectedTypeAsString, matcherType, replacementName);74 SuggestedFix.Builder fixBuilder = SuggestedFix.builder();75 ExpressionTree methodSelect = tree.getMethodSelect();76 String replacement;77 if (methodSelect instanceof MemberSelectTree) {78 MemberSelectTree qualifier = (MemberSelectTree) methodSelect;79 replacement = state.getSourceForNode(qualifier.getExpression()) + "." + replacementName;80 } else {81 replacement = replacementName;82 String staticImport = method.owner + "." + replacementName;83 fixBuilder.addStaticImport(staticImport);84 }85 SuggestedFix fix = fixBuilder.replace(tree, replacement + "()").build();86 return buildDescription(tree).setMessage(message).addFix(fix).build();87 }88 return NO_MATCH;89 }90 /**91 * Get the type of the parameter for a supplied argument.92 *93 * @param method the method symbol that is being called.94 * @param argumentIndex the index of the argument, can be greater than the number of parameters95 * for a var arg method.96 * @return the type of the associated parameter.97 */98 private Type getParameterType(MethodSymbol method, int argumentIndex) {99 List<VarSymbol> parameters = method.getParameters();100 Type parameterType;101 int parameterCount = parameters.size();102 if (argumentIndex >= parameterCount && method.isVarArgs()) {103 VarSymbol varArgParameter = parameters.get(parameterCount - 1);104 parameterType = ((ArrayType) varArgParameter.asType()).getComponentType();105 } else {106 parameterType = parameters.get(argumentIndex).asType();107 }108 return parameterType;109 }110}...

Full Screen

Full Screen

getParameterType

Using AI Code Generation

copy

Full Screen

1package org.mockito.errorprone.bugpatterns;2import com.google.errorprone.BugPattern;3import com.google.errorprone.VisitorState;4import com.google.errorprone.bugpatterns.BugChecker;5import com.google.errorprone.fixes.SuggestedFix;6import com.google.errorprone.matchers.Description;7import com.google.errorprone.matchers.Matcher;8import com.google.errorprone.matchers.Matchers;9import com.sun.source.tree.ExpressionTree;10import com.sun.source.tree.MethodInvocationTree;11import com.sun.source.tree.Tree;12import com.sun.tools.javac.code.Symbol;13import com.sun.tools.javac.code.Type;14import com.sun.tools.javac.code.TypeTag;15import com.sun.tools.javac.tree.JCTree;16import com.sun.tools.javac.tree.JCTree.JCExpression;17import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;18import com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree;19import com.sun.tools.javac.tree.JCTree.JCTypeApply;20import com.sun.tools.javac.tree.JCTree.JCTypeCast;21import com.sun.tools.javac.tree.JCTree.JCTypeParameter;22import com.sun.tools.javac.tree.JCTree.JCVariableDecl;23import com.sun.tools.javac.util.List;24import java.util.Optional;25import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;26import static com.google.errorprone.BugPattern.StandardTags.FRAGILE_CODE;27@BugPattern(28public class AbstractMockitoAnyForPrimitiveType extends BugChecker implements BugChecker.MethodInvocationTreeMatcher {29 private static final String MOCKITO_ANY_METHOD = "any";30 private static final String MOCKITO_ANY_CLASS = "org.mockito.ArgumentMatchers";31 private static final Matcher<ExpressionTree> MOCKITO_ANY_MATCHER = Matchers.instanceMethod()32 .onDescendantOf(MOCKITO_ANY_CLASS)33 .named(MOCKITO_ANY_METHOD);34 public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {35 if (MOCKITO_ANY_MATCHER.matches(tree, state)) {36 JCMethodInvocation methodInvocation = (JCMethodInvocation) tree;37 List<JCExpression> arguments = methodInvocation.getArguments();38 if (

Full Screen

Full Screen

getParameterType

Using AI Code Generation

copy

Full Screen

1import org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType;2import org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType.ParameterType;3class Test {4 public void test() {5 ParameterType type = AbstractMockitoAnyForPrimitiveType.getParameterType("int");6 if (type == ParameterType.INT) {7 System.out.println("type is int");8 }9 }10}

Full Screen

Full Screen

getParameterType

Using AI Code Generation

copy

Full Screen

1public class MockitoAnyForPrimitiveTypePositiveCases {2 public void test() {3 Mockito.any(int.class);4 Mockito.any(long.class);5 Mockito.any(double.class);6 Mockito.any(float.class);7 Mockito.any(short.class);8 Mockito.any(byte.class);9 Mockito.any(boolean.class);10 Mockito.any(char.class);11 }12}13public class MockitoAnyForPrimitiveTypeNegativeCases {14 public void test() {15 Mockito.any(Integer.class);16 Mockito.any(Long.class);17 Mockito.any(Double.class);18 Mockito.any(Float.class);19 Mockito.any(Short.class);20 Mockito.any(Byte.class);21 Mockito.any(Boolean.class);22 Mockito.any(Character.class);23 }24}25@AutoService(BugChecker.class)26public class MockitoAnyForPrimitiveType extends BugChecker implements MethodInvocationTreeMatcher {27 private static final Matcher<ExpressionTree> MOCKITO_ANY_METHOD = Matchers.anyOf(28 Matchers.instanceMethod().onDescendantOf("org.mockito.Mockito").named("any")29 );30 public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {31 if (MOCKITO_ANY_METHOD.matches(tree, state)) {32 Type parameterType = getParameterType(tree, state);33 if (isPrimitiveType(parameterType)) {34 return describeMatch(tree);35 }36 }37 return Description.NO_MATCH;38 }39 private static Type getParameterType(MethodInvocationTree tree, VisitorState state) {40 List<? extends ExpressionTree> arguments = tree.getArguments();41 if (arguments.size() != 1) {42 throw new IllegalStateException("Unexpected number of arguments");43 }44 ExpressionTree argument = arguments.get(0);45 return ASTHelpers.getType(argument);46 }47 private static boolean isPrimitiveType(Type type) {48 return type.getKind().isPrimitive();49 }50}51@AutoService(BugChecker.class)52public class MockitoAnyForPrimitiveTypeTest extends BugCheckerRefactoringTest {53 public void testPositiveCases() {54 BugCheckerRefactoringTestHelper.newInstance(new MockitoAnyForPrimitiveType(), getClass())55 .addInputLines("in/Test.java",56 "import org.mockito.Mockito;",57 "public class Test {",58 " public void test() {",59 " Mockito.any(int.class);",60 " Mockito.any(long.class);",61 " Mockito.any(double.class);",62 " Mockito.any(float.class);",63 " Mockito.any(short

Full Screen

Full Screen

getParameterType

Using AI Code Generation

copy

Full Screen

1import org.mockito.Matchers;2import org.mockito.Mockito;3import org.mockito.Mockito;4import org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType;5import org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType;6import org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType;7import org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType;8import org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType;9import org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType;10import org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType;

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in AbstractMockitoAnyForPrimitiveType

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful