Refine search
private static List<String> getComparableParameterNames( ExecutableElement element, boolean skipFirst) { List<? extends VariableElement> parameters = element.getParameters(); if (skipFirst) { parameters = parameters.subList(1, parameters.size()); } List<String> result = new ArrayList<>(parameters.size()); for (VariableElement parameter : parameters) { result.add(parameter.asType().toString()); } return result; }
private static void validateGlideTypeParameters(ExecutableElement executableElement) { if (executableElement.getParameters().size() != 1) { throw new IllegalArgumentException("@GlideType methods must take a" + " RequestBuilder object as their first and only parameter, but given multiple for: " + getQualifiedMethodName(executableElement)); } VariableElement first = executableElement.getParameters().get(0); TypeMirror argumentType = first.asType(); if (!argumentType.toString().startsWith("com.bumptech.glide.RequestBuilder")) { throw new IllegalArgumentException("@GlideType methods must take a" + " RequestBuilder object as their first and only parameter, but given: " + argumentType + " for: " + getQualifiedMethodName(executableElement)); } }
private boolean hasActivityOptions(TypeElement type, int optionsParamPosition) { if (type == null) { return false; } for (ExecutableElement element : ElementFilter.methodsIn(elementUtils.getAllMembers(type))) { if (element.getSimpleName().contentEquals("startActivity")) { List<? extends VariableElement> parameters = element.getParameters(); if (parameters.size() == optionsParamPosition + 1) { VariableElement parameter = parameters.get(optionsParamPosition); if (parameter.asType().toString().equals(CanonicalNameConstants.BUNDLE)) { return true; } } } } return false; }
public void hasSeekBarTouchTrackingMethodParameters(ExecutableElement executableElement, ElementValidation valid) { List<? extends VariableElement> parameters = executableElement.getParameters(); if (parameters.size() > 1) { valid.addError("Unrecognized parameter declaration. You can only have one parameter of type " + CanonicalNameConstants.SEEKBAR + ". Try declaring " + executableElement.getSimpleName() + "(" + CanonicalNameConstants.SEEKBAR + " seekBar);"); return; } if (parameters.size() == 1) { String parameterType = parameters.get(0).asType().toString(); if (!parameterType.equals(CanonicalNameConstants.SEEKBAR)) { valid.addError("Unrecognized parameter declaration. You can only have one parameter of type " + CanonicalNameConstants.SEEKBAR + ". Try declaring " + executableElement.getSimpleName() + "(" + CanonicalNameConstants.SEEKBAR + " seekBar);"); } } }
private ExecutableElement getMethod(List<ExecutableElement> methods, String methodName, String returnType, String... parameterTypes) { for (ExecutableElement method : methods) { List<? extends VariableElement> parameters = method.getParameters(); // Get the method return type or "VOID" if none. String methodReturnType = method.getReturnType().getKind() == TypeKind.VOID ? TypeKind.VOID.toString() : method.getReturnType().toString(); if (parameters.size() == parameterTypes.length && methodReturnType.equals(returnType)) { if (methodName == null || method.getSimpleName().toString().equals(methodName)) { // At this point, method name, return type and number of // parameters are correct. Now we need to validate the // parameter types. boolean validMethod = true; for (int i = 0; i < parameters.size(); i++) { VariableElement param = parameters.get(i); if (!param.asType().toString().equals(parameterTypes[i])) { // Parameter type does not match, this is not the // correct method. validMethod = false; break; } } if (validMethod) { return method; } } } } return null; }
@SupportedAnnotationTypes("com.example.MyAnnotation") public class CompileTimeAnnotationProcessor extends AbstractProcessor { @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { // Only one annotation, so just use annotations.iterator().next(); Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith( annotations.iterator().next()); Set<VariableElement> fields = ElementFilter.fieldsIn(elements); for (VariableElement field : fields) { TypeMirror fieldType = field.asType(); String fullTypeClassName = fieldType.toString(); // Validate fullTypeClassName } return true; } }
public static String getSetter(Element element, Elements elements) { TypeElement enclosingElement = (TypeElement) element.getEnclosingElement(); String elementName = element.getSimpleName().toString(); String elementNameLowerCase = elementName.toLowerCase(); List<String> possibleMethodNames = new ArrayList<>(); possibleMethodNames.add("set" + elementNameLowerCase); // Handle the case where variables are named in the form mVariableName instead of just variableName if (elementName.length() > 1 && elementName.charAt(0) == 'm' && (elementName.charAt(1) >= 'A' && elementName.charAt(1) <= 'Z')) { possibleMethodNames.add("set" + elementNameLowerCase.substring(1)); } List<? extends Element> elementMembers = elements.getAllMembers(enclosingElement); List<ExecutableElement> elementMethods = ElementFilter.methodsIn(elementMembers); for (ExecutableElement methodElement : elementMethods) { String methodNameString = methodElement.getSimpleName().toString(); String methodNameLowerCase = methodNameString.toLowerCase(); if (possibleMethodNames.contains(methodNameLowerCase)) { if (methodElement.getParameters().size() == 1) { if (methodElement.getParameters().get(0).asType().toString().equals(element.asType().toString())) { return methodNameString; } } } } return null; }
protected SortedMap<String, JVar> addMethodParams(ExecutableElement executableElement, RestHolder restHolder, JMethod method) { List<? extends VariableElement> params = executableElement.getParameters(); SortedMap<String, JVar> methodParams = new TreeMap<>(); for (VariableElement parameter : params) { String paramName = parameter.getSimpleName().toString(); String paramType = parameter.asType().toString(); JVar param; if (parameter.asType().getKind().isPrimitive()) { param = method.param(getCodeModel().parseType(paramType), paramName); } else { AbstractJClass parameterClass = codeModelHelper.typeMirrorToJClass(parameter.asType()); param = method.param(parameterClass, paramName); } methodParams.put(paramName, param); } return methodParams; }
public void hasAfterTextChangedMethodParameters(ExecutableElement executableElement, ElementValidation valid) { List<? extends VariableElement> parameters = executableElement.getParameters(); boolean editableParameterFound = false; boolean textViewParameterFound = false; for (VariableElement parameter : parameters) { String parameterType = parameter.asType().toString(); if (parameterType.equals(CanonicalNameConstants.EDITABLE)) { if (editableParameterFound) { valid.addError("Unrecognized parameter declaration. you can declare only one parameter of type android.text.Editable"); } editableParameterFound = true; continue; } if (parameterType.equals(CanonicalNameConstants.TEXT_VIEW)) { if (textViewParameterFound) { valid.addError("Unrecognized parameter declaration. you can declare only one parameter of type android.widget.TextView"); } textViewParameterFound = true; continue; } valid.addError("Unrecognized parameter type. %s can only have a android.widget.TextView parameter and/or an android.text.Editable parameter." + " See android.text.TextWatcher.afterTextChanged() for more informations."); } }
boolean textViewParameterFound = false; for (VariableElement parameter : parameters) { String parameterType = parameter.asType().toString(); if (parameterType.equals(CanonicalNameConstants.CHAR_SEQUENCE)) { if (charSequenceParameterFound) { continue; if (parameter.asType().getKind() == TypeKind.INT || CanonicalNameConstants.INTEGER.equals(parameterType)) { String parameterName = parameter.toString(); if ("start".equals(parameterName) || "count".equals(parameterName) || "after".equals(parameterName)) {
boolean progressParameterFound = false; for (VariableElement parameter : parameters) { String parameterType = parameter.asType().toString(); if (parameterType.equals(CanonicalNameConstants.SEEKBAR)) { if (seekBarParameterFound) { continue; if (parameter.asType().getKind() == TypeKind.INT || CanonicalNameConstants.INTEGER.equals(parameterType)) { if (progressParameterFound) { valid.addError("You can have only one parameter of type " + CanonicalNameConstants.INTEGER); continue; if (parameter.asType().getKind() == TypeKind.BOOLEAN || CanonicalNameConstants.BOOLEAN.equals(parameterType)) { if (fromUserParameterFound) { valid.addError("You can have only one parameter of type " + CanonicalNameConstants.BOOLEAN);
boolean textViewParameterFound = false; for (VariableElement parameter : parameters) { String parameterType = parameter.asType().toString(); if (parameterType.equals(CanonicalNameConstants.CHAR_SEQUENCE)) { if (charSequenceParameterFound) { continue; if (parameter.asType().getKind() == TypeKind.INT || CanonicalNameConstants.INTEGER.equals(parameterType)) { String parameterName = parameter.toString(); if ("start".equals(parameterName) || "before".equals(parameterName) || "count".equals(parameterName)) {
public void isAbstractOrHasEmptyOrContextConstructor(Element element, ElementValidation valid) { List<ExecutableElement> constructors = ElementFilter.constructorsIn(element.getEnclosedElements()); if (!annotationHelper.isAbstract(element)) { if (constructors.size() == 1) { ExecutableElement constructor = constructors.get(0); if (!annotationHelper.isPrivate(constructor)) { if (constructor.getParameters().size() > 1) { valid.addError("%s annotated element should have a constructor with one parameter max, of type " + CanonicalNameConstants.CONTEXT); } else if (constructor.getParameters().size() == 1) { VariableElement parameter = constructor.getParameters().get(0); if (!parameter.asType().toString().equals(CanonicalNameConstants.CONTEXT)) { valid.addError("%s annotated element should have a constructor with one parameter max, of type " + CanonicalNameConstants.CONTEXT); } } } else { valid.addError("%s annotated element should not have a private constructor"); } } else { valid.addError("%s annotated element should have only one constructor"); } } }
private static boolean areParamsTheSame(ExecutableElement method1, MethodSpec method2, Types types, Elements elements) { List<? extends VariableElement> params1 = method1.getParameters(); List<ParameterSpec> params2 = method2.parameters; if (params1.size() != params2.size()) { return false; } for (int i = 0; i < params1.size(); i++) { VariableElement param1 = params1.get(i); ParameterSpec param2 = params2.get(i); TypeMirror param1Type = types.erasure(param1.asType()); TypeMirror param2Type = types.erasure(KotlinUtilsKt.getTypeMirror(param2.type.toString(), elements)); // If a param is a type variable then we don't need an exact type match, it just needs to // be assignable if (param1.asType().getKind() == TypeKind.TYPEVAR) { if (!types.isAssignable(param2Type, param1Type)) { return false; } } else if (!param1Type.toString().equals(param2Type.toString())) { return false; } } return true; }
@Override public void passParametersToMethodCall(Element element, HasKeyEventCallbackMethods holder, JInvocation methodCall) { ExecutableElement executableElement = (ExecutableElement) element; List<? extends VariableElement> parameters = executableElement.getParameters(); if (parameters.size() == 1) { if (parameters.get(0).asType().toString().equals(int.class.getName())) { methodCall.arg(holder.getOnKeyMultipleCountParam()); } else { methodCall.arg(holder.getOnKeyMultipleKeyEventParam()); } } else if (parameters.size() == 2) { if (parameters.get(0).asType().toString().equals(int.class.getName())) { methodCall.arg(holder.getOnKeyMultipleCountParam()).arg(holder.getOnKeyMultipleKeyEventParam()); } else { methodCall.arg(holder.getOnKeyMultipleKeyEventParam()).arg(holder.getOnKeyMultipleCountParam()); } } } }
public static void validateSwipeView(VariableElement element) throws IllegalUseException { validateNonPrivateModifier(element); String androidViewCanonicalName = NameStore.getClassNameWithPackage( NameStore.Package.ANDROID_VIEW, NameStore.Class.ANDROID_VIEW); if (!element.asType().toString().equals(androidViewCanonicalName)) { String msg = "@SwipeView should be used only on " + androidViewCanonicalName; throw new IllegalUseException(msg); } }