本文整理了Java中javax.lang.model.util.Types.asElement()
方法的一些代码示例,展示了Types.asElement()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Types.asElement()
方法的具体详情如下:
包路径:javax.lang.model.util.Types
类名称:Types
方法名:asElement
[英]Returns the element corresponding to a type. The type may be a DeclaredType or TypeVariable. Returns null if the type is not one with a corresponding element.
[中]返回与类型对应的元素。类型可以是DeclaredType或TypeVariable。如果类型不是具有相应元素的类型,则返回null。
代码示例来源:origin: robolectric/robolectric
public Element asElement(TypeMirror typeMirror) {
return types.asElement(typeMirror);
}
代码示例来源:origin: jenkinsci/jenkins
private Element asElement(TypeMirror m) {
return processingEnv.getTypeUtils().asElement(m);
}
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Return the super class of the specified {@link Element} or null if this
* {@code element} represents {@link Object}.
*/
public Element getSuperClass(Element element) {
List<? extends TypeMirror> superTypes = this.types.directSupertypes(element.asType());
if (superTypes.isEmpty()) {
return null; // reached java.lang.Object
}
return this.types.asElement(superTypes.get(0));
}
代码示例来源:origin: greenrobot/EventBus
private TypeElement getSuperclass(TypeElement type) {
if (type.getSuperclass().getKind() == TypeKind.DECLARED) {
TypeElement superclass = (TypeElement) processingEnv.getTypeUtils().asElement(type.getSuperclass());
String name = superclass.getQualifiedName().toString();
if (name.startsWith("java.") || name.startsWith("javax.") || name.startsWith("android.")) {
// Skip system classes, this just degrades performance
return null;
} else {
return superclass;
}
} else {
return null;
}
}
代码示例来源:origin: bumptech/glide
/**
* Returns a safe String to use in a Javadoc that will function in a link.
*
* <p>This method exists because by Javadoc doesn't handle type parameters({@literal <T>}
* in {@literal RequestOptions<T>} for example).
*/
private TypeName getJavadocSafeName(Element element) {
Types typeUtils = processingEnv.getTypeUtils();
TypeMirror type = element.asType();
if (typeUtils.asElement(type) == null) {
// If there is no Element, it's a primitive and can't have additional types, so we're done.
return ClassName.get(element.asType());
}
Name simpleName = typeUtils.asElement(type).getSimpleName();
return ClassName.bestGuess(simpleName.toString());
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Return the interfaces that are <strong>directly</strong> implemented by the
* specified {@link Element} or an empty list if this {@code element} does not
* implement any interface.
*/
public List<Element> getDirectInterfaces(Element element) {
List<? extends TypeMirror> superTypes = this.types.directSupertypes(element.asType());
List<Element> directInterfaces = new ArrayList<>();
if (superTypes.size() > 1) { // index 0 is the super class
for (int i = 1; i < superTypes.size(); i++) {
Element e = this.types.asElement(superTypes.get(i));
if (e != null) {
directInterfaces.add(e);
}
}
}
return directInterfaces;
}
代码示例来源:origin: immutables/immutables
protected String toName(TypeMirror typeMirror) {
return ((TypeElement) Preconditions.checkNotNull(
types.asElement(typeMirror), "not declared type"))
.getQualifiedName().toString();
}
}
代码示例来源:origin: airbnb/epoxy
static Element getElementByName(String name, Elements elements, Types types) {
try {
return elements.getTypeElement(name);
} catch (MirroredTypeException mte) {
return types.asElement(mte.getTypeMirror());
}
}
代码示例来源:origin: requery/requery
Optional<EntityDescriptor> embeddedDescriptorOf(AttributeDescriptor attribute) {
TypeMirror mirror = attribute.typeMirror();
Element element = types.asElement(mirror);
if (element instanceof TypeElement) {
return Optional.ofNullable(embeddedTypes.get(element));
}
return Optional.empty();
}
代码示例来源:origin: hibernate/hibernate-orm
@Override
public Boolean visitArray(ArrayType t, Element element) {
TypeMirror componentMirror = t.getComponentType();
TypeElement componentElement = (TypeElement) context.getTypeUtils().asElement( componentMirror );
return Constants.BASIC_ARRAY_TYPES.contains( componentElement.getQualifiedName().toString() );
}
代码示例来源:origin: neo4j/neo4j
private Stream<CompilationMessage> validateRecord( TypeMirror recordType )
{
Element recordElement = typeUtils.asElement( recordType );
return Stream.concat( validateFieldModifiers( recordElement ), validateFieldType( recordElement ) );
}
代码示例来源:origin: hibernate/hibernate-orm
@Override
public String visitDeclared(DeclaredType declaredType, Element element) {
TypeElement returnedElement = (TypeElement) context.getTypeUtils().asElement( declaredType );
String fqNameOfReturnType = null;
if ( containsAnnotation( returnedElement, Constants.EMBEDDABLE ) ) {
fqNameOfReturnType = returnedElement.getQualifiedName().toString();
}
return fqNameOfReturnType;
}
代码示例来源:origin: androidannotations/androidannotations
public void typeHasAnnotation(Class<? extends Annotation> annotation, TypeMirror elementType, ElementValidation valid) {
Element typeElement = annotationHelper.getTypeUtils().asElement(elementType);
if (!elementHasAnnotationSafe(annotation, typeElement)) {
valid.addError("%s can only be used on an element annotated with @" + annotation.getName());
}
}
代码示例来源:origin: bumptech/glide
private MethodSpec overrideGlideStaticMethod(ExecutableElement methodToOverride) {
List<ParameterSpec> parameters = ProcessorUtil.getParameters(methodToOverride);
TypeElement element =
(TypeElement) processingEnv.getTypeUtils().asElement(methodToOverride.getReturnType());
MethodSpec.Builder builder =
MethodSpec.methodBuilder(methodToOverride.getSimpleName().toString())
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addJavadoc(processorUtil.generateSeeMethodJavadoc(methodToOverride))
.addParameters(parameters);
addReturnAnnotations(builder, methodToOverride);
boolean returnsValue = element != null;
if (returnsValue) {
builder.returns(ClassName.get(element));
}
StringBuilder code = new StringBuilder(returnsValue ? "return " : "");
code.append("$T.$N(");
List<Object> args = new ArrayList<>();
args.add(ClassName.get(glideType));
args.add(methodToOverride.getSimpleName());
if (!parameters.isEmpty()) {
for (ParameterSpec param : parameters) {
code.append("$L, ");
args.add(param.name);
}
code = new StringBuilder(code.substring(0, code.length() - 2));
}
code.append(")");
builder.addStatement(code.toString(), args.toArray(new Object[0]));
return builder.build();
}
代码示例来源:origin: androidannotations/androidannotations
public void typeIsValid(Class<? extends Annotation> annotation, TypeMirror elementType, ElementValidation elementValidation) {
Set<? extends Element> validElements = validatedModel().getRootAnnotatedElements(annotation.getName());
Set<? extends Element> extractedElements = environment().getExtractedElements().getRootAnnotatedElements(annotation.getName());
Element typeElement = annotationHelper.getTypeUtils().asElement(elementType);
if (!extractedElements.contains(typeElement) || validElements.contains(typeElement)) {
return;
}
elementValidation.addError("The type " + typeElement.getSimpleName() + " is invalid, " + "please check the messages on that type.");
}
代码示例来源:origin: neo4j/neo4j
private Stream<CompilationMessage> validateAggregationType( ExecutableElement aggregationFunction )
{
TypeMirror returnType = aggregationFunction.getReturnType();
Element returnTypeElement = types.asElement( returnType );
if ( returnTypeElement == null )
{
return Stream.of( new AggregationError( aggregationFunction,
"Unsupported return type <%s> of aggregation function.", returnType.toString(),
aggregationFunction.getEnclosingElement() ) );
}
List<ExecutableElement> updateMethods = methodsAnnotatedWith( returnTypeElement, UserAggregationUpdate.class );
List<ExecutableElement> resultMethods = methodsAnnotatedWith( returnTypeElement, UserAggregationResult.class );
return Stream.concat( validateAggregationUpdateMethod( aggregationFunction, returnTypeElement, updateMethods ),
validateAggregationResultMethod( aggregationFunction, returnTypeElement, resultMethods ) );
}
代码示例来源:origin: greenrobot/EventBus
List<? extends VariableElement> parameters = method.getParameters();
TypeMirror paramType = getParamTypeMirror(parameters.get(0), null);
TypeElement paramElement = (TypeElement) processingEnv.getTypeUtils().asElement(paramType);
String methodName = method.getSimpleName().toString();
String eventClass = getClassString(paramElement, myPackage) + ".class";
代码示例来源:origin: immutables/immutables
private ImmutableList<TypeMirror> extractImports(TypeElement type) {
ImmutableList.Builder<TypeMirror> importedTypes = ImmutableList.builder();
for (TypeElement t = type; t != null; t = (TypeElement) types.asElement(t.getSuperclass())) {
importedTypes.addAll(extractDeclaredImports(t).reverse());
importedTypes.addAll(extractDeclaredImports(elements.getPackageOf(type)).reverse());
}
return importedTypes.build().reverse();
}
代码示例来源:origin: androidannotations/androidannotations
private void rebindContextIfBean(Element element, JBlock initIfNonConfigurationNotNullBlock, JFieldVar field) {
boolean hasBeanAnnotation = element.getAnnotation(Bean.class) != null;
if (hasBeanAnnotation) {
TypeMirror elementType = annotationHelper.extractAnnotationClassParameter(element, Bean.class.getName());
if (elementType == null) {
elementType = element.asType();
}
String typeQualifiedName = elementType.toString();
AbstractJClass fieldGeneratedBeanClass = getJClass(typeQualifiedName + classSuffix());
// do not generate rebind call for singleton beans
Element eBeanTypeElement = annotationHelper.getTypeUtils().asElement(elementType);
EBean eBean = eBeanTypeElement.getAnnotation(EBean.class);
if (eBean != null && eBean.scope() != EBean.Scope.Singleton) {
initIfNonConfigurationNotNullBlock.invoke(cast(fieldGeneratedBeanClass, field), "rebind").arg(_this());
}
}
}
}
代码示例来源:origin: requery/requery
ParameterizedTypeName parameterizedCollectionName(TypeMirror typeMirror) {
TypeMirror genericType = tryFirstTypeArgument(typeMirror);
TypeName elementName = nameResolver.tryGeneratedTypeName(genericType);
TypeElement collectionElement = (TypeElement) types.asElement(typeMirror);
ClassName collectionName = ClassName.get(collectionElement);
return ParameterizedTypeName.get(collectionName, elementName);
}
内容来源于网络,如有侵权,请联系作者删除!