private static String getSmartPrimitiveParameterName(VariableElement parameter) { for (AnnotationMirror annotation : parameter.getAnnotationMirrors()) { String annotationName = annotation.getAnnotationType().toString().toUpperCase(); if (annotationName.endsWith("RES")) { // Catch annotations like StringRes return "id"; } else if (annotationName.endsWith("RANGE")) { // Catch annotations like IntRange return "value"; } } return parameter.getSimpleName().toString(); }
private static List<AnnotationSpec> getAnnotations(VariableElement element) { List<AnnotationSpec> result = new ArrayList<>(); for (AnnotationMirror mirror : element.getAnnotationMirrors()) { result.add(AnnotationSpec.get(mirror)); } return result; }
public static MethodSpec.Builder overriding(ExecutableElement method) { return MethodSpec.methodBuilder(method.getSimpleName().toString()) .addAnnotation(Override.class) .addModifiers(method.getModifiers().stream().filter(modifier -> modifier != Modifier.ABSTRACT).collect(Collectors.toList())) .returns(TypeName.get(method.getReturnType())) .varargs(method.isVarArgs()) .addExceptions(method.getThrownTypes().stream().map(TypeName::get).collect(Collectors.toList())) .addTypeVariables(method.getTypeParameters().stream().map(TypeVariableName::get).collect(Collectors.toList())) .addParameters(method.getParameters().stream().map(element -> ParameterSpec.get(element).toBuilder() .addAnnotations(element.getAnnotationMirrors().stream().map(AnnotationSpec::get).collect(Collectors.toList())).build()).collect(Collectors.toList())); }
/** Returns the provider key for {@code variable}. */ public static String get(VariableElement variable) { StringBuilder result = new StringBuilder(); AnnotationMirror qualifier = getQualifier(variable.getAnnotationMirrors()); if (qualifier != null) { qualifierToString(qualifier, result); } typeToString(variable.asType(), result, '$'); return result.toString(); }
protected static List<ParameterSpec> buildParamSpecs(List<? extends VariableElement> params) { List<ParameterSpec> result = new ArrayList<>(); for (VariableElement param : params) { Builder builder = ParameterSpec.builder(TypeName.get(param.asType()), param.getSimpleName().toString()); for (AnnotationMirror annotation : param.getAnnotationMirrors()) { builder.addAnnotation(AnnotationSpec.get(annotation)); } result.add(builder.build()); } return result; }
private void addParamToMethod(JMethod method, VariableElement parameter, int mod, Map<String, TypeMirror> actualTypes, boolean varParam) { String parameterName = parameter.getSimpleName().toString(); AbstractJClass parameterClass = typeMirrorToJClass(parameter.asType(), actualTypes); JVar param = varParam ? method.varParam(mod, parameterClass.elementType(), parameterName) : method.param(mod, parameterClass, parameterName); copyNonAAAnnotations(param, parameter.getAnnotationMirrors()); }
private static List<AnnotationSpec> getExternalAnnotations(VariableElement param) { final List<? extends AnnotationMirror> annotationMirrors = param.getAnnotationMirrors(); final List<AnnotationSpec> annotations = new ArrayList<>(); for (AnnotationMirror annotationMirror : annotationMirrors) { if (annotationMirror.getAnnotationType().toString().startsWith(COMPONENTS_PACKAGE)) { continue; } final AnnotationSpec.Builder annotationSpec = AnnotationSpec.builder( ClassName.bestGuess(annotationMirror.getAnnotationType().toString())); Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues = annotationMirror.getElementValues(); for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> elementValue : elementValues.entrySet()) { annotationSpec.addMember( elementValue.getKey().getSimpleName().toString(), elementValue.getValue().toString()); } annotations.add(annotationSpec.build()); } return annotations; }
private void debugMethod(ExecutableElement method) { logger.finest(format(" method: simpleName=%s, asType=%s, varargs=%s, returnType=%s, enclosingElement=%s, params=%s, typeParams=%s", method.getSimpleName(), method.asType(), method.isVarArgs(), method.getReturnType(), method.getEnclosingElement(), method.getParameters(), method.getTypeParameters())); for (VariableElement variable : method.getParameters()) { logger.finest(format(" variable: name=%s, annotationMirrors=%s, @Option=%s, @Parameters=%s", variable.getSimpleName(), variable.getAnnotationMirrors(), variable.getAnnotation( Option.class), variable.getAnnotation(Parameters.class))); } }
@Override public Stream<CompilationMessage> visitVariable( VariableElement parameter, Void ignored ) { Name annotation = parameter.getAnnotation( Name.class ); if ( annotation == null ) { return Stream.of( new ParameterMissingAnnotationError( parameter, annotationMirror( parameter.getAnnotationMirrors() ), "@%s usage error: missing on parameter <%s>", Name.class.getName(), nameOf( parameter ) ) ); } if ( !parameterTypeVisitor.visit( parameter.asType() ) ) { Element method = parameter.getEnclosingElement(); return Stream.of( new ParameterTypeError( parameter, "Unsupported parameter type <%s> of " + "procedure|function" + " %s#%s", parameter.asType().toString(), method.getEnclosingElement().getSimpleName(), method.getSimpleName() ) ); } return Stream.empty(); }
/** * Only allow construction from within the parent class. * * @param processingEnv the annotation processing environment. * @param qualifiedType the qualified type name of the parameter. * @param param the parameter. * @param formatterClass the formatter class, or {@code null} if none * @param isVarArgs {@code true} if this is a vararg parameter, otherwise {@code false}. */ AptParameter(final ProcessingEnvironment processingEnv, final String qualifiedType, final VariableElement param, final String formatterClass, final boolean isVarArgs) { super(processingEnv, param); this.qualifiedType = qualifiedType; this.param = param; this.formatterClass = formatterClass; this.isVarArgs = isVarArgs; isFormatArg = param.getAnnotationMirrors().isEmpty() || ElementHelper.isAnnotatedWith(param, FormatWith.class) || ElementHelper.isAnnotatedWith(param, Transform.class) || ElementHelper.isAnnotatedWith(param, Pos.class); }
OptionSpec.Builder builder = OptionSpec.builder(typedMember, factory); builder.completionCandidates(extractCompletionCandidates(variable, variable.getAnnotationMirrors())); builder.converters(extractConverters(variable, variable.getAnnotationMirrors())); options.put(variable, builder); } else if (!isMixin) { TypedMember typedMember = new TypedMember(variable, position); PositionalParamSpec.Builder builder = PositionalParamSpec.builder(typedMember, factory); builder.completionCandidates(extractCompletionCandidates(variable, variable.getAnnotationMirrors())); builder.converters(extractConverters(variable, variable.getAnnotationMirrors())); parameters.put(variable, builder);
/** * Traverses the arguments of {@code method} and for each argument annotated with {@link ParameterGroup} it invokes * {@link #getOperationParameterGroupDocumentation(TypeElement, Map, ProcessingEnvironment)} * * @param env the current {@link ProcessingEnvironment} * @param method the operation method being processed * @param docs a {@link Map} which keys are attribute names and values are their documentation */ private void parseOperationParameterGroups(ProcessingEnvironment env, ExecutableElement method, Map<String, String> docs) { for (VariableElement variable : method.getParameters()) { for (AnnotationMirror compound : variable.getAnnotationMirrors()) { DeclaredType annotationType = compound.getAnnotationType(); if (annotationType != null) { Class annotationClass = classFor((TypeElement) compound.getAnnotationType().asElement(), env).get(); if (ParameterGroup.class.isAssignableFrom(annotationClass)) { try { getOperationParameterGroupDocumentation((TypeElement) env.getTypeUtils().asElement(variable.asType()), docs, env); } catch (Exception e) { throw new RuntimeException(e); } } } } } }
/** * Lookup both {@link javax.inject.Qualifier} and {@link javax.inject.Named} * to provide the name of an injection. * * @param element the element for which a qualifier is to be found. * @return the name of this element or null if it has no qualifier annotations. */ private String findQualifierName(VariableElement element) { String name = null; if (element.getAnnotationMirrors().isEmpty()) { return name; } for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) { TypeElement annotationTypeElement = (TypeElement) annotationMirror.getAnnotationType().asElement(); if (isSameType(annotationTypeElement, "javax.inject.Named")) { checkIfAlreadyHasName(element, name); name = getValueOfAnnotation(annotationMirror); } else if (annotationTypeElement.getAnnotation(javax.inject.Qualifier.class) != null) { checkIfAlreadyHasName(element, name); name = annotationTypeElement.getQualifiedName().toString(); } } return name; }
/** * Checks if a given method has any constraint or cascaded validation annotations on its parameters. * * @param method the method to check * @return {@code true} if a constraint or cascaded annotations are present on any of the method parameters, * {@code false} otherwise */ private boolean hasAnnotationsOnParameters(ExecutableElement method) { for ( VariableElement parameter : method.getParameters() ) { for ( AnnotationMirror annotationMirror : parameter.getAnnotationMirrors() ) { ConstraintHelper.AnnotationType annotationType = constraintHelper.getAnnotationType( annotationMirror ); if ( ConstraintHelper.AnnotationType.CONSTRAINT_ANNOTATION.equals( annotationType ) || ConstraintHelper.AnnotationType.MULTI_VALUED_CONSTRAINT_ANNOTATION.equals( annotationType ) || ConstraintHelper.AnnotationType.GRAPH_VALIDATION_ANNOTATION.equals( annotationType ) ) { return true; } } } return false; }
@Override public List<? extends AnnotationMirror> getAnnotationMirrors() { return variableElement.getAnnotationMirrors(); }
@Override public Collection<AnnotationMirror> getDeclaredAnnotation(VariableElement field) { List<? extends AnnotationMirror> list = field.getAnnotationMirrors(); ArrayList<AnnotationMirror> tmp = new ArrayList<AnnotationMirror>(list.size()); for (AnnotationMirror am : list) { tmp.add(am); } return tmp; } }
private boolean isTransient(VariableElement field) { for (AnnotationMirror annotationMirror : field.getAnnotationMirrors()) { if (annotationMirror.getAnnotationType().toString().equals( "org.eiichiro.acidhouse.Transient")) { return true; } } return false; }
private Optional<AnnotationMirror> fieldNamesAnnotation(VariableElement visitorArg) { return visitorArg.getAnnotationMirrors() .stream() .filter(am -> types.isSameType(types.getDeclaredType(elements.getTypeElement(FieldNames.class.getName())), am.getAnnotationType())) .findFirst() .map(Function.<AnnotationMirror>identity()); }
private Optional<AnnotationMirror> fieldNamesAnnotation(VariableElement visitorArg) { return visitorArg.getAnnotationMirrors() .stream() .filter(am -> types.isSameType(types.getDeclaredType(elements.getTypeElement(FieldNames.class.getName())), am.getAnnotationType())) .findFirst() .map(Function.<AnnotationMirror>identity()); }
@Override public Void visitVariable(VariableElement f, Void p) { visitAnnotations(f, f.getAnnotationMirrors()); visitTypeReference(f, f.asType()); return super.visitVariable(f, p); }