private static TypeMirror getMyValue1(MyAnnotation annotation) { try { annotation.myValue(); // this should throw } catch( MirroredTypeException mte ) { return mte.getTypeMirror(); } return null; // can this ever happen ?? }
protected RuntimeException generateException() { return new MirroredTypeException(type); } }
@Before public void setup() throws IOException { this.processor = new SVGShapeProcessor(); this.processor.init(processingEnvironment); when(processingEnvironment.getElementUtils()).thenReturn(elements); when(processingEnvironment.getMessager()).thenReturn(messager); when(processingEnvironment.getFiler()).thenReturn(filer); final JavaFileObject jfo = mock(JavaFileObject.class); when(filer.createSourceFile(any(CharSequence.class))).thenReturn(jfo); when(jfo.openWriter()).thenReturn(mock(Writer.class)); doThrow(new MirroredTypeException(svgViewFactoryBuilder)).when(svgViewFactory).builder(); }
static Element getElementByName(String name, Elements elements, Types types) { try { return elements.getTypeElement(name); } catch (MirroredTypeException mte) { return types.asElement(mte.getTypeMirror()); } }
@SuppressWarnings("unchecked") public <T> T extractAnnotationParameter(Element element, String annotationName, String methodName) { Annotation annotation; try { annotation = element.getAnnotation((Class<? extends Annotation>) Class.forName(annotationName)); } catch (ClassNotFoundException e) { throw new RuntimeException("Could not load annotation class " + annotationName, e); } Method method; try { method = annotation.getClass().getMethod(methodName); return (T) method.invoke(annotation); } catch (InvocationTargetException e) { if (e.getCause() instanceof MirroredTypeException) { MirroredTypeException cause = (MirroredTypeException) e.getCause(); return (T) cause.getTypeMirror(); } else { throw new RuntimeException(e); } } catch (Exception e) { throw new RuntimeException(e); } }
@Nullable public static TypeElement getTestSpecValue(TypeElement element) { try { element.getAnnotation(TestSpec.class).value(); } catch (MirroredTypeException e) { final TypeMirror typeMirror = e.getTypeMirror(); return (TypeElement) ((Type.ClassType) typeMirror).asElement(); } return null; } }
/** * Returns the {@link Class} instance for the {@link Matcher} associated with the provided {@link * NotMatches} annotation. This roundabout solution is recommended and explained by {@link * Element#getAnnotation(Class)}. */ static Class<? extends Matcher<? super ExpressionTree>> getValue(NotMatches matches) { String name; try { matches.value(); throw new RuntimeException("unreachable"); } catch (MirroredTypeException e) { DeclaredType type = (DeclaredType) e.getTypeMirror(); name = ((TypeElement) type.asElement()).getQualifiedName().toString(); } try { return asSubclass(Class.forName(name), new TypeToken<Matcher<? super ExpressionTree>>() {}); } catch (ClassNotFoundException | ClassCastException e) { throw new RuntimeException(e); } }
/** * Returns the {@link Class} instance for the {@link Matcher} associated with the provided {@link * Matches} annotation. This roundabout solution is recommended and explained by {@link * Element#getAnnotation(Class)}. */ static Class<? extends Matcher<? super ExpressionTree>> getValue(Matches matches) { String name; try { matches.value(); throw new RuntimeException("unreachable"); } catch (MirroredTypeException e) { DeclaredType type = (DeclaredType) e.getTypeMirror(); name = ((TypeElement) type.asElement()).getQualifiedName().toString(); } try { return asSubclass(Class.forName(name), new TypeToken<Matcher<? super ExpressionTree>>() {}); } catch (ClassNotFoundException | ClassCastException e) { throw new RuntimeException(e); } }
private static boolean isFromEventTypeSpecifiedInAnnotation( MethodParamModel methodParamModel, TypeName eventFieldType) { FromEvent fromEvent = (FromEvent) MethodParamModelUtils.getAnnotation(methodParamModel, FromEvent.class); TypeName baseClassType; try { baseClassType = ClassName.get(fromEvent.baseClass()); } catch (MirroredTypeException mte) { baseClassType = ClassName.get(mte.getTypeMirror()); } return baseClassType.equals(eventFieldType); } }
@Override public Optional<TypeMirror> builderType() { Optional<Entity> entityAnnotation = annotationOf(Entity.class); if (entityAnnotation.isPresent()) { Entity entity = entityAnnotation.get(); Elements elements = processingEnvironment.getElementUtils(); TypeMirror mirror = null; try { Class<?> builderClass = entity.builder(); // easiest way to get the class TypeMirror if (builderClass != void.class) { mirror = elements.getTypeElement(builderClass.getName()).asType(); } } catch (MirroredTypeException typeException) { mirror = typeException.getTypeMirror(); } if (mirror != null && mirror.getKind() != TypeKind.VOID) { return Optional.of(mirror); } } if (builderFactoryMethod().isPresent()) { return Optional.of(builderFactoryMethod().get().getReturnType()); } return ElementFilter.typesIn(element().getEnclosedElements()).stream() .filter(element -> element.getSimpleName().toString().contains("Builder")) .map(Element::asType) .filter(Objects::nonNull) .filter(type -> type.getKind() != TypeKind.VOID) .findFirst(); }
public ArgumentAnnotatedField(Element element, TypeElement classElement, Arg annotation) throws ProcessingException { this.name = element.getSimpleName().toString(); this.key = getKey(element, annotation); this.type = element.asType().toString(); this.element = element; this.required = annotation.required(); this.classElement = classElement; try { Class<? extends ArgsBundler> clazz = annotation.bundler(); bundlerClass = getFullQualifiedNameByClass(clazz); } catch (MirroredTypeException mte) { TypeMirror baggerClass = mte.getTypeMirror(); bundlerClass = getFullQualifiedNameByTypeMirror(baggerClass); } }
widget.widgetClass(); } catch (MirroredTypeException mte) { TypeMirror nameMirror = mte.getTypeMirror(); if (nameMirror.getKind() == TypeKind.DECLARED) { name = ((DeclaredType) nameMirror).asElement().getSimpleName().toString();
private void processJsonFieldAnnotation(Element element, Map<String, JsonObjectHolder> jsonObjectMap, Elements elements, Types types) { if (!isJsonFieldFieldAnnotationValid(element, elements)) { return; } TypeElement enclosingElement = (TypeElement)element.getEnclosingElement(); JsonObjectHolder objectHolder = jsonObjectMap.get(TypeUtils.getInjectedFQCN(enclosingElement, elements)); JsonFieldHolder fieldHolder = objectHolder.fieldMap.get(element.getSimpleName().toString()); if (fieldHolder == null) { fieldHolder = new JsonFieldHolder(); objectHolder.fieldMap.put(element.getSimpleName().toString(), fieldHolder); } JsonField annotation = element.getAnnotation(JsonField.class); TypeMirror typeConverterType; try { typeConverterType = mProcessingEnv.getElementUtils().getTypeElement(annotation.typeConverter().getCanonicalName()).asType(); } catch (MirroredTypeException mte) { typeConverterType = mte.getTypeMirror(); } String[] fieldName = annotation.name(); JsonIgnore ignoreAnnotation = element.getAnnotation(JsonIgnore.class); boolean shouldParse = ignoreAnnotation == null || ignoreAnnotation.ignorePolicy() == IgnorePolicy.SERIALIZE_ONLY; boolean shouldSerialize = ignoreAnnotation == null || ignoreAnnotation.ignorePolicy() == IgnorePolicy.PARSE_ONLY; String error = fieldHolder.fill(element, elements, types, fieldName, typeConverterType, objectHolder, shouldParse, shouldSerialize); if (!TextUtils.isEmpty(error)) { error(element, error); } ensureTypeConverterClassValid(typeConverterType, elements, types); }
public void createClasses() throws IOException { TypeElement baseBuilder; try { baseBuilder = processingEnv.getElementUtils().getTypeElement(configuration.baseBuilderClass().getName()); } catch (MirroredTypeException e) { baseBuilder = MoreTypes.asTypeElement(e.getTypeMirror()); } final List<Element> elements = new ModelBuilder(baseAnnotation, new ElementFactory(processingEnv.getElementUtils()), baseBuilder, processingEnv.getMessager()).build(); createBuilderClass(elements); createConfigClass(elements); if (configuration.isPlugin()) { createBuilderInterface(elements); createFactoryClass(); } }
table.type(); } catch (MirroredTypeException e) { if (!e.getTypeMirror().toString().equals("void")) { mirror = e.getTypeMirror(); // easiest way to get the mirror
private void register(Element clazz, Class<? extends Annotation> annotation, ServiceProvider svc) { try { svc.service(); assert false; return; } catch (MirroredTypeException e) { register(clazz, annotation, e.getTypeMirror(), svc.path(), svc.position(), svc.supersedes()); } }
private void updateCommandAttributes(CommandSpec result, Command cmd) { // null factory to prevent // javax.lang.model.type.MirroredTypeException: Attempt to access Class object for TypeMirror picocli.CommandLine.NoVersionProvider result.updateCommandAttributes(cmd, null); try { cmd.versionProvider(); } catch (MirroredTypeException ex) { VersionProviderMetaData provider = new VersionProviderMetaData(ex.getTypeMirror()); if (!provider.isDefault()) { result.versionProvider(provider); } } try { cmd.defaultValueProvider(); } catch (MirroredTypeException ex) { DefaultValueProviderMetaData provider = new DefaultValueProviderMetaData(ex.getTypeMirror()); if (!provider.isDefault()) { result.defaultValueProvider(provider); } } }
shortcut.activity(); } catch (MirroredTypeException mte) { if (!(mte.getTypeMirror() instanceof NoType)) { DeclaredType classTypeMirror = (DeclaredType) mte.getTypeMirror(); TypeElement classTypeElement = (TypeElement) classTypeMirror.asElement(); String targetActivityClassName = classTypeElement.getQualifiedName().toString();
static TypeElement extractType(Types typeUtils, TypeMirror typeToExtract) { try { // Throws a MirroredTypeException if the type is not compiled. return (TypeElement) typeUtils.asElement(typeToExtract); } catch (MirroredTypeException mte) { DeclaredType typeMirror = (DeclaredType) mte.getTypeMirror(); return (TypeElement) typeMirror.asElement(); } }
private static String getCanonicalTypeFrom(MirroredTypeException me) { TypeMirror tm = me.getTypeMirror(); if (tm.getKind() == TypeKind.DECLARED) { DeclaredType dec = (DeclaredType) tm; return ((TypeElement)dec.asElement()).getQualifiedName().toString(); } return ""; //ok? }