Java Code Examples for javax.lang.model.element.VariableElement#getAnnotation()

The following examples show how to use javax.lang.model.element.VariableElement#getAnnotation() . 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: EntityMetaFactory.java    From doma with Apache License 2.0 6 votes vote down vote up
public void doFieldElements(TypeElement classElement, EntityMeta entityMeta) {
  for (VariableElement fieldElement : getFieldElements(classElement)) {
    try {
      if (fieldElement.getAnnotation(Transient.class) != null) {
        continue;
      } else if (fieldElement.getModifiers().contains(Modifier.STATIC)) {
        continue;
      } else if (fieldElement.getAnnotation(OriginalStates.class) != null) {
        doOriginalStatesField(classElement, fieldElement, entityMeta);
      } else {
        doEntityPropertyMeta(classElement, fieldElement, entityMeta);
      }
    } catch (AptException e) {
      ctx.getReporter().report(e);
      entityMeta.setError(true);
    }
  }
}
 
Example 2
Source File: KeyAnnotatedField.java    From simple-preferences with Apache License 2.0 6 votes vote down vote up
public KeyAnnotatedField(VariableElement element) throws ProcessingException {
  if (element.getModifiers().contains(Modifier.PRIVATE)) {
    throw new ProcessingException(element,
        "Field %s is private, must be accessible from inherited class", element.getSimpleName());
  }
  annotatedElement = element;

  type = TypeName.get(element.asType());

  Key annotation = element.getAnnotation(Key.class);
  name = element.getSimpleName().toString();
  preferenceKey = Strings.isNullOrEmpty(annotation.name()) ? Utils.lowerCamelToLowerSnake(name)
      : annotation.name();
  omitGetterPrefix = annotation.omitGetterPrefix();
  needCommitMethod = annotation.needCommitMethod();
}
 
Example 3
Source File: RetroFacebookProcessor.java    From RetroFacebook with Apache License 2.0 6 votes vote down vote up
public String buildBody(ExecutableElement method) {
  String body = "";

  // TODO duplicated routine
  retrofacebook.RetroFacebook.POST post = method.getAnnotation(retrofacebook.RetroFacebook.POST.class);
  if (post == null) return body;

  // TODO duplicated code
  List<? extends VariableElement> parameters = method.getParameters();
  for (VariableElement parameter : parameters) {
    if (parameter.getAnnotation(retrofacebook.RetroFacebook.Body.class) != null) {
      body = parameter.getSimpleName().toString();
    }
  }
  return body;
}
 
Example 4
Source File: InjectObjectField.java    From WandFix with MIT License 6 votes vote down vote up
public InjectObjectField(Element element) throws IllegalArgumentException {
    if (element.getKind() != ElementKind.FIELD) {
        throw new IllegalArgumentException(String.format("Only fields can be annotated with @%s",
                InjectObject.class.getSimpleName()));
    }
    mVariableElement = (VariableElement) element;

    InjectObject injectOvject = mVariableElement.getAnnotation(InjectObject.class);
    mClassName = injectOvject.className();
    mLevel = injectOvject.level();
    if (mClassName == null) {
        throw new IllegalArgumentException(
                String.format("value() in %s for field %s is not valid !", InjectObject.class.getSimpleName(),
                        mVariableElement.getSimpleName()));
    }
}
 
Example 5
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 6
Source File: ProcessUtils.java    From RxAndroidOrm with Apache License 2.0 5 votes vote down vote up
public static List<VariableElement> filterIgnore(List<VariableElement> elements) {
    List<VariableElement> filtered = new ArrayList<>();
    for (VariableElement variableElement : elements) {
        if (variableElement.getAnnotation(Ignore.class) == null && !Constants.PARCEL_CREATOR.equals(
            ProcessUtils.getObjectName(variableElement)) || ProcessUtils.isNotVariable(variableElement)) {
            filtered.add(variableElement);
        }
    }
    return filterStaticFinal(filtered);
}
 
Example 7
Source File: ColumnElement.java    From Ollie with Apache License 2.0 5 votes vote down vote up
public ColumnElement(Registry registry, TypeElement enclosingType, VariableElement element) {
	this.element = element;
	this.column = element.getAnnotation(Column.class);
	this.enclosingType = enclosingType;
	this.deserializedType = registry.getElements().getTypeElement(element.asType().toString());

	final TypeAdapterElement typeAdapterElement = registry.getTypeAdapterElement(deserializedType);
	final TypeElement modelElement = registry.getElements().getTypeElement("ollie.Model");
	final DeclaredType modelType = registry.getTypes().getDeclaredType(modelElement);
	isModel = registry.getTypes().isAssignable(element.asType(), modelType);

	if (isModel) {
		final Table table = deserializedType.getAnnotation(Table.class);
		serializedType = registry.getElements().getTypeElement(Long.class.getName());
		modelTableName = table.value();
	} else if (typeAdapterElement != null) {
		serializedType = typeAdapterElement.getSerializedType();
	} else {
		serializedType = deserializedType;
	}

	this.sqlType = SQL_TYPE_MAP.get(getSerializedQualifiedName());

	List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors();
	for (AnnotationMirror annotationMirror : annotationMirrors) {
		try {
			Class annotationClass = Class.forName(annotationMirror.getAnnotationType().toString());
			annotations.put(annotationClass, element.getAnnotation(annotationClass));
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}
 
Example 8
Source File: MoreElements.java    From doma with Apache License 2.0 5 votes vote down vote up
public String getParameterName(VariableElement variableElement) {
  assertNotNull(variableElement);
  ParameterName parameterName = variableElement.getAnnotation(ParameterName.class);
  if (parameterName != null && !parameterName.value().isEmpty()) {
    return parameterName.value();
  }
  return variableElement.getSimpleName().toString();
}
 
Example 9
Source File: ColumnInfo.java    From AirData with MIT License 5 votes vote down vote up
private String getColumnName(VariableElement element) {
    Column c = element.getAnnotation(Column.class);
    if (c != null) {
        String columnName = c.name();
        if (columnName != null && columnName.length() > 0) {
            return columnName;
        }
    }
    return element.getSimpleName().toString();
}
 
Example 10
Source File: FieldReferencePlugin.java    From squidb with Apache License 2.0 5 votes vote down vote up
@Override
public boolean processVariableElement(VariableElement field, DeclaredTypeName fieldType) {
    if (field.getAnnotation(Deprecated.class) != null) {
        return false;
    }
    if (field.getAnnotation(ColumnSpec.class) != null) {
        utils.getMessager().printMessage(Diagnostic.Kind.WARNING,
                "ColumnSpec is ignored outside of table models", field);
    }
    return super.processVariableElement(field, fieldType);
}
 
Example 11
Source File: RetroFacebookProcessor.java    From RetroFacebook with Apache License 2.0 5 votes vote down vote up
public List<String> buildQueryBundles(ExecutableElement method) {
  List<String> queryBundles = new ArrayList<String>();
  List<? extends VariableElement> parameters = method.getParameters();
  for (VariableElement parameter : parameters) {
    retrofacebook.RetroFacebook.QueryBundle queryBundle = parameter
        .getAnnotation(retrofacebook.RetroFacebook.QueryBundle.class);
    if (queryBundle == null) {
      continue;
    }

    queryBundles.add(parameter.getSimpleName().toString());
  }
  return queryBundles;
}
 
Example 12
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 13
Source File: RetroWeiboProcessor.java    From SimpleWeibo with Apache License 2.0 5 votes vote down vote up
public List<String> buildQueryBundles(ExecutableElement method) {
  List<String> queryBundles = new ArrayList<String>();
  List<? extends VariableElement> parameters = method.getParameters();
  for (VariableElement parameter : parameters) {
    retroweibo.RetroWeibo.QueryBundle queryBundle = parameter
        .getAnnotation(retroweibo.RetroWeibo.QueryBundle.class);
    if (queryBundle == null) {
      continue;
    }

    queryBundles.add(parameter.getSimpleName().toString());
  }
  return queryBundles;
}
 
Example 14
Source File: WebServiceVisitor.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
protected boolean isLegalParameter(VariableElement param,
                                   ExecutableElement method,
                                   TypeElement typeElement,
                                   int paramIndex) {
    if (!isLegalType(param.asType())) {
        builder.processError(WebserviceapMessages.WEBSERVICEAP_METHOD_PARAMETER_TYPES_CANNOT_IMPLEMENT_REMOTE(typeElement.getQualifiedName(),
                method.getSimpleName(),
                param.getSimpleName(),
                param.asType().toString()), param);
        return false;
    }
    TypeMirror holderType;
    holderType = builder.getHolderValueType(param.asType());
    WebParam webParam = param.getAnnotation(WebParam.class);
    WebParam.Mode mode = null;
    if (webParam != null)
        mode = webParam.mode();

    if (holderType != null) {
        if (mode != null && mode == WebParam.Mode.IN)
            builder.processError(WebserviceapMessages.WEBSERVICEAP_HOLDER_PARAMETERS_MUST_NOT_BE_IN_ONLY(typeElement.getQualifiedName(), method.toString(), paramIndex), param);
    } else if (mode != null && mode != WebParam.Mode.IN) {
        builder.processError(WebserviceapMessages.WEBSERVICEAP_NON_IN_PARAMETERS_MUST_BE_HOLDER(typeElement.getQualifiedName(), method.toString(), paramIndex), param);
    }

    return true;
}
 
Example 15
Source File: JSONFieldPlugin.java    From squidb with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean hasPropertyGeneratorForField(VariableElement field, DeclaredTypeName fieldType) {
    if (field.getAnnotation(JSONColumn.class) == null) {
        return false;
    }
    if (field.getModifiers().containsAll(TypeConstants.PUBLIC_STATIC_FINAL)) {
        // Might be a constant, ignore
        return false;
    }

    // Check that all type args are concrete types
    return recursivelyCheckTypes(field, fieldType, new AtomicBoolean(false));
}
 
Example 16
Source File: WebServiceVisitor.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
protected boolean isLegalParameter(VariableElement param,
                                   ExecutableElement method,
                                   TypeElement typeElement,
                                   int paramIndex) {
    if (!isLegalType(param.asType())) {
        builder.processError(WebserviceapMessages.WEBSERVICEAP_METHOD_PARAMETER_TYPES_CANNOT_IMPLEMENT_REMOTE(typeElement.getQualifiedName(),
                method.getSimpleName(),
                param.getSimpleName(),
                param.asType().toString()), param);
        return false;
    }
    TypeMirror holderType;
    holderType = builder.getHolderValueType(param.asType());
    WebParam webParam = param.getAnnotation(WebParam.class);
    WebParam.Mode mode = null;
    if (webParam != null)
        mode = webParam.mode();

    if (holderType != null) {
        if (mode != null && mode == WebParam.Mode.IN)
            builder.processError(WebserviceapMessages.WEBSERVICEAP_HOLDER_PARAMETERS_MUST_NOT_BE_IN_ONLY(typeElement.getQualifiedName(), method.toString(), paramIndex), param);
    } else if (mode != null && mode != WebParam.Mode.IN) {
        builder.processError(WebserviceapMessages.WEBSERVICEAP_NON_IN_PARAMETERS_MUST_BE_HOLDER(typeElement.getQualifiedName(), method.toString(), paramIndex), param);
    }

    return true;
}
 
Example 17
Source File: ColumnInfo.java    From AirData with MIT License 4 votes vote down vote up
public ColumnInfo(VariableElement columnElement) {

        typeMirror = columnElement.asType();

        LogUtils.debug("" + typeMirror);

        setType(DataType.getTypeString(typeMirror));

        String simpleName = columnElement.getSimpleName().toString();
        if (Utils.isPublic(columnElement)) {
            setGetter(simpleName);
            setSetter(simpleName + " = $L");
        } else {
            if (DataType.isBoolean(typeMirror)) {
                setGetter("is" + new String(new char[]{simpleName.charAt(0)}).toString().toUpperCase() + simpleName.substring(1) + "()");
            } else {
                setGetter("get" + new String(new char[]{simpleName.charAt(0)}).toString().toUpperCase() + simpleName.substring(1) + "()");
            }
            setSetter("set" + new String(new char[]{simpleName.charAt(0)}).toString().toUpperCase() + simpleName.substring(1) + "($L)");
        }

        setPrimaryKey(columnElement.getAnnotation(PrimaryKey.class) != null);

        setName(getColumnName(columnElement));

        // generate column definition
        StringBuilder sb = new StringBuilder();
        sb.append(name).append(" ").append(type);
        if (isPrimaryKey()) {
            sb.append(" PRIMARY KEY AUTOINCREMENT");
        }

        Column annotation = columnElement.getAnnotation(Column.class);
        if (annotation != null) {
            if (annotation.notNull()) {
                sb.append(" NOT NULL ON CONFLICT ");
                sb.append(annotation.onNullConflict().toString());
            }

            if (annotation.unique()) {
                sb.append(" UNIQUE ON CONFLICT ");
                sb.append(annotation.onUniqueConflict().toString());
            }
        }
        definition = sb.toString();
    }
 
Example 18
Source File: InlineAnnotationReaderImpl.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public boolean hasFieldAnnotation(Class<? extends Annotation> annotationType, VariableElement f) {
    return f.getAnnotation(annotationType)!=null;
}
 
Example 19
Source File: InlineAnnotationReaderImpl.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public boolean hasFieldAnnotation(Class<? extends Annotation> annotationType, VariableElement f) {
    return f.getAnnotation(annotationType)!=null;
}
 
Example 20
Source File: ColumnReader.java    From droitatedDB with Apache License 2.0 4 votes vote down vote up
private boolean isAutoIncrementAnnotated(final VariableElement variableElement) {
    return variableElement.getAnnotation(AutoIncrement.class) != null;
}