Пример #1
0
 @Before
 public void setup() {
   ProcessingEnvironment env = ProcessingEnvironmentRunner.getProcessingEnvironment();
   elements = env.getElementUtils();
   TypeMUtils typeMUtils = new TypeMUtils();
   underTest = new BuilderModelProducer(env, typeMUtils);
 }
Пример #2
0
 @Override
 public synchronized void init(ProcessingEnvironment env) {
   super.init(env);
   elementUtils = env.getElementUtils();
   typeUtils = env.getTypeUtils();
   filer = env.getFiler();
 }
Пример #3
0
 @Before
 public void init() {
   ProcessingEnvironment processingEnv = mock(ProcessingEnvironment.class);
   Elements elementUtils = mock(Elements.class);
   when(processingEnv.getElementUtils()).thenReturn(elementUtils);
   processor.init(processingEnv);
 }
Пример #4
0
 public static FunctionMirror newInstance(ExecutableElement method, ProcessingEnvironment env) {
   assertNotNull(env);
   AnnotationMirror annotationMirror =
       ElementUtil.getAnnotationMirror(method, Function.class, env);
   if (annotationMirror == null) {
     return null;
   }
   FunctionMirror result = new FunctionMirror(annotationMirror, method.getSimpleName().toString());
   for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry :
       env.getElementUtils().getElementValuesWithDefaults(annotationMirror).entrySet()) {
     String name = entry.getKey().getSimpleName().toString();
     AnnotationValue value = entry.getValue();
     if ("catalog".equals(name)) {
       result.catalog = value;
     } else if ("schema".equals(name)) {
       result.schema = value;
     } else if ("name".equals(name)) {
       result.name = value;
     } else if ("queryTimeout".equals(name)) {
       result.queryTimeout = value;
     } else if ("mapKeyNaming".equals(name)) {
       result.mapKeyNaming = value;
     } else if ("ensureResultMapping".equals(name)) {
       result.ensureResultMapping = value;
     }
   }
   return result;
 }
 @Override
 public synchronized void init(ProcessingEnvironment processingEnv) {
   super.init(processingEnv);
   note("LensKit Shareable linting active");
   typeUtils = processingEnv.getTypeUtils();
   elementUtils = processingEnv.getElementUtils();
 }
Пример #6
0
 @Override
 public synchronized void init(ProcessingEnvironment env) {
   super.init(env);
   mLogger = new Logger(env);
   mElements = env.getElementUtils();
   mTypes = env.getTypeUtils();
   mFiler = env.getFiler();
 }
Пример #7
0
  @Override
  public synchronized void init(ProcessingEnvironment processingEnv) {
    super.init(processingEnv);

    Elements elementUtils = processingEnv.getElementUtils();
    Types typeUtils = processingEnv.getTypeUtils();
    Filer filer = processingEnv.getFiler();
    Messager messager = processingEnv.getMessager();
  }
Пример #8
0
 private boolean isValidElementFound(String typeName) {
   try {
     @Nullable TypeElement typeElement = processing.getElementUtils().getTypeElement(typeName);
     return typeElement != null && typeElement.asType().getKind() != TypeKind.ERROR;
   } catch (Exception e) {
     // type loading problem
     return false;
   }
 }
 @Override
 public synchronized void init(ProcessingEnvironment processingEnv) {
   super.init(processingEnv);
   elements = processingEnv.getElementUtils();
   types = processingEnv.getTypeUtils();
   messager = processingEnv.getMessager();
   factoryWriter = new FactoryWriter(processingEnv.getFiler());
   providedChecker = new ProvidedChecker(messager);
   declarationFactory = new AutoFactoryDeclaration.Factory(elements, messager);
   factoryDescriptorGenerator =
       new FactoryDescriptorGenerator(messager, elements, declarationFactory);
 }
Пример #10
0
  @Override
  public void init(ProcessingEnvironment processingEnv) {
    super.init(processingEnv);

    filer = processingEnv.getFiler();
    messager = processingEnv.getMessager();
    eltUtils = processingEnv.getElementUtils();

    outputDir = processingEnv.getOptions().get("structoutputdir");
    outputDir = outputDir == null ? "gensrc" : outputDir;

    basedir = processingEnv.getOptions().get("basedir");
  }
  @Test
  public void testBuilderPackage() {
    // Given:
    TypeElement pojoTypeElement = env.getElementUtils().getTypeElement(ADDRESS_DTO_CLASSNAME);

    // When:
    Output output = underTest.produce(new Input(pojoTypeElement));
    BuilderM builder = output.getBuilder();

    // Then:
    assertEquals("builder classname", "AddressDTOBuilder", builder.getType().getSimpleName());
    assertEquals("package", "testdata.builder", builder.getType().getPackage());
  }
  public Option<IRClass> find(AndroidManifest manifest) {
    Elements elementUtils = processingEnv.getElementUtils();
    String rClass = getRClassPackageName(manifest) + ".R";
    TypeElement rType = elementUtils.getTypeElement(rClass);

    if (rType == null) {
      LOGGER.error("The generated {} class cannot be found", rClass);
      return Option.absent();
    }

    LOGGER.info("Found project R class: {}", rType.toString());

    return Option.<IRClass>of(new RClass(rType));
  }
Пример #13
0
  @Test
  public void testProduceModelReturnsModelWithTypeParameters() {
    // Given:
    String pojoClassname = NumberGrid.class.getName();
    TypeElement pojoTypeElement = env.getElementUtils().getTypeElement(pojoClassname);

    // When:
    Output output = underTest.produce(new Input(pojoTypeElement));
    BuilderM builder = output.getBuilder();

    // Then:
    assertEquals(
        "type",
        "NumberGridBuilder<E extends Number>",
        builder.getType().getGenericTypeSimpleNameWithBounds());
  }
Пример #14
0
 private void checkForMutableFields(Protoclass protoclass, TypeElement element) {
   for (VariableElement field :
       ElementFilter.fieldsIn(
           processing.getElementUtils().getAllMembers(CachingElements.getDelegate(element)))) {
     if (!field.getModifiers().contains(Modifier.FINAL)) {
       Reporter report = protoclass.report();
       boolean ownField = CachingElements.equals(element, field.getEnclosingElement());
       if (ownField) {
         report
             .withElement(field)
             .warning("Avoid introduction of fields (except constants) in abstract value types");
       } else {
         report.warning("Abstract value type inherits mutable fields");
       }
     }
   }
 }
Пример #15
0
 public static EntityMirror newInstance(TypeElement clazz, ProcessingEnvironment env) {
   assertNotNull(env);
   AnnotationMirror annotationMirror = ElementUtil.getAnnotationMirror(clazz, Entity.class, env);
   if (annotationMirror == null) {
     return null;
   }
   EntityMirror result = new EntityMirror(annotationMirror);
   for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry :
       env.getElementUtils().getElementValuesWithDefaults(annotationMirror).entrySet()) {
     String name = entry.getKey().getSimpleName().toString();
     AnnotationValue value = entry.getValue();
     if ("listener".equals(name)) {
       result.listener = value;
     } else if ("naming".equals(name)) {
       result.naming = value;
     } else if ("immutable".equals(name)) {
       result.immutable = value;
     }
   }
   return result;
 }
Пример #16
0
 /**
  * Whether the specified element is assignable to the fqTn parameter
  *
  * @param processingEnvironment The environment this runs in
  * @param fqTn THe fully qualified type name of the element we want to check
  * @param element The element to check that implements
  * @return true if element implements the fqTn
  */
 public static boolean isSubclass(
     ProcessingEnvironment processingEnvironment, String fqTn, TypeElement element) {
   TypeElement typeElement = processingEnvironment.getElementUtils().getTypeElement(fqTn);
   if (typeElement == null) {
     processingEnvironment
         .getMessager()
         .printMessage(
             Diagnostic.Kind.ERROR,
             "Type Element was null for: "
                 + fqTn
                 + ""
                 + "ensure that the visibility of the class is not private.");
     return false;
   } else {
     TypeMirror classMirror = typeElement.asType();
     return classMirror != null
         && element != null
         && element.asType() != null
         && processingEnvironment.getTypeUtils().isSubtype(element.asType(), classMirror);
   }
 }
  public ValidateDescriptor(ModelDescriptor modelDescriptor, Element field) {
    super(modelDescriptor, field);
    this.validateIf = field.getAnnotation(ValidateIf.class);
    this.validateIfValue = field.getAnnotation(ValidateIfValue.class);

    ProcessingEnvironment processingEnv = modelDescriptor.getProcessingEnvironment();
    Types typeUtils = processingEnv.getTypeUtils();
    this.isMethodValidation = getField().getKind().equals(ElementKind.METHOD);

    if (this.isMethodValidation) {
      // Make sure our method validation returns a boolean value
      if (!((ExecutableElement) this.field).getReturnType().getKind().equals(TypeKind.BOOLEAN)) {
        modelDescriptor
            .getMessager()
            .printMessage(
                Diagnostic.Kind.ERROR,
                "Methods annotated with @Validate must return a boolean value!",
                field);
      }

      methodAnnotation = getField().getAnnotation(Validate.class);
      if (this.methodAnnotation == null) {
        modelDescriptor
            .getMessager()
            .printMessage(Diagnostic.Kind.ERROR, "Could not retrieve method validation annotation");
      }
    } else {
      this.isList =
          typeUtils.isAssignable(
              field.asType(),
              typeUtils.getDeclaredType(
                  processingEnv
                      .getElementUtils()
                      .getTypeElement(ListContainer.class.getCanonicalName()),
                  typeUtils.getWildcardType(null, null)));
    }
  }
Пример #18
0
 public Elements getElements() {
   return processingEnvironment.getElementUtils();
 }
Пример #19
0
 @Override
 public synchronized void init(ProcessingEnvironment processingEnv) {
   super.init(processingEnv);
   this.utils = new Utils(processingEnv.getElementUtils(), processingEnv.getTypeUtils());
 }
Пример #20
0
 @Override
 public void init(ProcessingEnvironment env) {
   filer = env.getFiler();
   messager = env.getMessager();
   elements = env.getElementUtils();
 }
Пример #21
0
 /**
  * Returns an implementation of some utility methods for operating on elements.
  *
  * @return element utilities
  */
 public final Elements getElementUtils() {
   return processingEnv.getElementUtils();
 }
Пример #22
0
  @Override
  public StringBuffer generate(
      final String packageName,
      final PackageElement packageElement,
      final String className,
      final Element element,
      final ProcessingEnvironment processingEnvironment)
      throws GenerationException {

    final Messager messager = processingEnvironment.getMessager();
    messager.printMessage(Kind.NOTE, "Starting code generation for [" + className + "]");

    final Elements elementUtils = processingEnvironment.getElementUtils();

    // Extract required information
    final TypeElement classElement = (TypeElement) element;
    final String annotationName = ClientAPIModule.getWorkbenchScreenClass();

    String identifier = null;
    Integer preferredHeight = null;
    Integer preferredWidth = null;

    for (final AnnotationMirror am : classElement.getAnnotationMirrors()) {
      if (annotationName.equals(am.getAnnotationType().toString())) {
        for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry :
            am.getElementValues().entrySet()) {
          AnnotationValue aval = entry.getValue();
          if ("identifier".equals(entry.getKey().getSimpleName().toString())) {
            identifier = aval.getValue().toString();
          } else if ("preferredHeight".equals(entry.getKey().getSimpleName().toString())) {
            final int _preferredHeight = (Integer) aval.getValue();
            if (_preferredHeight > 0) {
              preferredHeight = _preferredHeight;
            }
          } else if ("preferredWidth".equals(entry.getKey().getSimpleName().toString())) {
            final int _preferredWidth = (Integer) aval.getValue();
            if (_preferredWidth > 0) {
              preferredWidth = _preferredWidth;
            }
          }
        }
        break;
      }
    }

    final String owningPlace =
        GeneratorUtils.getOwningPerspectivePlaceRequest(classElement, processingEnvironment);

    final String beanActivatorClass =
        GeneratorUtils.getBeanActivatorClassName(classElement, processingEnvironment);

    final ExecutableElement onStartupMethod =
        GeneratorUtils.getOnStartupMethodForNonEditors(classElement, processingEnvironment);

    final String onStartup0ParameterMethodName;
    final String onStartup1ParameterMethodName;
    if (onStartupMethod == null) {
      onStartup0ParameterMethodName = null;
      onStartup1ParameterMethodName = null;
    } else if (onStartupMethod.getParameters().isEmpty()) {
      onStartup0ParameterMethodName = onStartupMethod.getSimpleName().toString();
      onStartup1ParameterMethodName = null;
    } else {
      onStartup0ParameterMethodName = null;
      onStartup1ParameterMethodName = onStartupMethod.getSimpleName().toString();
    }

    final String onMayCloseMethodName =
        GeneratorUtils.getOnMayCloseMethodName(classElement, processingEnvironment);
    final String onCloseMethodName =
        GeneratorUtils.getOnCloseMethodName(classElement, processingEnvironment);
    final String onShutdownMethodName =
        GeneratorUtils.getOnShutdownMethodName(classElement, processingEnvironment);
    final String onOpenMethodName =
        GeneratorUtils.getOnOpenMethodName(classElement, processingEnvironment);
    final String onLostFocusMethodName =
        GeneratorUtils.getOnLostFocusMethodName(classElement, processingEnvironment);
    final String onFocusMethodName =
        GeneratorUtils.getOnFocusMethodName(classElement, processingEnvironment);
    final String getDefaultPositionMethodName =
        GeneratorUtils.getDefaultPositionMethodName(classElement, processingEnvironment);
    final String getTitleMethodName =
        GeneratorUtils.getTitleMethodName(classElement, processingEnvironment);
    final String getContextIdMethodName =
        GeneratorUtils.getContextIdMethodName(classElement, processingEnvironment);
    final ExecutableElement getTitleWidgetMethod =
        GeneratorUtils.getTitleWidgetMethodName(classElement, processingEnvironment);
    final String getTitleWidgetMethodName =
        getTitleWidgetMethod == null ? null : getTitleWidgetMethod.getSimpleName().toString();
    final ExecutableElement getWidgetMethod =
        GeneratorUtils.getWidgetMethodName(classElement, processingEnvironment);
    final String getWidgetMethodName =
        getWidgetMethod == null ? null : getWidgetMethod.getSimpleName().toString();
    final boolean hasUberView =
        GeneratorUtils.hasUberViewReference(classElement, processingEnvironment, getWidgetMethod);

    final boolean isWidget = GeneratorUtils.getIsWidget(classElement, processingEnvironment);
    final String getMenuBarMethodName =
        GeneratorUtils.getMenuBarMethodName(classElement, processingEnvironment);
    final String getToolBarMethodName =
        GeneratorUtils.getToolBarMethodName(classElement, processingEnvironment);
    final String securityTraitList =
        GeneratorUtils.getSecurityTraitList(elementUtils, classElement);
    final String rolesList = GeneratorUtils.getRoleList(elementUtils, classElement);

    if (GeneratorUtils.debugLoggingEnabled()) {
      messager.printMessage(Kind.NOTE, "Package name: " + packageName);
      messager.printMessage(Kind.NOTE, "Class name: " + className);
      messager.printMessage(Kind.NOTE, "Identifier: " + identifier);
      messager.printMessage(Kind.NOTE, "Owning Perspective Identifier: " + owningPlace);
      messager.printMessage(Kind.NOTE, "Preferred Height: " + preferredHeight);
      messager.printMessage(Kind.NOTE, "Preferred Width: " + preferredWidth);
      messager.printMessage(Kind.NOTE, "getContextIdMethodName: " + getContextIdMethodName);
      messager.printMessage(
          Kind.NOTE, "onStartup0ParameterMethodName: " + onStartup0ParameterMethodName);
      messager.printMessage(
          Kind.NOTE, "onStartup1ParameterMethodName: " + onStartup1ParameterMethodName);
      messager.printMessage(Kind.NOTE, "onMayCloseMethodName: " + onMayCloseMethodName);
      messager.printMessage(Kind.NOTE, "onCloseMethodName: " + onCloseMethodName);
      messager.printMessage(Kind.NOTE, "onShutdownMethodName: " + onShutdownMethodName);
      messager.printMessage(Kind.NOTE, "onOpenMethodName: " + onOpenMethodName);
      messager.printMessage(Kind.NOTE, "onLostFocusMethodName: " + onLostFocusMethodName);
      messager.printMessage(Kind.NOTE, "onFocusMethodName: " + onFocusMethodName);
      messager.printMessage(
          Kind.NOTE, "getDefaultPositionMethodName: " + getDefaultPositionMethodName);
      messager.printMessage(Kind.NOTE, "getTitleMethodName: " + getTitleMethodName);
      messager.printMessage(Kind.NOTE, "getTitleWidgetMethodName: " + getTitleWidgetMethodName);
      messager.printMessage(Kind.NOTE, "getWidgetMethodName: " + getWidgetMethodName);
      messager.printMessage(Kind.NOTE, "isWidget: " + Boolean.toString(isWidget));
      messager.printMessage(Kind.NOTE, "hasUberView: " + Boolean.toString(hasUberView));
      messager.printMessage(Kind.NOTE, "getMenuBarMethodName: " + getMenuBarMethodName);
      messager.printMessage(Kind.NOTE, "getToolBarMethodName: " + getToolBarMethodName);
      messager.printMessage(Kind.NOTE, "securityTraitList: " + securityTraitList);
      messager.printMessage(Kind.NOTE, "rolesList: " + rolesList);
    }

    // Validate getWidgetMethodName and isWidget
    if (!isWidget && getWidgetMethodName == null) {
      throw new GenerationException(
          "The WorkbenchScreen must either extend IsWidget or provide a @WorkbenchPartView annotated method to return a com.google.gwt.user.client.ui.IsWidget.",
          packageName + "." + className);
    }
    if (isWidget && getWidgetMethodName != null) {
      final String msg =
          "The WorkbenchScreen both extends com.google.gwt.user.client.ui.IsWidget and provides a @WorkbenchPartView annotated method. The annotated method will take precedence.";
      messager.printMessage(Kind.WARNING, msg, classElement);
    }

    // Validate getTitleMethodName and getTitleWidgetMethodName
    if (getTitleMethodName == null) {
      throw new GenerationException(
          "The WorkbenchScreen must provide a @WorkbenchPartTitle annotated method to return a java.lang.String.",
          packageName + "." + className);
    }

    // Setup data for template sub-system
    Map<String, Object> root = new HashMap<String, Object>();
    root.put("packageName", packageName);
    root.put("className", className);
    root.put("identifier", identifier);
    root.put("owningPlace", owningPlace);
    root.put("preferredHeight", preferredHeight);
    root.put("preferredWidth", preferredWidth);
    root.put("getContextIdMethodName", getContextIdMethodName);
    root.put("realClassName", classElement.getSimpleName().toString());
    root.put("beanActivatorClass", beanActivatorClass);
    root.put("onStartup0ParameterMethodName", onStartup0ParameterMethodName);
    root.put("onStartup1ParameterMethodName", onStartup1ParameterMethodName);
    root.put("onMayCloseMethodName", onMayCloseMethodName);
    root.put("onCloseMethodName", onCloseMethodName);
    root.put("onShutdownMethodName", onShutdownMethodName);
    root.put("onOpenMethodName", onOpenMethodName);
    root.put("onLostFocusMethodName", onLostFocusMethodName);
    root.put("onFocusMethodName", onFocusMethodName);
    root.put("getDefaultPositionMethodName", getDefaultPositionMethodName);
    root.put("getTitleMethodName", getTitleMethodName);
    root.put("getTitleWidgetMethodName", getTitleWidgetMethodName);
    root.put("getWidgetMethodName", getWidgetMethodName);
    root.put("isWidget", isWidget);
    root.put("hasUberView", hasUberView);
    root.put("getMenuBarMethodName", getMenuBarMethodName);
    root.put("getToolBarMethodName", getToolBarMethodName);
    root.put("securityTraitList", securityTraitList);
    root.put("rolesList", rolesList);

    // Generate code
    final StringWriter sw = new StringWriter();
    final BufferedWriter bw = new BufferedWriter(sw);
    try {
      final Template template = config.getTemplate("activityScreen.ftl");
      template.process(root, bw);
    } catch (IOException ioe) {
      throw new GenerationException(ioe);
    } catch (TemplateException te) {
      throw new GenerationException(te);
    } finally {
      try {
        bw.close();
        sw.close();
      } catch (IOException ioe) {
        throw new GenerationException(ioe);
      }
    }
    messager.printMessage(Kind.NOTE, "Successfully generated code for [" + className + "]");

    return sw.getBuffer();
  }