Java Code Examples for javax.lang.model.element.TypeElement#toString()

The following examples show how to use javax.lang.model.element.TypeElement#toString() . 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: ObjectRouterGenerator.java    From componentrouter with Apache License 2.0 6 votes vote down vote up
@Override
public TypeSpec onCreateTypeSpec(TypeElement element, String packageName, String className) throws EmptyPathException {
    ObjectRoute objectRoute = element.getAnnotation(ObjectRoute.class);
    String path = objectRoute.value();
    if (path.isEmpty()) {
        throw new EmptyPathException(element.toString());
    }
    ClassName routeType = ClassName.get(packageName, className);
    TypeSpec.Builder builder = TypeSpec.classBuilder(className + INSTANCE_PROXY_SUFFIX)
            .addSuperinterface(ComponentRouterInstance.class)
            .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
            .addField(ComponentRouterProxy.class, INSTANCE_COMPONENT_ROUTER_PROXY_FIELD_NAME, Modifier.PRIVATE)
            .addField(routeType, INSTANCE_FIELD_NAME, Modifier.PRIVATE);
    buildConstructor(builder, element, routeType);
    generateInterfaceMethod(builder);
    return builder.build();
}
 
Example 2
Source File: ErrorPopUpControllerAnnotationScanner.java    From nalu with Apache License 2.0 6 votes vote down vote up
private ErrorPopUpControllerModel handlePopUpController()
    throws ProcessorException {
  // get Annotation ...
  ErrorPopUpController annotation = errorPopUpControllerElement.getAnnotation(ErrorPopUpController.class);
  // handle ...
  TypeElement componentTypeElement = this.getComponentTypeElement(annotation);
  if (componentTypeElement == null) {
    throw new ProcessorException("Nalu-Processor: @ErrorPopUpController - componentTypeElement is null");
  }
  TypeElement componentInterfaceTypeElement = this.getComponentInterfaceTypeElement(annotation);
  // check, if the controller implements IsComponentController
  boolean isComponentCreator = this.checkIsComponentCreator(errorPopUpControllerElement,
                                                            componentInterfaceTypeElement);
  // get context!
  String context = this.getContextType(errorPopUpControllerElement);
  if (Objects.isNull(context)) {
    throw new ProcessorException("Nalu-Processor: controller >>" + errorPopUpControllerElement.toString() + "<< does not have a context generic!");
  }
  // save model ...
  return new ErrorPopUpControllerModel(new ClassNameModel(context),
                                       new ClassNameModel(errorPopUpControllerElement.toString()),
                                       new ClassNameModel(componentInterfaceTypeElement.toString()),
                                       new ClassNameModel(componentTypeElement.toString()),
                                       isComponentCreator);
}
 
Example 3
Source File: ClassEntity.java    From RxPay with Apache License 2.0 6 votes vote down vote up
/**
 * @param elementUtils
 * @param typeUtils
 * @param element      current anntated class
 */
public ClassEntity(Elements elementUtils, Types typeUtils, TypeElement element) {
    elementWeakCache = new WeakReference<TypeElement>(element);
    this.classPackageName = elementUtils.getPackageOf(element).getQualifiedName().toString();
    this.modifierSet = element.getModifiers();
    this.className = element.toString();
    annotationMirrors = element.getAnnotationMirrors();
    this.classSimpleName = element.getSimpleName();
    this.classQualifiedName = element.getQualifiedName();
    if ("java.lang.Object".equals(element.getSuperclass().toString())){
        this.superclass = null;
    }else{
        this.superclass = element.getSuperclass().toString();
    }
    List<? extends TypeMirror> interfaces = element.getInterfaces();

    for (TypeMirror anInterface : interfaces){
        this.interfaces.add(typeUtils.asElement(anInterface).toString());
    }
}
 
Example 4
Source File: Validator.java    From AutoBundle with Apache License 2.0 6 votes vote down vote up
static void checkConverterClass(TypeElement element) {
    if (!element.getModifiers().contains(Modifier.PUBLIC)) {
        throw new ProcessingException(element.toString() + " must be public.");
    }

    boolean existEmptyConstructor = false;
    for (Element e : element.getEnclosedElements()) {
        if (e.getKind() == ElementKind.CONSTRUCTOR) {
            ExecutableElement executableElement = (ExecutableElement) e;
            if ((executableElement.getParameters() == null ||
                    executableElement.getParameters().isEmpty()) &&
                    executableElement.getModifiers().contains(Modifier.PUBLIC)) {
                existEmptyConstructor = true;
                break;
            }
        }
    }
    if (!existEmptyConstructor) {
        throw new ProcessingException(element + " must have public empty constructor.");
    }
}
 
Example 5
Source File: PopUpControllerAnnotationScanner.java    From nalu with Apache License 2.0 5 votes vote down vote up
private PopUpControllerModel handlePopUpController()
    throws ProcessorException {
  // get Annotation ...
  PopUpController annotation = popUpControllerElement.getAnnotation(PopUpController.class);
  // handle ...
  TypeElement componentTypeElement = this.getComponentTypeElement(annotation);
  if (componentTypeElement == null) {
    throw new ProcessorException("Nalu-Processor: @PopUpController - componentTypeElement is null");
  }
  TypeElement componentInterfaceTypeElement = this.getComponentInterfaceTypeElement(annotation);
  // check, if the controller implements IsComponentController
  boolean componentController = this.checkIsComponentCreator(popUpControllerElement,
                                                             componentInterfaceTypeElement);
  // get context!
  String context = this.getContextType(popUpControllerElement);
  if (Objects.isNull(context)) {
    throw new ProcessorException("Nalu-Processor: controller >>" + popUpControllerElement.toString() + "<< does not have a generic context!");
  }
  // save model ...
  return new PopUpControllerModel(annotation.name(),
                                  new ClassNameModel(context),
                                  new ClassNameModel(popUpControllerElement.toString()),
                                  new ClassNameModel(componentInterfaceTypeElement.toString()),
                                  new ClassNameModel(componentTypeElement.toString()),
                                  new ClassNameModel(popUpControllerElement.toString()),
                                  componentController);
}
 
Example 6
Source File: BlockControllerAnnotationScanner.java    From nalu with Apache License 2.0 5 votes vote down vote up
private BlockControllerModel handleBlockController()
    throws ProcessorException {
  // get Annotation ...
  BlockController annotation = blockControllerElement.getAnnotation(BlockController.class);
  // handle ...
  TypeElement componentTypeElement = this.getComponentTypeElement(annotation);
  if (componentTypeElement == null) {
    throw new ProcessorException("Nalu-Processor: @BlockController - componentTypeElement is null");
  }
  TypeElement componentInterfaceTypeElement = this.getComponentInterfaceTypeElement(annotation);
  // check, if the controller implements IsComponentController
  boolean componentController = this.checkIsComponentCreator(blockControllerElement,
                                                             componentInterfaceTypeElement);
  // get context!
  String context = this.getContextType(blockControllerElement);
  if (Objects.isNull(context)) {
    throw new ProcessorException("Nalu-Processor: controller >>" + blockControllerElement.toString() + "<< does not have a generic context!");
  }
  // save model ...
  return new BlockControllerModel(annotation.name(),
                                  new ClassNameModel(context),
                                  new ClassNameModel(blockControllerElement.toString()),
                                  new ClassNameModel(componentInterfaceTypeElement.toString()),
                                  new ClassNameModel(componentTypeElement.toString()),
                                  new ClassNameModel(blockControllerElement.toString()),
                                  componentController,
                                  new ClassNameModel(Objects.requireNonNull(getConditionElement(annotation))
                                                            .toString()));
}
 
Example 7
Source File: APIIsSelfContainedTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean isAPIClass(TypeElement clazz) {
    String nameS = /*!!!!*/clazz.toString();
    
    for (String s : API_PACKAGES) {
        if (nameS.startsWith(s))
            return true;
    }
    
    return false;
}
 
Example 8
Source File: TurbineElements.java    From turbine with Apache License 2.0 5 votes vote down vote up
@Override
public Name getBinaryName(TypeElement element) {
  if (!(element instanceof TurbineTypeElement)) {
    throw new IllegalArgumentException(element.toString());
  }
  return getName(((TurbineTypeElement) element).sym().binaryName().replace('/', '.'));
}
 
Example 9
Source File: HTAnnotatedClass.java    From ht-universalrouter-android with MIT License 5 votes vote down vote up
public HTAnnotatedClass(TypeElement typeElement, String[] url, int entryAnim, int exitAnim) {
    this.typeElement = typeElement;
    this.activity = typeElement.toString();
    this.url = url;
    this.entryAnim = entryAnim;
    this.exitAnim = exitAnim;
}
 
Example 10
Source File: ControllerAnnotationScanner.java    From nalu with Apache License 2.0 4 votes vote down vote up
private ControllerModel handleController()
    throws ProcessorException {
  // get Annotation ...
  Controller annotation = controllerElement.getAnnotation(Controller.class);
  // handle ...
  TypeElement componentTypeElement = this.getComponentTypeElement(annotation);
  if (componentTypeElement == null) {
    throw new ProcessorException("Nalu-Processor: componentTypeElement is null");
  }
  TypeElement componentInterfaceTypeElement = this.getComponentInterfaceTypeElement(annotation);
  TypeMirror componentTypeTypeMirror = this.getComponentType(controllerElement.asType());
  // check and save the component type ...
  if (metaModel.getComponentType() == null) {
    metaModel.setComponentType(new ClassNameModel(componentTypeTypeMirror.toString()));
  } else {
    ClassNameModel compareValue = new ClassNameModel(componentTypeTypeMirror.toString());
    if (!metaModel.getComponentType()
                  .equals(compareValue)) {
      throw new ProcessorException("Nalu-Processor: componentType >>" + compareValue.getClassName() + "<< is different. All controllers must implement the componentType!");
    }
  }
  // check, if the controller implements IsComponentController
  boolean componentController = this.checkIsComponentCreator(controllerElement,
                                                             componentInterfaceTypeElement);
  // get context!
  String context = this.getContextType(controllerElement);
  if (Objects.isNull(context)) {
    throw new ProcessorException("Nalu-Processor: controller >>" + controllerElement.toString() + "<< does not have a generic context!");
  }
  // save model ...
  return new ControllerModel(annotation.route(),
                             getRoute(annotation.route()),
                             annotation.selector(),
                             getParametersFromRoute(annotation.route()),
                             new ClassNameModel(context),
                             new ClassNameModel(controllerElement.toString()),
                             new ClassNameModel(componentInterfaceTypeElement.toString()),
                             new ClassNameModel(componentTypeElement.toString()),
                             new ClassNameModel(componentTypeTypeMirror.toString()),
                             new ClassNameModel(controllerElement.toString()),
                             componentController);
}
 
Example 11
Source File: CompositeControllerAnnotationScanner.java    From nalu with Apache License 2.0 4 votes vote down vote up
private CompositeModel handleComposite(Element element)
    throws ProcessorException {
  // get Annotation ...
  CompositeController annotation = element.getAnnotation(CompositeController.class);
  // handle ...
  TypeElement componentTypeElement = this.getComponentTypeElement(annotation);
  if (componentTypeElement == null) {
    throw new ProcessorException("Nalu-Processor: componentTypeElement is null");
  }
  TypeElement componentInterfaceTypeElement = this.getComponentInterfaceTypeElement(annotation);
  TypeMirror componentTypeTypeMirror = this.getComponentType(element.asType());
  if (Objects.isNull(componentTypeTypeMirror)) {
    throw new ProcessorException("Nalu-Processor: componentTypeTypeMirror is null");
  }
  // check and save the component type ...
  if (metaModel.getComponentType() == null) {
    metaModel.setComponentType(new ClassNameModel(componentTypeTypeMirror.toString()));
  } else {
    ClassNameModel compareValue = new ClassNameModel(componentTypeTypeMirror.toString());
    if (!metaModel.getComponentType()
                  .equals(compareValue)) {
      throw new ProcessorException("Nalu-Processor: componentType >>" + compareValue.getClassName() + "<< is different. All composite controllers must implement the componentType!");
    }
  }
  // check, if the controller implements IsComponentController
  boolean componentController = this.checkIsComponentCreator(element,
                                                             componentInterfaceTypeElement);
  // get context!
  String context = this.getContextType(element);
  if (Objects.isNull(context)) {
    throw new ProcessorException("Nalu-Processor: composite controller >>" + element.toString() + "<< does not have a context generic!");
  }
  // create model ...
  if (Objects.isNull(componentInterfaceTypeElement)) {
    throw new ProcessorException("Nalu-Processor: componentInterfaceTypeElement is null!");
  }
  return new CompositeModel(new ClassNameModel(context),
                            new ClassNameModel(element.toString()),
                            new ClassNameModel(componentInterfaceTypeElement.toString()),
                            new ClassNameModel(componentTypeElement.toString()),
                            componentController);
}
 
Example 12
Source File: InitializerSpec.java    From spring-init with Apache License 2.0 4 votes vote down vote up
private boolean createBeanMethod(MethodSpec.Builder builder, ExecutableElement beanMethod, TypeElement type,
		boolean conditionsAvailable) {
	// TODO will need to handle bean methods in private configs
	try {
		TypeMirror returnType = utils.getReturnType(beanMethod);

		Element returnTypeElement = utils.asElement(returnType);
		boolean conditional = utils.hasAnnotation(beanMethod, SpringClassNames.CONDITIONAL.toString());
		if (conditional) {
			if (!conditionsAvailable) {
				builder.addStatement("$T conditions = context.getBeanFactory().getBean($T.class)",
						SpringClassNames.CONDITION_SERVICE, SpringClassNames.CONDITION_SERVICE);
			}
		}

		if (returnTypeElement.getModifiers().contains(Modifier.PRIVATE)) {

			if (conditional) {
				builder.beginControlFlow(
						"if (conditions.matches($T.class, $T.resolveClassName(\"$L\", context.getClassLoader())))",
						type, SpringClassNames.CLASS_UTILS, utils.erasure(returnType));
			}
			utils.printMessage(Kind.WARNING, "Generating source for bean method, type involved is private: "
					+ beanMethod.getEnclosingElement() + "." + beanMethod);
			builder.addStatement("context.registerBean($T.resolveClassName(\"$L\", context.getClassLoader()))",
					SpringClassNames.CLASS_UTILS, ((TypeElement) returnTypeElement).getQualifiedName());

		}
		else {

			if (conditional) {
				builder.beginControlFlow("if (conditions.matches($T.class, $T.class))", type,
						utils.erasure(returnType));
			}
			Parameters params = autowireParamsForMethod(beanMethod);

			builder.addStatement("context.registerBean(" + "\"" + beanMethod.getSimpleName() + "\", $T.class, "
					+ supplier(type, beanMethod, params.format) + customizer(type, beanMethod, params) + ")",
					ArrayUtils.merge(utils.erasure(returnType), type, params.args));
		}

		if (conditional) {
			builder.endControlFlow();
		}

		return conditional;
	}
	catch (Throwable t) {
		throw new RuntimeException(
				"Problem performing createBeanMethod for method " + type.toString() + "." + beanMethod.toString(),
				t);
	}
}
 
Example 13
Source File: ElementTo.java    From sundrio with Apache License 2.0 4 votes vote down vote up
public Boolean apply(TypeElement item) {
    String fqn = item.toString();
    return fqn.startsWith(JAVA_PEFIX) || fqn.startsWith(JAVAX_PEFIX) || fqn.startsWith(COM_SUN_PREFIX);
}
 
Example 14
Source File: AutoAnnotationProcessor.java    From auto with Apache License 2.0 4 votes vote down vote up
private void processMethod(ExecutableElement method) {
  if (!method.getModifiers().contains(Modifier.STATIC)) {
    throw abortWithError(method, "@AutoAnnotation method must be static");
  }

  TypeElement annotationElement = getAnnotationReturnType(method);

  Set<Class<?>> wrapperTypesUsedInCollections = wrapperTypesUsedInCollections(method);

  ImmutableMap<String, ExecutableElement> memberMethods = getMemberMethods(annotationElement);
  TypeElement methodClass = (TypeElement) method.getEnclosingElement();
  String pkg = TypeSimplifier.packageNameOf(methodClass);

  ImmutableMap<String, AnnotationValue> defaultValues = getDefaultValues(annotationElement);
  ImmutableMap<String, Member> members = getMembers(method, memberMethods);
  ImmutableMap<String, Parameter> parameters = getParameters(annotationElement, method, members);
  validateParameters(annotationElement, method, members, parameters, defaultValues);

  String generatedClassName = generatedClassName(method);

  AutoAnnotationTemplateVars vars = new AutoAnnotationTemplateVars();
  vars.annotationFullName = annotationElement.toString();
  vars.annotationName = TypeEncoder.encode(annotationElement.asType());
  vars.className = generatedClassName;
  vars.generated = getGeneratedTypeName();
  vars.members = members;
  vars.params = parameters;
  vars.pkg = pkg;
  vars.wrapperTypesUsedInCollections = wrapperTypesUsedInCollections;
  vars.gwtCompatible = isGwtCompatible(annotationElement);
  ImmutableMap<String, Integer> invariableHashes = invariableHashes(members, parameters.keySet());
  vars.invariableHashSum = 0;
  for (int h : invariableHashes.values()) {
    vars.invariableHashSum += h;
  }
  vars.invariableHashes = invariableHashes.keySet();
  String text = vars.toText();
  text = TypeEncoder.decode(text, processingEnv, pkg, annotationElement.asType());
  text = Reformatter.fixup(text);
  String fullName = fullyQualifiedName(pkg, generatedClassName);
  writeSourceFile(fullName, text, methodClass);
}