com.google.auto.common.SuperficialValidation Java Examples

The following examples show how to use com.google.auto.common.SuperficialValidation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EasyMVPProcessor.java    From EasyMVP with Apache License 2.0 6 votes vote down vote up
private void parsePresenterInjection(Element element,
                                     Map<TypeElement, DelegateClassGenerator> delegateClassMap) {
    //TODO print errors
    if (!SuperficialValidation.validateElement(element)) {
        error("Superficial validation error for %s", element.getSimpleName());
        return;
    }
    if (Validator.isPrivate(element)) {
        error("%s can't be private", element.getSimpleName());
        return;
    }
    VariableElement variableElement = (VariableElement) element;
    DelegateClassGenerator delegateClassGenerator =
            getDelegate((TypeElement) element.getEnclosingElement(),
                        delegateClassMap);
    delegateClassGenerator.setViewPresenterField(variableElement.getSimpleName().toString());
    if (variableElement.getAnnotation(Inject.class) != null) {
        delegateClassGenerator.injectablePresenterInView(true);
    }
    delegateClassGenerator.setPresenterTypeInView(variableElement.asType().toString());
}
 
Example #2
Source File: ButterKnifeProcessor.java    From butterknife with Apache License 2.0 6 votes vote down vote up
private void findAndParseListener(RoundEnvironment env,
    Class<? extends Annotation> annotationClass,
    Map<TypeElement, BindingSet.Builder> builderMap, Set<TypeElement> erasedTargetNames) {
  for (Element element : env.getElementsAnnotatedWith(annotationClass)) {
    if (!SuperficialValidation.validateElement(element)) continue;
    try {
      parseListenerAnnotation(annotationClass, element, builderMap, erasedTargetNames);
    } catch (Exception e) {
      StringWriter stackTrace = new StringWriter();
      e.printStackTrace(new PrintWriter(stackTrace));

      error(element, "Unable to generate view binder for @%s.\n\n%s",
          annotationClass.getSimpleName(), stackTrace.toString());
    }
  }
}
 
Example #3
Source File: AutoValueBuilderProcessor.java    From auto with Apache License 2.0 6 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  TypeElement autoValueBuilder =
      processingEnv.getElementUtils().getTypeElement(AUTO_VALUE_BUILDER_NAME);
  Set<? extends Element> builderTypes = roundEnv.getElementsAnnotatedWith(autoValueBuilder);
  if (!SuperficialValidation.validateElements(builderTypes)) {
    return false;
  }
  for (Element annotatedType : builderTypes) {
    // Double-check that the annotation is there. Sometimes the compiler gets confused in case of
    // erroneous source code. SuperficialValidation should protect us against this but it doesn't
    // cost anything to check again.
    if (hasAnnotationMirror(annotatedType, AUTO_VALUE_BUILDER_NAME)) {
      validate(
          annotatedType,
          "@AutoValue.Builder can only be applied to a class or interface inside an"
              + " @AutoValue class");
    }
  }
  return false;
}
 
Example #4
Source File: AnnotationParsers.java    From convalida with Apache License 2.0 6 votes vote down vote up
private static void parseGenericValidation(
        Class<? extends Annotation> annotation,
        RoundEnvironment env,
        Set<Element> parents,
        List<ValidationField> fields,
        ValidationCallBack validationCallBack
) {
    for (Element element : env.getElementsAnnotatedWith(annotation)) {
        if (!SuperficialValidation.validateElement(element)) continue;
        try {
            validationCallBack.execute(element, parents, fields);
        } catch (Exception e) {
            logParsingError(element, annotation, e);
        }
    }
}
 
Example #5
Source File: EasyMVPProcessor.java    From EasyMVP with Apache License 2.0 6 votes vote down vote up
private void parseConductorController(Element element,
                                      Map<TypeElement, DelegateClassGenerator> delegateClassMap) {
    if (!SuperficialValidation.validateElement(element)) {
        error("Superficial validation error for %s", element.getSimpleName());
        return;
    }
    if (!Validator.isNotAbstractClass(element)) {
        error("%s is abstract", element.getSimpleName());
        return;
    }
    if (!Validator.isSubType(element, CONDUCTOR_CONTROLLER_CLASS_NAME, processingEnv)) {
        error("%s must extend View", element.getSimpleName());
        return;
    }
    //getEnclosing for class type will returns its package/
    TypeElement enclosingElement = (TypeElement) element;
    DelegateClassGenerator delegateClassGenerator =
            getDelegate(enclosingElement, delegateClassMap);
    delegateClassGenerator.setViewType(ViewType.CONDUCTOR_CONTROLLER);
    ConductorController annotation = element.getAnnotation(ConductorController.class);
    try {
        annotation.presenter();
    } catch (MirroredTypeException mte) {
        parsePresenter(delegateClassGenerator, mte);
    }
}
 
Example #6
Source File: AnnotationParsers.java    From convalida with Apache License 2.0 6 votes vote down vote up
private static void parseGenericElement(
        Class<? extends Annotation> annotation,
        RoundEnvironment env,
        Set<Element> parents,
        List<Element> action,
        ElementCallBack elementCallBack
) {
    for (Element element : env.getElementsAnnotatedWith(annotation)) {
        if (!SuperficialValidation.validateElement(element)) continue;
        try {
            elementCallBack.execute(element, parents, action);
        } catch (Exception e) {
            logParsingError(element, annotation, e);
        }
    }
}
 
Example #7
Source File: EasyMVPProcessor.java    From EasyMVP with Apache License 2.0 5 votes vote down vote up
private void parseFragmentView(Element element,
                               Map<TypeElement, DelegateClassGenerator> delegateClassMap) {
    //TODO print errors
    if (!SuperficialValidation.validateElement(element)) {
        error("Superficial validation error for %s", element.getSimpleName());
        return;
    }
    if (!Validator.isNotAbstractClass(element)) {
        error("%s is abstract", element.getSimpleName());
        return;
    }
    boolean isFragment =
            Validator.isSubType(element, ANDROID_FRAGMENT_CLASS_NAME, processingEnv);
    boolean isSupportFragment =
            Validator.isSubType(element, ANDROID_SUPPORT_FRAGMENT_CLASS_NAME, processingEnv);
    if (!isFragment && !isSupportFragment) {
        error("%s must extend Fragment or support Fragment", element.getSimpleName());
        return;
    }
    //getEnclosing for class type will returns its package/
    TypeElement enclosingElement = (TypeElement) element;
    DelegateClassGenerator delegateClassGenerator =
            getDelegate(enclosingElement, delegateClassMap);
    if (isFragment) {
        delegateClassGenerator.setViewType(ViewType.FRAGMENT);
    } else {
        delegateClassGenerator.setViewType(ViewType.SUPPORT_FRAGMENT);
    }
    FragmentView annotation = element.getAnnotation(FragmentView.class);
    try {
        annotation.presenter();
    } catch (MirroredTypeException mte) {
        parsePresenter(delegateClassGenerator, mte);
    }
}
 
Example #8
Source File: EasyMVPProcessor.java    From EasyMVP with Apache License 2.0 5 votes vote down vote up
private void parseCustomView(Element element,
                             Map<TypeElement, DelegateClassGenerator> delegateClassMap) {
    //TODO print errors
    if (!SuperficialValidation.validateElement(element)) {
        error("Superficial validation error for %s", element.getSimpleName());
        return;
    }
    if (!Validator.isNotAbstractClass(element)) {
        error("%s is abstract", element.getSimpleName());
        return;
    }
    if (!Validator.isSubType(element, ANDROID_CUSTOM_VIEW_CLASS_NAME, processingEnv)) {
        error("%s must extend View", element.getSimpleName());
        return;
    }

    //getEnclosing for class type will returns its package/
    TypeElement enclosingElement = (TypeElement) element;
    DelegateClassGenerator delegateClassGenerator =
            getDelegate(enclosingElement, delegateClassMap);
    delegateClassGenerator.setViewType(ViewType.CUSTOM_VIEW);

    CustomView annotation = element.getAnnotation(CustomView.class);
    try {
        annotation.presenter();
    } catch (MirroredTypeException mte) {
        parsePresenter(delegateClassGenerator, mte);
    }
}
 
Example #9
Source File: EasyMVPProcessor.java    From EasyMVP with Apache License 2.0 5 votes vote down vote up
private void parseActivityView(Element element,
                               Map<TypeElement, DelegateClassGenerator> delegateClassMap) {
    //TODO print errors
    if (!SuperficialValidation.validateElement(element)) {
        error("Superficial validation error for %s", element.getSimpleName());
        return;
    }
    if (!Validator.isNotAbstractClass(element)) {
        error("%s is abstract", element.getSimpleName());
        return;
    }
    boolean isActivity =
            Validator.isSubType(element, ANDROID_ACTIVITY_CLASS_NAME, processingEnv);
    boolean isSupportActivity =
            Validator.isSubType(element, ANDROID_SUPPORT_ACTIVITY_CLASS_NAME, processingEnv);
    if (!isActivity && !isSupportActivity) {
        error("%s must extend Activity or AppCompatActivity", element.getSimpleName());
        return;
    }
    //getEnclosing for class type will returns its package/
    TypeElement enclosingElement = (TypeElement) element;
    DelegateClassGenerator delegateClassGenerator =
            getDelegate(enclosingElement, delegateClassMap);
    ActivityView annotation = element.getAnnotation(ActivityView.class);
    delegateClassGenerator.setResourceID(annotation.layout());
    if (isSupportActivity) {
        delegateClassGenerator.setViewType(ViewType.SUPPORT_ACTIVITY);
    } else {
        delegateClassGenerator.setViewType(ViewType.ACTIVITY);
    }
    try {
        annotation.presenter();
    } catch (MirroredTypeException mte) {
        parsePresenter(delegateClassGenerator, mte);
    }
}
 
Example #10
Source File: SnakeProcessor.java    From Snake with Apache License 2.0 5 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment env) {
    for(Element element : env.getElementsAnnotatedWith(EnableDragToClose.class)) {
        if(!SuperficialValidation.validateElement(element)) continue;

        parseEnableDragToClose(element);
    }

    return false;
}
 
Example #11
Source File: EasyMVPProcessor.java    From EasyMVP with Apache License 2.0 5 votes vote down vote up
private void parsePresenterId(Element element,
                              Map<TypeElement, DelegateClassGenerator> delegateClassMap) {
    if (!SuperficialValidation.validateElement(element)) {
        error("Superficial validation error for %s", element.getSimpleName());
        return;
    }
    if (Validator.isPrivate(element)) {
        error("%s can't be private", element.getSimpleName());
        return;
    }
    if (Validator.isMethod(element)) {
        ExecutableType emeth = (ExecutableType) element.asType();
        if (!emeth.getReturnType().getKind().equals(TypeKind.LONG) &&
                !emeth.getReturnType().getKind().equals(TypeKind.INT)) {
            error("%s must have return type int or long", element);
        }
    } else {
        TypeKind kind = element.asType().getKind();
        if (kind != TypeKind.INT && kind != TypeKind.LONG) {
            error("%s must be int or long", element.getSimpleName());
            return;
        }
    }
    String presenterId = element.toString();
    DelegateClassGenerator delegateClassGenerator =
            getDelegate((TypeElement) element.getEnclosingElement(),
                    delegateClassMap);
    delegateClassGenerator.setPresenterId(presenterId);
}
 
Example #12
Source File: RetroFacebookBuilderProcessor.java    From RetroFacebook with Apache License 2.0 5 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  Set<? extends Element> builderTypes =
      roundEnv.getElementsAnnotatedWith(RetroFacebook.Builder.class);
  if (!SuperficialValidation.validateElements(builderTypes)) {
    return false;
  }
  for (Element annotatedType : builderTypes) {
    // Double-check that the annotation is there. Sometimes the compiler gets confused in case of
    // erroneous source code. SuperficialValidation should protect us against this but it doesn't
    // cost anything to check again.
    if (isAnnotationPresent(annotatedType, RetroFacebook.Builder.class)) {
      validate(
          annotatedType,
          "@RetroFacebook.Builder can only be applied to a class or interface inside an"
              + " @RetroFacebook class");
    }
  }

  Set<? extends Element> validateMethods =
      roundEnv.getElementsAnnotatedWith(RetroFacebook.Validate.class);
  if (!SuperficialValidation.validateElements(validateMethods)) {
    return false;
  }
  for (Element annotatedMethod : validateMethods) {
    if (isAnnotationPresent(annotatedMethod, RetroFacebook.Validate.class)) {
      validate(
          annotatedMethod,
          "@RetroFacebook.Validate can only be applied to a method inside an @RetroFacebook class");
    }
  }
  return false;
}
 
Example #13
Source File: RetroFacebookBuilderProcessor.java    From RetroFacebook with Apache License 2.0 5 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  Set<? extends Element> builderTypes =
      roundEnv.getElementsAnnotatedWith(RetroFacebook.Builder.class);
  if (!SuperficialValidation.validateElements(builderTypes)) {
    return false;
  }
  for (Element annotatedType : builderTypes) {
    // Double-check that the annotation is there. Sometimes the compiler gets confused in case of
    // erroneous source code. SuperficialValidation should protect us against this but it doesn't
    // cost anything to check again.
    if (isAnnotationPresent(annotatedType, RetroFacebook.Builder.class)) {
      validate(
          annotatedType,
          "@RetroFacebook.Builder can only be applied to a class or interface inside an"
              + " @RetroFacebook class");
    }
  }

  Set<? extends Element> validateMethods =
      roundEnv.getElementsAnnotatedWith(RetroFacebook.Validate.class);
  if (!SuperficialValidation.validateElements(validateMethods)) {
    return false;
  }
  for (Element annotatedMethod : validateMethods) {
    if (isAnnotationPresent(annotatedMethod, RetroFacebook.Validate.class)) {
      validate(
          annotatedMethod,
          "@RetroFacebook.Validate can only be applied to a method inside an @RetroFacebook class");
    }
  }
  return false;
}
 
Example #14
Source File: KratosProcessor.java    From Kratos with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void parseOnChangedTarget(RoundEnvironment env, Map<TypeElement, BindingClass> targetClassMap, Set<String> erasedTargetNames, Class... annotationClasses) {
    for (Class clazz : annotationClasses) {
        for (Element element : env.getElementsAnnotatedWith(clazz)) {
            if (!SuperficialValidation.validateElement(element)) continue;
            try {
                parseOnChanged(element, targetClassMap, erasedTargetNames, clazz);
            } catch (Exception e) {
                logParsingError(element, clazz, e);
            }
        }
    }
}
 
Example #15
Source File: RetroWeiboBuilderProcessor.java    From SimpleWeibo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  Set<? extends Element> builderTypes =
      roundEnv.getElementsAnnotatedWith(RetroWeibo.Builder.class);
  if (!SuperficialValidation.validateElements(builderTypes)) {
    return false;
  }
  for (Element annotatedType : builderTypes) {
    // Double-check that the annotation is there. Sometimes the compiler gets confused in case of
    // erroneous source code. SuperficialValidation should protect us against this but it doesn't
    // cost anything to check again.
    if (isAnnotationPresent(annotatedType, RetroWeibo.Builder.class)) {
      validate(
          annotatedType,
          "@RetroWeibo.Builder can only be applied to a class or interface inside an"
              + " @RetroWeibo class");
    }
  }

  Set<? extends Element> validateMethods =
      roundEnv.getElementsAnnotatedWith(RetroWeibo.Validate.class);
  if (!SuperficialValidation.validateElements(validateMethods)) {
    return false;
  }
  for (Element annotatedMethod : validateMethods) {
    if (isAnnotationPresent(annotatedMethod, RetroWeibo.Validate.class)) {
      validate(
          annotatedMethod,
          "@RetroWeibo.Validate can only be applied to a method inside an @RetroWeibo class");
    }
  }
  return false;
}
 
Example #16
Source File: AndroidRouterProcessor.java    From Android-Router with Apache License 2.0 5 votes vote down vote up
private List<JavaFile> findAndParseTargets(RoundEnvironment env) {
    List<JavaFile> javaFiles = new ArrayList<>();

    // Process each @RouterModule element.
    for (Element e : env.getElementsAnnotatedWith(RouterModule.class)) {
        if (!SuperficialValidation.validateElement(e))
            continue;
        List<? extends Element> allEle = e.getEnclosedElements();
        parseRouterModule(e, allEle, javaFiles);
    }
    return javaFiles;
}
 
Example #17
Source File: ProducerModuleProcessingStep.java    From dagger2-sample with Apache License 2.0 4 votes vote down vote up
@Override
public void process(SetMultimap<Class<? extends Annotation>, Element> elementsByAnnotation) {
  // first, check and collect all produces methods
  ImmutableSet.Builder<ExecutableElement> validProducesMethodsBuilder = ImmutableSet.builder();
  for (Element producesElement : elementsByAnnotation.get(Produces.class)) {
    if (producesElement.getKind().equals(METHOD)) {
      ExecutableElement producesMethodElement = (ExecutableElement) producesElement;
      ValidationReport<ExecutableElement> methodReport =
          producesMethodValidator.validate(producesMethodElement);
      methodReport.printMessagesTo(messager);
      if (methodReport.isClean()) {
        validProducesMethodsBuilder.add(producesMethodElement);
      }
    }
  }
  ImmutableSet<ExecutableElement> validProducesMethods = validProducesMethodsBuilder.build();

  // process each module
  for (Element moduleElement :
      Sets.difference(elementsByAnnotation.get(ProducerModule.class),
          processedModuleElements)) {
    if (SuperficialValidation.validateElement(moduleElement)) {
      ValidationReport<TypeElement> report =
          moduleValidator.validate(MoreElements.asType(moduleElement));
      report.printMessagesTo(messager);

      if (report.isClean()) {
        ImmutableSet.Builder<ExecutableElement> moduleProducesMethodsBuilder =
            ImmutableSet.builder();
        List<ExecutableElement> moduleMethods =
            ElementFilter.methodsIn(moduleElement.getEnclosedElements());
        for (ExecutableElement methodElement : moduleMethods) {
          if (isAnnotationPresent(methodElement, Produces.class)) {
            moduleProducesMethodsBuilder.add(methodElement);
          }
        }
        ImmutableSet<ExecutableElement> moduleProducesMethods =
            moduleProducesMethodsBuilder.build();

        if (Sets.difference(moduleProducesMethods, validProducesMethods).isEmpty()) {
          // all of the produces methods in this module are valid!
          // time to generate some factories!
          ImmutableSet<ProductionBinding> bindings = FluentIterable.from(moduleProducesMethods)
              .transform(new Function<ExecutableElement, ProductionBinding>() {
                @Override
                public ProductionBinding apply(ExecutableElement producesMethod) {
                  return productionBindingFactory.forProducesMethod(producesMethod,
                      producesMethod.getEnclosingElement().asType());
                }
              })
              .toSet();

          try {
            for (ProductionBinding binding : bindings) {
              factoryGenerator.generate(binding);
            }
          } catch (SourceFileGenerationException e) {
            e.printMessageTo(messager);
          }
        }
      }

      processedModuleElements.add(moduleElement);
    }
  }
}