private Stream<TypeElement> getTargetClasses(InspectorFactory factory) { try { factory.include(); } catch (MirroredTypesException e) { //noinspection Convert2Lambda this doesn't work on CI as a lambda/method ref ಠ_ಠ return e.getTypeMirrors() .stream() .map(new Function<TypeMirror, String>() { @Override public String apply(TypeMirror typeMirror) { return typeMirror.toString(); } }) .map(name -> elementUtils.getTypeElement(name)); } throw new RuntimeException( "Could not inspect factory includes. Java annotation processing is weird."); }
/** * Return the list of {@link VueComponent} dependencies listed on {@link * Component#components()}. * When retrieving this list, it can produce a {@link MirroredTypesException}. * The case is managed here and we always returns {@link TypeMirror} of the dependencies. * @param elementsUtil The Element utils provided by the annotation processor environement. * @param component The {@link Component} annotation to process * @return The list of TypeMirror of the {@link VueComponent} the {@link Component} depends on */ public static List<TypeMirror> getComponentLocalComponents(Elements elementsUtil, TypeElement component) { Component componentAnnotation = component.getAnnotation(Component.class); try { Class<?>[] componentsClass = componentAnnotation.components(); return Stream .of(componentsClass) .map(Class::getCanonicalName) .map(elementsUtil::getTypeElement) .map(TypeElement::asType) .collect(Collectors.toList()); } catch (MirroredTypesException mte) { return new LinkedList<>(mte.getTypeMirrors()); } }
/** * Return the list of {@link CustomizeOptions} that will be used to customize the options * before passing them down to Vue.js. * When retrieving this list, it can produce a {@link MirroredTypesException}. * The case is managed here and we always returns {@link TypeMirror} of the items. * @param elementsUtil The Element utils provided by the annotation processor environement. * @param component The {@link Component} annotation to process * @return The list of TypeMirror of the {@link CustomizeOptions} the {@link Component} depends on */ public static List<TypeMirror> getComponentCustomizeOptions(Elements elementsUtil, TypeElement component) { Component componentAnnotation = component.getAnnotation(Component.class); try { Class<?>[] componentsClass = componentAnnotation.customizeOptions(); return Stream .of(componentsClass) .map(Class::getCanonicalName) .map(elementsUtil::getTypeElement) .map(TypeElement::asType) .collect(Collectors.toList()); } catch (MirroredTypesException mte) { return new LinkedList<>(mte.getTypeMirrors()); } }
/** * Retrieves {@link ClassName}s from {@code annotation} with {@code getter} and if exception is thrown, retrieves it from the exception. */ public static <A> List<ClassName> getParamClasses(final A annotation, final IGetter<A, Class<?>[]> getter) { List<ClassName> className = new ArrayList<>(); try { Class<?>[] classes = getter.get(annotation); for (Class<?> cls : classes) { className.add(ClassName.get(cls)); } } catch (MirroredTypesException mte) { try { for (TypeMirror typeMirror : mte.getTypeMirrors()) { className.add((ClassName) ClassName.get(((DeclaredType) typeMirror).asElement().asType())); } } catch (Exception e) { // if there is 'primitive'.class } } return className; }
public static List<? extends TypeMirror> getOnBindEachTypeMirrors(Element action) { List<? extends TypeMirror> bindClasses; try { action.getAnnotation(se.snylt.witch.annotations.OnBindEach.class).value(); return null; } catch (MirroredTypesException mte) { bindClasses = mte.getTypeMirrors(); } return bindClasses; }
private boolean parseBindsInternal(DataBindingInfo info, String varName, int index,String[] props, BindMethod[] methods) { final ProcessorPrinter pp = mContext.getProcessorPrinter(); if(props.length == 0){ pp.error(TAG, "parseBindsProperty", "props.length must > 0"); return false; } if(props.length > methods.length){ pp.error(TAG, "parseBindsProperty", "props.length can't > methods.length"); return false; } //truncate if props.length < methods.length if(props.length < methods.length){ methods = truncate(methods, props.length); } for(int i =0 ; i < props.length ; i ++){ final String prop = props[i]; if(prop == null || prop.isEmpty()){ continue; } List<String> types = null; //read Class<?> in compile time is wrong. see https://area-51.blog/2009/02/13/getting-class-values-from-annotations-in-an-annotationprocessor/. try { methods[i].paramTypes(); }catch (MirroredTypesException mte){ List<? extends TypeMirror> mirrors = mte.getTypeMirrors(); types = convertToClassname(mirrors, null); } info.addBindInfo(new DataBindingInfo.BindInfo(varName, prop, index, methods[i].value(), types)); } return true; }
/** * Register directives passed to the annotation. * @param annotation The Component annotation on the Component we generate for * @param injectDependenciesBuilder The builder for the injectDependencies method */ private void registerLocalDirectives(Component annotation, MethodSpec.Builder injectDependenciesBuilder) { try { Class<?>[] componentsClass = annotation.directives(); if (componentsClass.length > 0) addGetDirectivesStatement(injectDependenciesBuilder); Stream .of(componentsClass) .forEach(clazz -> injectDependenciesBuilder.addStatement( "directives.set($S, new $T())", directiveToTagName(clazz.getName()), directiveOptionsName(clazz))); } catch (MirroredTypesException mte) { List<DeclaredType> classTypeMirrors = (List<DeclaredType>) mte.getTypeMirrors(); if (!classTypeMirrors.isEmpty()) addGetDirectivesStatement(injectDependenciesBuilder); classTypeMirrors.forEach(classTypeMirror -> { TypeElement classTypeElement = (TypeElement) classTypeMirror.asElement(); injectDependenciesBuilder.addStatement("directives.set($S, new $T())", directiveToTagName(classTypeElement.getSimpleName().toString()), directiveOptionsName(classTypeElement)); }); } }
@SuppressWarnings("unchecked") private List<TypeMirror> getParameters(RoutableActivity annotation) { try { annotation.params(); // TODO get forResult } catch (MirroredTypesException e) { return (List<TypeMirror>) e.getTypeMirrors(); } return null; }
@SuppressWarnings("unchecked") private List<TypeMirror> getParameters(RoutableView annotation) { try { annotation.params(); // TODO get forResult } catch (MirroredTypesException e) { return (List<TypeMirror>) e.getTypeMirrors(); } return null; }
private List<ClassName> getValidTypes(Transformer tAnnot) { List<ClassName> validTypes = new ArrayList<>(); try { // The code smells are strong here, thanks Java API devs... tAnnot.validArgTypes(); } catch (MirroredTypesException e) { for (TypeMirror mirror : e.getTypeMirrors()) validTypes.add(ClassName.get(MoreTypes.asTypeElement(mirror))); } return validTypes; }
@SuppressWarnings("unchecked") public List<DeclaredType> getClassArrayFromAnnotationMethod(Supplier<Class<?>[]> supplier) { // JDK suggested way of getting type mirrors, do not waste time here, // just move on try { supplier.get(); } catch (MirroredTypesException e) { // types WILL be declared return (List<DeclaredType>) e.getTypeMirrors(); } return Collections.emptyList(); }
public EventHandlerHolder parse(TypeElement element) { final ru.noties.handle.annotations.EventHandler eventHandler = element.getAnnotation(ru.noties.handle.annotations.EventHandler.class); if (eventHandler == null) { return null; } final List<String> events = new ArrayList<>(); try { final Class<?>[] classes = eventHandler.value(); for (Class<?> c: classes) { events.add(c.getCanonicalName()); } } catch (MirroredTypesException e) { final List<? extends TypeMirror> typeMirrors = e.getTypeMirrors(); if (typeMirrors != null && typeMirrors.size() > 0) { for (TypeMirror mirror: typeMirrors) { events.add(mirror.toString()); } } } if (events.size() == 0) { return null; } return new EventHandlerHolder(element, events); }
private List<? extends TypeMirror> getReflectClasses(Reflected reflected) { try { reflected.classes(); } catch(MirroredTypesException mte) { return mte.getTypeMirrors(); } return new ArrayList<>(); }
private List<? extends TypeMirror> getIgnoreClasses(Reflected reflected) { try { reflected.ignored(); } catch(MirroredTypesException mte) { return mte.getTypeMirrors(); } return new ArrayList<>(); }
private static Matcher<Tree> shouldAllow(RestrictedApi api, VisitorState state) { try { return anyAnnotation(api.whitelistAnnotations()); } catch (MirroredTypesException e) { return anyAnnotation(e.getTypeMirrors(), state); } }
private static Matcher<Tree> shouldAllowWithWarning(RestrictedApi api, VisitorState state) { try { return anyAnnotation(api.whitelistWithWarningAnnotations()); } catch (MirroredTypesException e) { return anyAnnotation(e.getTypeMirrors(), state); } }
protected RuntimeException generateException() { return new MirroredTypesException(types); }
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { Set<? extends Element> reactModuleListElements = roundEnv.getElementsAnnotatedWith( ReactModuleList.class); for (Element reactModuleListElement : reactModuleListElements) { if (!(reactModuleListElement instanceof TypeElement)) { continue; } TypeElement typeElement = (TypeElement) reactModuleListElement; ReactModuleList reactModuleList = typeElement.getAnnotation(ReactModuleList.class); if (reactModuleList == null) { continue; } ClassName className = ClassName.get(typeElement); String packageName = ClassName.get(typeElement).packageName(); String fileName = className.simpleName(); List<String> nativeModules = new ArrayList<>(); try { reactModuleList.nativeModules(); // throws MirroredTypesException } catch (MirroredTypesException mirroredTypesException) { List<? extends TypeMirror> typeMirrors = mirroredTypesException.getTypeMirrors(); for (TypeMirror typeMirror : typeMirrors) { nativeModules.add(typeMirror.toString()); } } MethodSpec getReactModuleInfosMethod; try { getReactModuleInfosMethod = MethodSpec.methodBuilder("getReactModuleInfos") .addAnnotation(Override.class) .addModifiers(PUBLIC) .addCode(getCodeBlockForReactModuleInfos(nativeModules)) .returns(MAP_TYPE) .build(); } catch (ReactModuleSpecException reactModuleSpecException) { mMessager.printMessage(ERROR, reactModuleSpecException.mMessage); return false; } TypeSpec reactModulesInfosTypeSpec = TypeSpec.classBuilder( fileName + "$$ReactModuleInfoProvider") .addModifiers(Modifier.PUBLIC) .addMethod(getReactModuleInfosMethod) .addSuperinterface(ReactModuleInfoProvider.class) .build(); JavaFile javaFile = JavaFile.builder(packageName, reactModulesInfosTypeSpec) .addFileComment("Generated by " + getClass().getName()) .build(); try { javaFile.writeTo(mFiler); } catch (IOException e) { e.printStackTrace(); } } return true; }
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { Set<? extends Element> reactModuleListElements = roundEnv.getElementsAnnotatedWith( ReactModuleList.class); for (Element reactModuleListElement : reactModuleListElements) { TypeElement typeElement = (TypeElement) reactModuleListElement; ClassName className = ClassName.get(typeElement); String packageName = ClassName.get(typeElement).packageName(); String fileName = className.simpleName(); ReactModuleList reactModuleList = typeElement.getAnnotation(ReactModuleList.class); List<String> nativeModules = new ArrayList<>(); try { reactModuleList.value(); // throws MirroredTypesException } catch (MirroredTypesException mirroredTypesException) { List<? extends TypeMirror> typeMirrors = mirroredTypesException.getTypeMirrors(); for (TypeMirror typeMirror : typeMirrors) { nativeModules.add(typeMirror.toString()); } } MethodSpec getReactModuleInfosMethod; try { getReactModuleInfosMethod = MethodSpec.methodBuilder("getReactModuleInfos") .addAnnotation(Override.class) .addModifiers(PUBLIC) .addCode(getCodeBlockForReactModuleInfos(nativeModules)) .returns(MAP_TYPE) .build(); } catch (ReactModuleSpecException reactModuleSpecException) { mMessager.printMessage(ERROR, reactModuleSpecException.mMessage); return false; } TypeSpec reactModulesInfosTypeSpec = TypeSpec.classBuilder( fileName + "$$ReactModuleInfoProvider") .addModifiers(Modifier.PUBLIC) .addMethod(getReactModuleInfosMethod) .addSuperinterface(ReactModuleInfoProvider.class) .build(); JavaFile javaFile = JavaFile.builder(packageName, reactModulesInfosTypeSpec) .addFileComment("Generated by " + getClass().getName()) .build(); try { javaFile.writeTo(mFiler); } catch (IOException e) { e.printStackTrace(); } } return true; }
/** * Convert an annotation member value from JDT into Reflection, and from whatever its actual type * is into whatever type the reflective invoker of a method is expecting. * <p> * Only certain types are permitted as member values. Specifically, a member must be a constant, * and must be either a primitive type, String, Class, an enum constant, an annotation, or an * array of any of those. Multidimensional arrays are not permitted. * * @param actualValue the value as represented by {@link ElementValuePair#getValue()} * @param actualType the return type of the corresponding {@link MethodBinding} * @param expectedType the type that the reflective method invoker is expecting * @return an object of the expected type representing the annotation member value, * or an appropriate dummy value (such as null) if no value is available */ private Object getReflectionValue(Object actualValue, TypeBinding actualType, Class<?> expectedType) { if (null == expectedType) { // With no expected type, we can't even guess at a conversion return null; } if (null == actualValue) { // Return a type-appropriate equivalent of null return Factory.getMatchingDummyValue(expectedType); } if (expectedType.isArray()) { if (Class.class.equals(expectedType.getComponentType())) { // package Class[]-valued return as a MirroredTypesException if (actualType.isArrayType() && actualValue instanceof Object[] && ((ArrayBinding)actualType).leafComponentType.erasure().id == TypeIds.T_JavaLangClass) { Object[] bindings = (Object[])actualValue; List<TypeMirror> mirrors = new ArrayList<TypeMirror>(bindings.length); for (int i = 0; i < bindings.length; ++i) { if (bindings[i] instanceof TypeBinding) { mirrors.add(_env.getFactory().newTypeMirror((TypeBinding)bindings[i])); } } throw new MirroredTypesException(mirrors); } // TODO: actual value is not a TypeBinding[]. Should we return a TypeMirror[] around an ErrorType? return null; } // Handle arrays of types other than Class, e.g., int[], MyEnum[], ... return convertJDTArrayToReflectionArray(actualValue, actualType, expectedType); } else if (Class.class.equals(expectedType)) { // package the Class-valued return as a MirroredTypeException if (actualValue instanceof TypeBinding) { TypeMirror mirror = _env.getFactory().newTypeMirror((TypeBinding)actualValue); throw new MirroredTypeException(mirror); } else { // TODO: actual value is not a TypeBinding. Should we return a TypeMirror around an ErrorType? return null; } } else { // Handle unitary values of type other than Class, e.g., int, MyEnum, ... return convertJDTValueToReflectionType(actualValue, actualType, expectedType); } }