Refine search
public static AnnotationSpec get(AnnotationMirror annotation) { TypeElement element = (TypeElement) annotation.getAnnotationType().asElement(); AnnotationSpec.Builder builder = AnnotationSpec.builder(ClassName.get(element)); Visitor visitor = new Visitor(builder); for (ExecutableElement executableElement : annotation.getElementValues().keySet()) { String name = executableElement.getSimpleName().toString(); AnnotationValue value = annotation.getElementValues().get(executableElement); value.accept(visitor, name); } return builder.build(); }
public static AnnotationSpec get(Annotation annotation, boolean includeDefaultValues) { Builder builder = builder(annotation.annotationType()); try { Method[] methods = annotation.annotationType().getDeclaredMethods(); Arrays.sort(methods, Comparator.comparing(Method::getName)); for (Method method : methods) { Object value = method.invoke(annotation); if (!includeDefaultValues) { if (Objects.deepEquals(value, method.getDefaultValue())) { continue; } } if (value.getClass().isArray()) { for (int i = 0; i < Array.getLength(value); i++) { builder.addMemberForValue(method.getName(), Array.get(value, i)); } continue; } if (value instanceof Annotation) { builder.addMember(method.getName(), "$L", get((Annotation) value)); continue; } builder.addMemberForValue(method.getName(), value); } } catch (Exception e) { throw new RuntimeException("Reflecting " + annotation + " failed!", e); } return builder.build(); }
public Builder toBuilder() { Builder builder = new Builder(type); for (Map.Entry<String, List<CodeBlock>> entry : members.entrySet()) { builder.members.put(entry.getKey(), new ArrayList<>(entry.getValue())); } return builder; }
public Builder addMember(String name, String format, Object... args) { return addMember(name, CodeBlock.of(format, args)); }
public static Builder builder(ClassName type) { checkNotNull(type, "type == null"); return new Builder(type); }
@Override public Builder visitEnumConstant(VariableElement c, String name) { return builder.addMember(name, "$T.$L", c.asType(), c.getSimpleName()); }
@Override public Builder visitType(TypeMirror t, String name) { return builder.addMember(name, "$T.class", t); }
/** * Delegates to {@link #addMember(String, String, Object...)}, with parameter {@code format} * depending on the given {@code value} object. Falls back to {@code "$L"} literal format if * the class of the given {@code value} object is not supported. */ Builder addMemberForValue(String memberName, Object value) { checkNotNull(memberName, "memberName == null"); checkNotNull(value, "value == null, constant non-null value expected for %s", memberName); checkArgument(SourceVersion.isName(memberName), "not a valid name: %s", memberName); if (value instanceof Class<?>) { return addMember(memberName, "$T.class", value); } if (value instanceof Enum) { return addMember(memberName, "$T.$L", value.getClass(), ((Enum<?>) value).name()); } if (value instanceof String) { return addMember(memberName, "$S", value); } if (value instanceof Float) { return addMember(memberName, "$Lf", value); } if (value instanceof Character) { return addMember(memberName, "'$L'", characterLiteralWithoutSingleQuotes((char) value)); } return addMember(memberName, "$L", value); }
private void handleMethodConsumer(MethodSpec.Builder methodSpec, Multimap<String, String> ramlTypeToMediaType, GType typeDeclaration) { Collection<String> mediaTypes = ramlTypeToMediaType.get(typeDeclaration == null ? null : typeDeclaration.name()); if (mediaTypes.size() > 0) { AnnotationSpec.Builder ann = buildAnnotation(mediaTypes, Consumes.class); methodSpec.addAnnotation(ann.build()); } }
private void handleMethodConsumer(MethodSpec.Builder methodSpec, Multimap<String, String> ramlTypeToMediaType, GType typeDeclaration) { Collection<String> mediaTypes = ramlTypeToMediaType.get(typeDeclaration.name()); AnnotationSpec.Builder ann = buildAnnotation(mediaTypes, Consumes.class); methodSpec.addAnnotation(ann.build()); }
public Builder toBuilder() { Builder builder = new Builder(type); for (Map.Entry<String, List<CodeBlock>> entry : members.entrySet()) { builder.members.put(entry.getKey(), new ArrayList<>(entry.getValue())); } return builder; }
public static Builder builder(ClassName type) { checkNotNull(type, "type == null"); return new Builder(type); }
public Builder toBuilder() { Builder builder = new Builder(type); for (Map.Entry<String, List<CodeBlock>> entry : members.entrySet()) { builder.members.put(entry.getKey(), new ArrayList<>(entry.getValue())); } return builder; }
public static Builder builder(ClassName type) { checkNotNull(type, "type == null"); return new Builder(type); }
private void createFactoryClass() throws IOException { final TypeName configurationBuilderFactory = Types.CONFIGURATION_BUILDER_FACTORY; Strings.writeClass(processingEnv.getFiler(), TypeSpec.classBuilder(factoryName) .addModifiers(Modifier.PUBLIC) .addSuperinterface(configurationBuilderFactory) .addAnnotation(AnnotationSpec.builder(AutoService.class).addMember("value", "$T.class", configurationBuilderFactory).build()) .addMethod(Types.overriding(Types.getOnlyMethod(processingEnv, Strings.CONFIGURATION_BUILDER_FACTORY)) .addAnnotation(Types.NON_NULL) .addStatement("return new $T($L)", ClassName.get(PACKAGE, builderName), PARAM_0) .build()) .build()); } }
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; }