本文整理汇总了Java中javax.lang.model.element.Element.asType方法的典型用法代码示例。如果您正苦于以下问题:Java Element.asType方法的具体用法?Java Element.asType怎么用?Java Element.asType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.lang.model.element.Element
的用法示例。
在下文中一共展示了Element.asType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: ExposedMethod
import javax.lang.model.element.Element; //导入方法依赖的package包/类
public ExposedMethod(Element element) {
ExecutableType method = (ExecutableType) element.asType();
TypeElement declaringClass = (TypeElement) element.getEnclosingElement();
this.name = element.getSimpleName().toString();
this.originalMethod = declaringClass.getQualifiedName().toString() + "." + element.getSimpleName();
this.returnType = method.getReturnType().toString();
this.params = new ArrayList<>();
int count = 0;
for (TypeMirror param : method.getParameterTypes()) {
this.params.add(param.toString());
String[] components = param.toString().toLowerCase().split("\\.");
String paramName = components[components.length - 1];
if (paramName.endsWith(">")) {
paramName = paramName.substring(0, paramName.length() - 1);
}
this.params.add(paramName + count);
count++;
}
this.thrown = Stream.of(method.getThrownTypes()).map(new Function<TypeMirror, String>() {
@Override
public String apply(TypeMirror typeMirror) {
return typeMirror.toString();
}
}).toList();
}
示例2: getCompatibilityType
import javax.lang.model.element.Element; //导入方法依赖的package包/类
private CompatibilityType getCompatibilityType(Element field) {
TypeMirror typeMirror = field.asType();
for (CompatibilityType compatibilityType : CompatibilityType.values()) {
if (compatibilityType.mGenericClass == null) {
if (isAssignable(typeMirror, compatibilityType.mClass)) {
return compatibilityType;
}
} else if (typeMirror.getKind() != TypeKind.WILDCARD) {
if (isAssignable(mTypeUtils.erasure(typeMirror), compatibilityType.mClass)) {
List<? extends TypeMirror> typeArguments = ((DeclaredType) typeMirror).getTypeArguments();
if (typeArguments != null && typeArguments.size() >= 1 && isAssignable(typeArguments.get(0), compatibilityType.mGenericClass)) {
return compatibilityType;
}
}
}
}
return null;
}
示例3: isErroneous
import javax.lang.model.element.Element; //导入方法依赖的package包/类
/**
* Checks whether 'e' contains error or is missing. If the passed element is null
* it's assumed the element could not be resolved and this method returns true. Otherwise,
* the element's type kind is checked against error constants and finally the erroneous
* state of the element is checked.
*
* @param e Element to check or {@code null}
* @return true, if the element is missing (is {@code null}) or contains errors.
*/
public boolean isErroneous(@NullAllowed Element e) {
if (e == null) {
return true;
}
if (e.getKind() == ElementKind.MODULE && ((Symbol)e).kind == Kinds.Kind.ERR) {
return true;
}
final TypeMirror type = e.asType();
if (type == null) {
return false;
}
if (type.getKind() == TypeKind.ERROR || type.getKind() == TypeKind.OTHER) {
return true;
}
if (type instanceof Type) {
if (((Type)type).isErroneous()) {
return true;
}
}
return false;
}
示例4: isInSerializableOrExternalizable
import javax.lang.model.element.Element; //导入方法依赖的package包/类
/**
* Also returns true on error / undecidable situation, so the filtering
* will probably accept serial methods and will not mark them as unused, if
* the class declaration is errneous.
*
* @param info the compilation context
* @param e the class member (the enclosing element will be tested)
* @return true, if in serializable/externalizable or unknown
*/
private static boolean isInSerializableOrExternalizable(CompilationInfo info, Element e) {
Element encl = e.getEnclosingElement();
if (encl == null || !encl.getKind().isClass()) {
return true;
}
TypeMirror m = encl.asType();
if (m == null || m.getKind() != TypeKind.DECLARED) {
return true;
}
Element serEl = info.getElements().getTypeElement("java.io.Serializable"); // NOI18N
Element extEl = info.getElements().getTypeElement("java.io.Externalizable"); // NOI18N
if (serEl == null || extEl == null) {
return true;
}
if (info.getTypes().isSubtype(m, serEl.asType())) {
return true;
}
if (info.getTypes().isSubtype(m, extEl.asType())) {
return true;
}
return false;
}
示例5: visitLambdaExpression
import javax.lang.model.element.Element; //导入方法依赖的package包/类
@Override
public Tree visitLambdaExpression(LambdaExpressionTree node, Element elementToFind) {
if (workingCopy.getTreeUtilities().isSynthetic(getCurrentPath())) {
return super.visitLambdaExpression(node, elementToFind);
}
Trees trees = workingCopy.getTrees();
Types types = workingCopy.getTypes();
TypeMirror type1 = trees.getTypeMirror(getCurrentPath());
if(type1 == null) {
return super.visitLambdaExpression(node, elementToFind);
}
type1 = types.erasure(type1);
TypeMirror type2 = elementToFind.asType();
type2 = types.erasure(type2);
if (types.isSameType(type1, type2) || (recursive && isSubtype(type1, type2))) {
addUsage(getCurrentPath());
}
return super.visitLambdaExpression(node, elementToFind);
}
示例6: getPrimitiveArrayClassName
import javax.lang.model.element.Element; //导入方法依赖的package包/类
public static String getPrimitiveArrayClassName(Element element) {
if (element.asType().getKind() != TypeKind.ARRAY) {
throw new IllegalArgumentException("element is not array: " + element.toString());
}
ArrayType arrayType = (ArrayType) element.asType();
TypeMirror componentType = arrayType.getComponentType();
ClassName className = (ClassName) TypeName.get(componentType).box();
return className.simpleName() + "Array";
}
示例7: isLazy
import javax.lang.model.element.Element; //导入方法依赖的package包/类
/**
* This method checks if the type is Lazy. Some DI frameworks may not be extending {@link
* Provider}, ex: Dagger, and so we make a string comparision of "Lazy" with the class name.
*
* @param element
* @return true if "Lazy".equals(className), false otherwise
*/
private boolean isLazy(Element element) {
final TypeMirror type = element.asType();
if (type.getKind() == TypeKind.DECLARED) {
final Name className = ((DeclaredType) type).asElement().getSimpleName();
return LAZY_CLASS_NAME.equals(className.toString());
}
return false;
}
示例8: visitDeclared
import javax.lang.model.element.Element; //导入方法依赖的package包/类
@Override
public Object visitDeclared(DeclaredType t, Object p) {
Element e = t.asElement();
if (e == null) {
unknownDeclaredTypes.add(t);
} else {
TypeMirror back = e.asType();
if (back == null || back.getKind() == TypeKind.ERROR) {
unknownDeclaredTypes.add(t);
}
}
return super.visitDeclared(t, p);
}
示例9: parseInjectView
import javax.lang.model.element.Element; //导入方法依赖的package包/类
private void parseInjectView(Element element, Map<TypeElement, ViewInjector> targetClassMap, Set<String> erasedTargetNames) {
boolean hasError = false;
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
TypeMirror elementType = element.asType();
if (elementType instanceof TypeVariable) {
elementType = ((TypeVariable) elementType).getUpperBound();
}
if (!(isSubtypeOfType(elementType, VIEW_TYPE) || isInterface(elementType))) {
error(element, "@InjectView fields must extend from View or be an interface. (%s.%s)", enclosingElement.getQualifiedName(), element.getSimpleName());
hasError = true;
}
hasError = (hasError | isInaccessibleViaGeneratedCode(InjectView.class, "fields", element)) | isBindingInWrongPackage(InjectView.class, element);
if (element.getAnnotation(InjectViews.class) != null) {
error(element, "Only one of @InjectView and @InjectViews is allowed. (%s.%s)", enclosingElement.getQualifiedName(), element.getSimpleName());
hasError = true;
}
if (!hasError) {
int id = ((InjectView) element.getAnnotation(InjectView.class)).value();
ViewInjector injector = (ViewInjector) targetClassMap.get(enclosingElement);
if (injector != null) {
ViewInjection viewInjection = injector.getViewInjection(id);
if (viewInjection != null) {
Iterator<ViewBinding> iterator = viewInjection.getViewBindings().iterator();
if (iterator.hasNext()) {
ViewBinding existingBinding = (ViewBinding) iterator.next();
error(element, "Attempt to use @InjectView for an already injected ID %d on '%s'. (%s.%s)", Integer.valueOf(id), existingBinding.getName(), enclosingElement.getQualifiedName(), element.getSimpleName());
return;
}
}
}
getOrCreateTargetClass(targetClassMap, enclosingElement).addView(id, new ViewBinding(element.getSimpleName().toString(), elementType.toString(), element.getAnnotation(Optional.class) == null));
erasedTargetNames.add(enclosingElement.toString());
}
}
示例10: elementSignature
import javax.lang.model.element.Element; //导入方法依赖的package包/类
private String elementSignature(Element el) {
switch (el.getKind()) {
case ANNOTATION_TYPE: case CLASS: case ENUM: case INTERFACE:
return ((TypeElement) el).getQualifiedName().toString();
case FIELD:
return elementSignature(el.getEnclosingElement()) + "." + el.getSimpleName() + ":" + el.asType();
case ENUM_CONSTANT:
return elementSignature(el.getEnclosingElement()) + "." + el.getSimpleName();
case EXCEPTION_PARAMETER: case LOCAL_VARIABLE: case PARAMETER: case RESOURCE_VARIABLE:
return el.getSimpleName() + ":" + el.asType();
case CONSTRUCTOR: case METHOD:
StringBuilder header = new StringBuilder();
header.append(elementSignature(el.getEnclosingElement()));
if (el.getKind() == ElementKind.METHOD) {
header.append(".");
header.append(el.getSimpleName());
}
header.append("(");
String sep = "";
ExecutableElement method = (ExecutableElement) el;
for (Iterator<? extends VariableElement> i = method.getParameters().iterator(); i.hasNext();) {
VariableElement p = i.next();
header.append(sep);
header.append(p.asType());
sep = ", ";
}
header.append(")");
return header.toString();
default:
return el.toString();
}
}
示例11: getKindParameter
import javax.lang.model.element.Element; //导入方法依赖的package包/类
/**
* Return the type parameter of a class that extends {@code Lazy} or {@code Provider}.
*
* <ul>
* <li>Return parameter type Foo for, Lazy<Foo>.
* <li>Return FooProvider if no parameter type.
* <li>Return null, if we are unable to determine any type/kind of element.
* </ul>
*
* @param element
* @return the TypeElement for declared type.
*/
@Nullable
private TypeElement getKindParameter(Element element) {
final TypeMirror type = element.asType();
if (TypeKind.DECLARED == type.getKind()) {
final List<? extends TypeMirror> typeMirrors = ((DeclaredType) type).getTypeArguments();
if (typeMirrors.size() == 1) {
return (TypeElement) typeUtils.asElement(typeUtils.erasure(typeMirrors.get(0)));
} else {
return (TypeElement) typeUtils.asElement(element.asType());
}
}
return null;
}
示例12: isError
import javax.lang.model.element.Element; //导入方法依赖的package包/类
private static boolean isError(Element el) {
return el == null || el.asType() == null || el.asType().getKind() == TypeKind.ERROR;
}
示例13: process
import javax.lang.model.element.Element; //导入方法依赖的package包/类
@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment environment) {
for(final Element element : environment.getElementsAnnotatedWith(GetMany.class)) {
final GetMany annotation = element.getAnnotation(GetMany.class);
if(ElementKind.METHOD != element.getKind()) {
throw new DataSourceDefinitionException(
"Must use @GetMany with methods only! Tried to use with " + element.getSimpleName()
+ ", which is not a method.");
}
final Set<Modifier> modifiers = element.getModifiers();
if(!modifiers.contains(Modifier.PUBLIC)) {
throw new DataSourceDefinitionException(
"Must use @GetMany with public methods only! Tried to use with " + element.getSimpleName()
+ ", which is not public.");
}
if(modifiers.contains(Modifier.STATIC)) {
throw new DataSourceDefinitionException(
"Must use @GetMany with non-static methods only! Tried to use with " + element.getSimpleName()
+ ", which is static.");
}
final ExecutableType method = (ExecutableType)element.asType();
final Types types = processingEnv.getTypeUtils();
final Elements elements = processingEnv.getElementUtils();
// annotation.value() will throw the exception because it doesn't have compiled class information yet, and is of type Class<?>.
TypeMirror annotatedType = null;
try {
annotation.value();
} catch(final MirroredTypeException e) {
annotatedType = e.getTypeMirror();
}
final TypeElement closeableIteratorType = elements.getTypeElement(CloseableIterator.class.getName());
final DeclaredType returnType = types.getDeclaredType(closeableIteratorType, annotatedType);
if(!types.isAssignable(method.getReturnType(), returnType)) {
throw new DataSourceDefinitionException(
"@GetMany methods must return a CloseableIterator over a type which is assignable from the annotation value. Tried to use with "
+ element.getSimpleName() + ", which returns " + method.getReturnType() + ".");
}
if(method.getParameterTypes().size() != 2) {
throw new DataSourceDefinitionException(
"@GetMany methods must take 2 arguments: Map<String, Object> query, PipelineContext context. Tried to use with "
+ element.getSimpleName() + ", which has a different signature.");
}
final TypeMirror stringType = elements.getTypeElement(String.class.getName()).asType();
final TypeMirror objectType = elements.getTypeElement(Object.class.getName()).asType();
final TypeElement mapType = elements.getTypeElement(Map.class.getName());
final TypeMirror[] genericTypes = new TypeMirror[] {stringType, objectType};
final DeclaredType queryType = types.getDeclaredType(mapType, genericTypes);
final TypeMirror contextType = elements.getTypeElement(PipelineContext.class.getName()).asType();
if(!types.isAssignable(method.getParameterTypes().get(0), queryType)) {
throw new DataSourceDefinitionException("@GetMany method first argument must be assignable from " + queryType + ". Tried to use with "
+ element.getSimpleName() + ", which takes " + method.getParameterTypes().get(0) + ".");
}
if(!types.isAssignable(method.getParameterTypes().get(1), contextType)) {
throw new DataSourceDefinitionException("@GetMany method second argument must be assignable from " + contextType + ". Tried to use with "
+ element.getSimpleName() + ", which takes " + method.getParameterTypes().get(1) + ".");
}
}
return true;
}
示例14: process
import javax.lang.model.element.Element; //导入方法依赖的package包/类
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Map<String, TypeMirror> types = new HashMap<>();
TypeMirror expectedInterfaceType =
processingEnv.getElementUtils().getTypeElement(Command.class.getCanonicalName())
.asType();
PackageElement pkg = null;
for (TypeElement a : annotations) {
for (Element element : roundEnv.getElementsAnnotatedWith(a)) {
Parameters parameters =
Objects.requireNonNull(element.getAnnotation(Parameters.class));
TypeMirror typeMirror = element.asType();
if (pkg == null) {
pkg = processingEnv.getElementUtils().getPackageOf(element);
}
if (!processingEnv.getTypeUtils().isAssignable(typeMirror, expectedInterfaceType)) {
processingEnv.getMessager()
.printMessage(Diagnostic.Kind.ERROR,
String.format("Class %s does not implement %s",
element.getSimpleName(),
Command.class.getCanonicalName()));
}
for (String commandName : extractCommandNames(element.getSimpleName(),
parameters.commandNames())) {
types.put(commandName, typeMirror);
}
}
}
if (!types.isEmpty()) {
try {
writeFile(types, pkg);
}
catch (IOException e) {
processingEnv.getMessager()
.printMessage(Diagnostic.Kind.ERROR,
"Unable to process jeesh annotations: " +
e.getMessage());
}
}
return false;
}
示例15: process
import javax.lang.model.element.Element; //导入方法依赖的package包/类
@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment environment) {
for(final Element element : environment.getElementsAnnotatedWith(Get.class)) {
final Get annotation = element.getAnnotation(Get.class);
if(ElementKind.METHOD != element.getKind()) {
throw new DataSourceDefinitionException(
"Must use @Get with methods only! Tried to use with " + element.getSimpleName()
+ ", which is not a method.");
}
final Set<Modifier> modifiers = element.getModifiers();
if(!modifiers.contains(Modifier.PUBLIC)) {
throw new DataSourceDefinitionException(
"Must use @Get with public methods only! Tried to use with " + element.getSimpleName()
+ ", which is not public.");
}
if(modifiers.contains(Modifier.STATIC)) {
throw new DataSourceDefinitionException(
"Must use @Get with non-static methods only! Tried to use with " + element.getSimpleName()
+ ", which is static.");
}
final ExecutableType method = (ExecutableType)element.asType();
final Types types = processingEnv.getTypeUtils();
final Elements elements = processingEnv.getElementUtils();
// annotation.value() will throw the exception because it doesn't have compiled class information yet, and is of type Class<?>.
TypeMirror annotatedType = null;
try {
annotation.value();
} catch(final MirroredTypeException e) {
annotatedType = e.getTypeMirror();
}
if(!types.isAssignable(method.getReturnType(), annotatedType)) {
throw new DataSourceDefinitionException("@Get method return type must be assignable from the annotation value! Tried to use with "
+ element.getSimpleName() + ", which returns " + method.getReturnType() + ".");
}
if(method.getParameterTypes().size() != 2) {
throw new DataSourceDefinitionException(
"@Get methods must take 2 arguments: Map<String, Object> query, PipelineContext context. Tried to use with "
+ element.getSimpleName()
+ ", which has a different signature.");
}
final TypeMirror stringType = elements.getTypeElement(String.class.getName()).asType();
final TypeMirror objectType = elements.getTypeElement(Object.class.getName()).asType();
final TypeElement mapType = elements.getTypeElement(Map.class.getName());
final TypeMirror[] genericTypes = new TypeMirror[] {stringType, objectType};
final DeclaredType queryType = types.getDeclaredType(mapType, genericTypes);
final TypeMirror contextType = elements.getTypeElement(PipelineContext.class.getName()).asType();
if(!types.isAssignable(method.getParameterTypes().get(0), queryType)) {
throw new DataSourceDefinitionException("@Get method first argument must be assignable from " + queryType + ". Tried to use with "
+ element.getSimpleName() + ", which takes " + method.getParameterTypes().get(0) + ".");
}
if(!types.isAssignable(method.getParameterTypes().get(1), contextType)) {
throw new DataSourceDefinitionException("@Get method second argument must be assignable from " + contextType + ". Tried to use with "
+ element.getSimpleName() + ", which takes " + method.getParameterTypes().get(1) + ".");
}
}
return true;
}