Java Code Examples for javax.lang.model.type.MirroredTypeException#getTypeMirror()

The following examples show how to use javax.lang.model.type.MirroredTypeException#getTypeMirror() . 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: AutoParcelProcessor.java    From auto-parcel with Apache License 2.0 6 votes vote down vote up
Property(String fieldName, VariableElement element) {
    this.fieldName = fieldName;
    this.element = element;
    this.typeName = TypeName.get(element.asType());
    this.annotations = getAnnotations(element);

    // get the parcel adapter if any
    ParcelAdapter parcelAdapter = element.getAnnotation(ParcelAdapter.class);
    if (parcelAdapter != null) {
        try {
            parcelAdapter.value();
        } catch (MirroredTypeException e) {
            this.typeAdapter = e.getTypeMirror();
        }
    }

    // get the element version, default 0
    ParcelVersion parcelVersion = element.getAnnotation(ParcelVersion.class);
    this.version = parcelVersion == null ? 0 : parcelVersion.from();
}
 
Example 2
Source File: FactoryAnnotatedCls.java    From ShapeView with Apache License 2.0 6 votes vote down vote up
public FactoryAnnotatedCls(TypeElement classElement) throws ProcessingException {
    this.mAnnotatedClsElement = classElement;
    ShapeType annotation = classElement.getAnnotation(ShapeType.class);
    type = annotation.value();
    try {

        mSupperClsSimpleName = annotation.superClass().getSimpleName();
        mSupperClsQualifiedName = annotation.superClass().getCanonicalName();
    } catch (MirroredTypeException mte) {
        DeclaredType classTypeMirror = (DeclaredType) mte.getTypeMirror();
        TypeElement classTypeElement = (TypeElement) classTypeMirror.asElement();
        mSupperClsQualifiedName = classTypeElement.getQualifiedName().toString();
        mSupperClsSimpleName = classTypeElement.getSimpleName().toString();
    }

    if (mSupperClsSimpleName == null || mSupperClsSimpleName.equals("")) {
        throw new ProcessingException(classElement,
                "superClass() in @%s for class %s is null or empty! that's not allowed",
                ShapeType.class.getSimpleName(), classElement.getQualifiedName().toString());
    }
}
 
Example 3
Source File: InjectViewStateProcessor.java    From Moxy with MIT License 6 votes vote down vote up
private String getViewStateClassFromAnnotationParams(TypeElement typeElement) {
	InjectViewState annotation = typeElement.getAnnotation(InjectViewState.class);
	String mvpViewStateClassName = "";

	if (annotation != null) {
		TypeMirror value;
		try {
			annotation.value();
		} catch (MirroredTypeException mte) {
			value = mte.getTypeMirror();
			mvpViewStateClassName = value.toString();
		}
	}

	if (mvpViewStateClassName.isEmpty() || DefaultViewState.class.getName().equals(mvpViewStateClassName)) {
		return null;
	}

	return mvpViewStateClassName;
}
 
Example 4
Source File: InjectViewStateProcessor.java    From Moxy with MIT License 6 votes vote down vote up
private String getViewClassFromAnnotationParams(TypeElement typeElement) {
	InjectViewState annotation = typeElement.getAnnotation(InjectViewState.class);
	String mvpViewClassName = "";

	if (annotation != null) {
		TypeMirror value = null;
		try {
			annotation.view();
		} catch (MirroredTypeException mte) {
			value = mte.getTypeMirror();
		}

		mvpViewClassName = Util.getFullClassName(value);
	}

	if (mvpViewClassName.isEmpty() || DefaultView.class.getName().equals(mvpViewClassName)) {
		return null;
	}

	return mvpViewClassName;
}
 
Example 5
Source File: ArgumentAnnotatedField.java    From fragmentargs with Apache License 2.0 6 votes vote down vote up
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);
  }
}
 
Example 6
Source File: ReduceAction.java    From reductor with Apache License 2.0 5 votes vote down vote up
private static TypeMirror getCreator(AutoReducer.Action action, Elements elements, Env env, ExecutableElement element) {
    TypeMirror typeMirror;
    try {
        Class<?> fromClass = action.from();
        String className = fromClass.getCanonicalName();
        typeMirror = elements.getTypeElement(className).asType();
    } catch (MirroredTypeException mte) {
        typeMirror = mte.getTypeMirror();
    }

    //Void is used by default meaning it's not linked to any action creator
    return env.getTypes().isSameType(typeMirror, env.asType(Void.class))
            ? null
            : typeMirror;
}
 
Example 7
Source File: ParcelableField.java    From ParcelablePlease with Apache License 2.0 5 votes vote down vote up
public ParcelableField(VariableElement element, Elements elementUtils, Types typeUtils) {

    this.element = element;
    fieldName = element.getSimpleName().toString();
    type = element.asType().toString();

    // Check for Bagger
    Bagger baggerAnnotation = element.getAnnotation(Bagger.class);
    if (baggerAnnotation != null) {
      // has a bagger annotation
      try {
        Class<? extends ParcelBagger> clazz = baggerAnnotation.value();
        baggerFullyQualifiedName = getFullQualifiedNameByClass(clazz);
      } catch (MirroredTypeException mte) {
        TypeMirror baggerClass = mte.getTypeMirror();
        baggerFullyQualifiedName = getFullQualifiedNameByTypeMirror(baggerClass);
      }

      // Everything is fine, so use the bagger
      codeGenerator = new BaggerCodeGen();
    } else {
      // Not using Bagger
      CodeGenInfo res = SupportedTypes.getCodeGenInfo(element, elementUtils, typeUtils);
      codeGenerator = res.getCodeGenerator();
      genericsTypeArgument = res.getGenericsType();

      // Check if type is supported
      if (codeGenerator == null) {
        ProcessorMessage.error(element,
            "Unsupported type %s for field %s. You could use @%s to provide your own serialization mechanism",
            element.asType().toString(), element.getSimpleName(), Bagger.class.getSimpleName());
      }
    }
  }
 
Example 8
Source File: ScoopsProcesssor.java    From Scoops with Apache License 2.0 5 votes vote down vote up
private TypeMirror getAdapterTypeMirror(BindTopping annotation){
    TypeMirror value = null;
    try {
        annotation.adapter();
    }catch (MirroredTypeException e){
        value = e.getTypeMirror();
    }
    return value;
}
 
Example 9
Source File: JServiceCodeGenerator.java    From jackdaw with Apache License 2.0 5 votes vote down vote up
private String getProviderClass(final JService annotation) {
    try {
        final Class<?> providerClass = annotation.value();
        return providerClass.getCanonicalName();
    } catch (final MirroredTypeException ex) {
        final TypeMirror typeMirror = ex.getTypeMirror();
        return typeMirror.toString();
    }
}
 
Example 10
Source File: StarlarkMethodProcessor.java    From bazel with Apache License 2.0 5 votes vote down vote up
private static TypeMirror getParamType(Param param) {
  // See explanation of this hack at Element.getAnnotation
  // and at https://stackoverflow.com/a/10167558.
  try {
    param.type();
    throw new IllegalStateException("unreachable");
  } catch (MirroredTypeException ex) {
    return ex.getTypeMirror();
  }
}
 
Example 11
Source File: ScoopsProcesssor.java    From Scoops with Apache License 2.0 5 votes vote down vote up
private TypeMirror getInterpolatorTypeMirror(BindToppingStatus annotation){
    TypeMirror value = null;
    try{
        annotation.interpolator();
    }catch (MirroredTypeException e){
        value = e.getTypeMirror();
    }

    return value;
}
 
Example 12
Source File: AptDeserializerBuilder.java    From domino-jackson with Apache License 2.0 5 votes vote down vote up
private String getBuilderName() {
    JsonDeserialize jsonDeserialize = typeUtils.asElement(beanType).getAnnotation(JsonDeserialize.class);
    if (isNull(jsonDeserialize)) {
        return null;
    }
    String builderName;
    try {
        builderName = jsonDeserialize.builder().getName();
    } catch (MirroredTypeException e) {
        TypeMirror typeMirror = e.getTypeMirror();
        builderName = typeMirror.toString();
    }
    return builderName;
}
 
Example 13
Source File: DaggerAutoInjectProcessor.java    From DaggerAutoInject with Apache License 2.0 5 votes vote down vote up
private static TypeMirror getComponent(InjectApplication annotation) {
    try {
        annotation.component(); // this should throw
    } catch (MirroredTypeException mte) {
        return mte.getTypeMirror();
    }
    return null; // can this ever happen ??
}
 
Example 14
Source File: ProcessorUtils.java    From Akatsuki with Apache License 2.0 5 votes vote down vote up
public DeclaredType getClassFromAnnotationMethod(Supplier<Class<?>> supplier) {
	// JDK suggested way of getting type mirrors, do not waste time here,
	// just move on
	try {
		return (DeclaredType) of(supplier.get());
	} catch (MirroredTypeException e) {
		// types WILL be declared
		return (DeclaredType) e.getTypeMirror();
	}
}
 
Example 15
Source File: VelocityTransformationProcessor.java    From sundrio with Apache License 2.0 5 votes vote down vote up
private TypeMirror annotationMirror(AnnotationSelector selector){
    try {
        selector.value();
        return null;
    } catch (MirroredTypeException m) {
        return m.getTypeMirror();
    }
}
 
Example 16
Source File: ConfigFieldParser.java    From aircon with MIT License 5 votes vote down vote up
private TypeMirror getTypeMirror(final Element element) {
	final Source annotation = element.getAnnotation(Source.class);
	if (annotation != null) {
		try {
			annotation.value();
		} catch (MirroredTypeException e) {
			return e.getTypeMirror();
		}
	}

	return null;
}
 
Example 17
Source File: CustomConfigAnnotationParser.java    From aircon with MIT License 5 votes vote down vote up
private TypeMirror getConfigTypeResolverTypeMirror(final ConfigType configTypeAnnotation) {
	try {
		configTypeAnnotation.value();
	} catch (MirroredTypeException e) {
		return e.getTypeMirror();
	}
	// Should never happen
	return null;
}
 
Example 18
Source File: HighLiteProcessor.java    From HighLite with Apache License 2.0 4 votes vote down vote up
private AbstractMap.SimpleEntry<Map<Element, SQLiteTable>,
        Boolean> getTableElementMappingForDatabase(
        final RoundEnvironment roundEnvironment,
        final Element databaseElement) {
    final AbstractMap.SimpleEntry<Map<Element, SQLiteTable>, Boolean> ret =
            new AbstractMap.SimpleEntry<Map<Element, SQLiteTable>, Boolean>(
                    new LinkedHashMap<Element, SQLiteTable>(), false);

    final List<String> tableNamesAdded = new ArrayList<>();
    for (final Element element : roundEnvironment.getElementsAnnotatedWith(SQLiteTable.class)) {
        if (element.getModifiers().contains(Modifier.ABSTRACT)
                || element.getKind().equals(ElementKind.INTERFACE)) continue;

        final SQLiteTable tableAnno = element.getAnnotation(SQLiteTable.class);

        TypeMirror mirror = null;
        try {
            tableAnno.database();
        } catch (MirroredTypeException e) {
            mirror = e.getTypeMirror();
        }

        final SQLiteDatabaseDescriptor dbAnno = mTypeUtils.asElement(mirror)
                .getAnnotation(SQLiteDatabaseDescriptor.class);
        if (dbAnno == null) {
            error(element, String.format("The database class must be annotated with %s",
                    SQLiteDatabaseDescriptor.class.getCanonicalName()));
            ret.setValue(true);
            return ret;
        }

        final String tableName = JavaWritableClass.getTableName(element);

        if (mTypeUtils.isSameType(mirror, databaseElement.asType())) {
            if (tableNamesAdded.contains(tableName)) {
                error(element, String.format("The table %s was already defined for database %s",
                        tableName, dbAnno.dbName()));
                ret.setValue(true);
                return ret;
            }

            tableNamesAdded.add(tableName);
            ret.getKey().put(element, tableAnno);
        }
    }

    return ret;
}
 
Example 19
Source File: AutoBundleBindingField.java    From AutoBundle with Apache License 2.0 4 votes vote down vote up
AutoBundleBindingField(VariableElement element,
                       AutoBundleField annotation,
                       Elements elementUtils,
                       Types typeUtils,
                       String getterName,
                       String setterName) {
    this.fieldName = element.toString();
    this.argKey = annotation.key().length() > 0 ? annotation.key() : this.fieldName;
    // @Nullable makes `required` to false
    this.required = annotation.required() && !BindingFieldHelper.hasNullableAnnotation(element);
    this.argType = TypeName.get(element.asType());
    this.getterName = getterName;
    this.setterName = setterName;
    this.annotations = BindingFieldHelper.getAnnotationsForField(element);
    Validator.checkAutoBundleFieldModifier(element, hasGetter() && hasSetter());

    TypeName converter;
    TypeName converted;
    try {
        Class clazz = annotation.converter();
        Validator.checkConverterClass(clazz);
        converter = TypeName.get(clazz);
        converted = detectConvertedTypeNameByClass(clazz);
    } catch (MirroredTypeException mte) {
        DeclaredType classTypeMirror = (DeclaredType) mte.getTypeMirror();
        TypeElement classTypeElement = (TypeElement) classTypeMirror.asElement();
        Validator.checkConverterClass(classTypeElement);
        converter = TypeName.get(classTypeMirror);
        converted = detectConvertedTypeByTypeElement(classTypeElement);
    }
    this.converter = converter;
    this.hasCustomConverter =
            !this.converter.equals(ClassName.get("com.yatatsu.autobundle", "DefaultAutoBundleConverter"));
    if (hasCustomConverter) {
        operationName = BindingFieldHelper.getOperationName(converted, elementUtils, typeUtils);
    } else {
        operationName = BindingFieldHelper.getOperationName(argType, elementUtils, typeUtils);
    }

    if (hasCustomConverter) {
        Validator.checkNotSupportedOperation(operationName, converted);
    } else {
        Validator.checkNotSupportedOperation(operationName, argType);
    }
}
 
Example 20
Source File: EasyMVPProcessor.java    From EasyMVP with Apache License 2.0 4 votes vote down vote up
private TypeElement getTypeElement(MirroredTypeException mte) {
    DeclaredType declaredType = (DeclaredType) mte.getTypeMirror();
    return (TypeElement) declaredType.asElement();
}