当前位置: 首页>>代码示例>>Java>>正文


Java TypeElement.asType方法代码示例

本文整理汇总了Java中javax.lang.model.element.TypeElement.asType方法的典型用法代码示例。如果您正苦于以下问题:Java TypeElement.asType方法的具体用法?Java TypeElement.asType怎么用?Java TypeElement.asType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.lang.model.element.TypeElement的用法示例。


在下文中一共展示了TypeElement.asType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getViewsType

import javax.lang.model.element.TypeElement; //导入方法依赖的package包/类
private Collection<TypeMirror> getViewsType(TypeElement typeElement) {
	TypeMirror superclass = typeElement.asType();

	List<TypeMirror> result = new ArrayList<>();

	while (superclass.getKind() != TypeKind.NONE) {
		TypeElement superclassElement = (TypeElement) ((DeclaredType) superclass).asElement();
		Collection<? extends TypeMirror> interfaces = new HashSet<>(superclassElement.getInterfaces());
		for (TypeMirror typeMirror : interfaces) {
			if (typeMirror instanceof DeclaredType) {
				result.addAll(getViewsType((TypeElement) ((DeclaredType) typeMirror).asElement()));
			}
		}
		result.addAll(interfaces);
		result.add(superclass);

		superclass = superclassElement.getSuperclass();
	}

	return result;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:22,代码来源:PresenterInjectorRules.java

示例2: getTypeMirror

import javax.lang.model.element.TypeElement; //导入方法依赖的package包/类
@Override
public TypeMirror getTypeMirror(TreePath arg0) {
    Tree tree = arg0.getLeaf();
    if (tree.getKind() == Tree.Kind.IDENTIFIER) {
        Map<String, ObjectVariable> map = null;
        try {
            // [TODO] add JPDADebuggerImpl.getAllLabels() to API
            Method method = debugger.getClass().getMethod("getAllLabels"); // NOI18N
            map = (Map<String, ObjectVariable>) method.invoke(debugger);
        } catch (Exception ex) {
            Exceptions.printStackTrace(ex);
        }
        if (map != null) {
            String name = ((IdentifierTree)tree).getName().toString();
            ObjectVariable var = map.get(name);
            if (var != null) {
                Elements elements = controller.getElements();
                TypeElement typeElem = elements.getTypeElement(var.getClassType().getName());
                if (typeElem != null) {
                    return typeElem.asType();
                }
            }
        }
    }
    return trees.getTypeMirror(arg0);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:27,代码来源:WatchPanel.java

示例3: resolveCapturedType

import javax.lang.model.element.TypeElement; //导入方法依赖的package包/类
private static TypeMirror resolveCapturedType(CompilationInfo info, TypeMirror tm) {
    if (tm == null) {
        return tm;
    }
    if (tm.getKind() == TypeKind.ERROR) {
        tm = info.getTrees().getOriginalType((ErrorType) tm);
    }
    TypeMirror type = resolveCapturedTypeInt(info, tm);
    if (type == null) {
        return tm;
    }
    if (type.getKind() == TypeKind.WILDCARD) {
        TypeMirror tmirr = ((WildcardType) type).getExtendsBound();
        if (tmirr != null)
            return tmirr;
        else { //no extends, just '?'
            TypeElement te = info.getElements().getTypeElement("java.lang.Object"); // NOI18N
            return te == null ? null : te.asType();
        }
            
    }
    
    return type;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:25,代码来源:TypeUtilities.java

示例4: VisitorImpl

import javax.lang.model.element.TypeElement; //导入方法依赖的package包/类
public VisitorImpl(CompilationInfo info, TypeElement undefinedSymbolScope, Cancel cancel) {
    super();
    this.info = info;
    this.cancel = cancel;
    this.undefinedSymbolScope = undefinedSymbolScope;
    this.thisName = info.getElements().getName("this");

    this.throwableEl = info.getElements().getTypeElement("java.lang.Throwable"); // NOI18N
    TypeElement tel =  info.getElements().getTypeElement("java.lang.RuntimeException"); // NOI18N
    if (tel != null) {
        runtimeExceptionType = tel.asType();
    } else {
        runtimeExceptionType = null;
    }
    tel =  info.getElements().getTypeElement("java.lang.Error"); // NOI18N
    if (tel != null) {
        errorType = tel.asType();
    } else {
        errorType = null;
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:22,代码来源:Flow.java

示例5: resolveCapturedType

import javax.lang.model.element.TypeElement; //导入方法依赖的package包/类
public static TypeMirror resolveCapturedType(CompilationInfo info, TypeMirror tm) {
    if (tm == null) {
        return tm;
    }
    if (tm.getKind() == TypeKind.ERROR) {
        tm = info.getTrees().getOriginalType((ErrorType) tm);
    }
    TypeMirror type = resolveCapturedTypeInt(info, tm);
    if (type == null) {
        return tm;
    }
    if (type.getKind() == TypeKind.WILDCARD) {
        TypeMirror tmirr = ((WildcardType) type).getExtendsBound();
        if (tmirr != null)
            return tmirr;
        else { //no extends, just '?'
            TypeElement te = info.getElements().getTypeElement("java.lang.Object"); // NOI18N
            return te == null ? null : te.asType();
        }
            
    }
    
    return type;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:25,代码来源:Utilities.java

示例6: CreateEnumConstant

import javax.lang.model.element.TypeElement; //导入方法依赖的package包/类
public CreateEnumConstant(CompilationInfo info, String name, Set<Modifier> modifiers, TypeElement target, TypeMirror proposedType, FileObject targetFile) {
    this.name = name;
    this.inFQN = target.getQualifiedName().toString();
    this.cpInfo = info.getClasspathInfo();
    this.targetFile = targetFile;
    this.target = ElementHandle.create(target);
    if (proposedType.getKind() == TypeKind.NULL) {
        TypeElement tel = info.getElements().getTypeElement("java.lang.Object"); // NOI18N
        if (tel != null) {
            proposedType = tel.asType();
            this.proposedType = TypeMirrorHandle.create(proposedType);
        } else {
            this.proposedType = null;
        }
    } else {
        this.proposedType = TypeMirrorHandle.create(proposedType);
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:19,代码来源:CreateEnumConstant.java

示例7: checkThrowsDeclared

import javax.lang.model.element.TypeElement; //导入方法依赖的package包/类
@NbBundle.Messages({"# {0} - [@throws|@exception]", "# {1} - @throws name",
                    "DUPLICATE_THROWS_DESC=Duplicate @{0} tag: {1}",
                    "# {0} - [@throws|@exception]", "# {1} - @throws name",
                    "UNKNOWN_THROWABLE_DESC=Unknown throwable: @{0} {1}"})
private void checkThrowsDeclared(ThrowsTree tree, Element ex, String fqn, List<? extends TypeMirror> list, DocTreePathHandle dtph, int start, int end, List<ErrorDescription> errors) {
    boolean found = false;
    final TypeMirror type;
    if(ex != null) {
        type = ex.asType();
    } else {
        TypeElement typeElement = javac.getElements().getTypeElement(fqn);
        if(typeElement != null) {
            type = typeElement.asType();
        } else {
            type = null;
        }
    }
    for (TypeMirror t: list) {
        if(ctx.isCanceled()) { return; }
        if(type != null && javac.getTypes().isAssignable(type, t)) {
            if(!foundThrows.add(type)) {
                errors.add(ErrorDescriptionFactory.forSpan(ctx, start, end, DUPLICATE_THROWS_DESC(tree.getTagName(), fqn), new RemoveTagFix(dtph, "@" + tree.getTagName()).toEditorFix()));
            }
            found = true;
            break;
        }
        if (type == null && fqn.equals(t.toString())) {
            if(!foundThrows.add(t)) {
                errors.add(ErrorDescriptionFactory.forSpan(ctx, start, end, DUPLICATE_THROWS_DESC(tree.getTagName(), fqn), new RemoveTagFix(dtph, "@" + tree.getTagName()).toEditorFix()));
            }
            found = true;
            break;
        }
    }
    if (!found) {
        errors.add(ErrorDescriptionFactory.forSpan(ctx, start, end, UNKNOWN_THROWABLE_DESC(tree.getTagName(), fqn), new RemoveTagFix(dtph, "@" + tree.getTagName()).toEditorFix()));
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:39,代码来源:Analyzer.java

示例8: getViewClassFromGeneric

import javax.lang.model.element.TypeElement; //导入方法依赖的package包/类
private String getViewClassFromGeneric(TypeElement typeElement) {
	TypeMirror superclass = typeElement.asType();

	Map<String, String> parentTypes = Collections.emptyMap();

	if (!typeElement.getTypeParameters().isEmpty()) {
		MvpCompiler.getMessager().printMessage(Diagnostic.Kind.WARNING, "Your " + typeElement.getSimpleName() + " is typed. @InjectViewState may generate wrong code. Your can set view/view state class manually.");
	}

	while (superclass.getKind() != TypeKind.NONE) {
		TypeElement superclassElement = (TypeElement) ((DeclaredType) superclass).asElement();

		final List<? extends TypeMirror> typeArguments = ((DeclaredType) superclass).getTypeArguments();
		final List<? extends TypeParameterElement> typeParameters = superclassElement.getTypeParameters();

		if (typeArguments.size() > typeParameters.size()) {
			throw new IllegalArgumentException("Code generation for interface " + typeElement.getSimpleName() + " failed. Simplify your generics. (" + typeArguments + " vs " + typeParameters + ")");
		}

		Map<String, String> types = new HashMap<>();
		for (int i = 0; i < typeArguments.size(); i++) {
			types.put(typeParameters.get(i).toString(), fillGenerics(parentTypes, typeArguments.get(i)));
		}

		if (superclassElement.toString().equals(MVP_PRESENTER_CLASS)) {
			// MvpPresenter is typed only on View class
			return fillGenerics(parentTypes, typeArguments);
		}

		parentTypes = types;

		superclass = superclassElement.getSuperclass();
	}

	return "";
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:37,代码来源:ViewStateProviderClassGenerator.java

示例9: resolveType

import javax.lang.model.element.TypeElement; //导入方法依赖的package包/类
public TypeMirror resolveType(String typeName) {
    TypeElement type = getCompilationInfo().getElements().getTypeElement(typeName);
    if (type != null) {
        return type.asType();
    }
    return null;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:8,代码来源:AnnotationHelper.java

示例10: getSymbol

import javax.lang.model.element.TypeElement; //导入方法依赖的package包/类
public TypeMirror getSymbol(String signature) {
    TypeMirror type = symtab.get(signature);
    if (type == null) {
        TypeElement typeElement = elementUtils.getTypeElement(signature);
        if (typeElement == null)
            return null;
        type = typeElement.asType();
        if (type == null)
            return null;
        symtab.put(signature, type);
    }
    return type;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:Utils.java

示例11: getIterableGenericType

import javax.lang.model.element.TypeElement; //导入方法依赖的package包/类
/**
 *
 * @param info context {@link CompilationInfo}
 * @param iterable tested {@link TreePath}
 * @return generic type of an {@link Iterable} or {@link ArrayType} at a TreePath
 */
public static TypeMirror getIterableGenericType(CompilationInfo info, TreePath iterable) {
    TypeElement iterableElement = info.getElements().getTypeElement("java.lang.Iterable"); //NOI18N
    if (iterableElement == null) {
        return null;
    }
    TypeMirror iterableType = info.getTrees().getTypeMirror(iterable);
    if (iterableType == null) {
        return null;
    }
    TypeMirror designedType = null;
    if (iterableType.getKind() == TypeKind.DECLARED) {
        DeclaredType declaredType = (DeclaredType) iterableType;
        if (!info.getTypes().isSubtype(info.getTypes().erasure(declaredType), info.getTypes().erasure(iterableElement.asType()))) {
            return null;
        }
        ExecutableElement iteratorMethod = (ExecutableElement) iterableElement.getEnclosedElements().get(0);
        ExecutableType iteratorMethodType = (ExecutableType) info.getTypes().asMemberOf(declaredType, iteratorMethod);
        List<? extends TypeMirror> typeArguments = ((DeclaredType) iteratorMethodType.getReturnType()).getTypeArguments();
        if (!typeArguments.isEmpty()) {
            designedType = typeArguments.get(0);
        } else {
            TypeElement jlObject = info.getElements().getTypeElement("java.lang.Object");

            if (jlObject != null) {
                designedType = jlObject.asType();
            }
        }
    } else if (iterableType.getKind() == TypeKind.ARRAY) {
        designedType = ((ArrayType) iterableType).getComponentType();
    }
    if (designedType == null) {
        return null;
    }
    return resolveTypeForDeclaration(info, designedType);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:42,代码来源:Utilities.java

示例12: getIterableGenericType

import javax.lang.model.element.TypeElement; //导入方法依赖的package包/类
/**
 *
 * @param info context {@link CompilationInfo}
 * @param iterable tested {@link TreePath}
 * @return generic type of an {@link Iterable} or {@link ArrayType} at a TreePath
 */
public static TypeMirror getIterableGenericType(CompilationInfo info, TreePath iterable) {
    TypeElement iterableElement = info.getElements().getTypeElement("java.lang.Iterable"); //NOI18N
    if (iterableElement == null) {
        return null;
    }
    TypeMirror iterableType = info.getTrees().getTypeMirror(iterable);
    if (iterableType == null) {
        return null;
    }
    TypeMirror designedType = null;
    if (iterableType.getKind() == TypeKind.DECLARED) {
        DeclaredType declaredType = (DeclaredType) iterableType;
        if (!info.getTypes().isSubtype(info.getTypes().erasure(declaredType), info.getTypes().erasure(iterableElement.asType()))) {
            return null;
        }
        ExecutableElement iteratorMethod = (ExecutableElement) iterableElement.getEnclosedElements().get(0);
        ExecutableType iteratorMethodType = (ExecutableType) info.getTypes().asMemberOf(declaredType, iteratorMethod);
        List<? extends TypeMirror> typeArguments = ((DeclaredType) iteratorMethodType.getReturnType()).getTypeArguments();
        if (!typeArguments.isEmpty()) {
            designedType = typeArguments.get(0);
        } else {
            TypeElement jlObject = info.getElements().getTypeElement("java.lang.Object");

            if (jlObject != null) {
                designedType = jlObject.asType();
            }
        }
    } else if (iterableType.getKind() == TypeKind.ARRAY) {
        designedType = ((ArrayType) iterableType).getComponentType();
    }
    if (designedType == null) {
        return null;
    }
    return JavaPluginUtils.resolveCapturedType(info, designedType);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:42,代码来源:CreateElementUtilities.java

示例13: getHandlerBaseType

import javax.lang.model.element.TypeElement; //导入方法依赖的package包/类
private TypeMirror  getHandlerBaseType() {
    if (eventHandlerBase == null) {
        TypeElement el = compilationInfo.getElements().getTypeElement(JAVAFX_EVENT_BASE);
        if (el == null) {
            // FIXME - better exception, must be catched & reported outside
            throw new IllegalStateException();
        }
        eventHandlerBase = el.asType();
    }
    return eventHandlerBase;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:12,代码来源:BeanModelBuilder.java

示例14: checkNoLoggers

import javax.lang.model.element.TypeElement; //导入方法依赖的package包/类
@TriggerTreeKind({Tree.Kind.ANNOTATION_TYPE, Tree.Kind.CLASS, Tree.Kind.ENUM, Tree.Kind.INTERFACE})
public static Iterable<ErrorDescription> checkNoLoggers(HintContext ctx) {
    Element cls = ctx.getInfo().getTrees().getElement(ctx.getPath());
    if (cls == null || cls.getKind() != ElementKind.CLASS || cls.getModifiers().contains(Modifier.ABSTRACT) ||
        (cls.getEnclosingElement() != null && cls.getEnclosingElement().getKind() != ElementKind.PACKAGE)
    ) {
        return null;
    }

    TypeElement loggerTypeElement = ctx.getInfo().getElements().getTypeElement("java.util.logging.Logger"); // NOI18N
    if (loggerTypeElement == null) {
        return null;
    }
    TypeMirror loggerTypeElementAsType = loggerTypeElement.asType();
    if (loggerTypeElementAsType == null || loggerTypeElementAsType.getKind() != TypeKind.DECLARED) {
        return null;
    }

    List<TypeMirror> customLoggersList = new ArrayList<>();
    if (isCustomEnabled(ctx.getPreferences())) {
        List<String> customLoggerClasses = getCustomLoggers(ctx.getPreferences());
        if (customLoggerClasses != null) {
            for (String className : customLoggerClasses) {
                TypeElement customTypeElement = ctx.getInfo().getElements().getTypeElement(className);
                if (customTypeElement == null) {
                    continue;
                }
                TypeMirror customTypeMirror = customTypeElement.asType();
                if (customTypeMirror == null || customTypeMirror.getKind() != TypeKind.DECLARED) {
                    continue;
                }
                customLoggersList.add(customTypeMirror);
            }
        }
    }

    List<VariableElement> loggerFields = new LinkedList<VariableElement>();
    List<VariableElement> fields = ElementFilter.fieldsIn(cls.getEnclosedElements());
    for(VariableElement f : fields) {
        if (f.getKind() != ElementKind.FIELD) {
            continue;
        }

        if (f.asType().equals(loggerTypeElementAsType)) {
            loggerFields.add(f);
        } else if (customLoggersList.contains(f.asType())) {
            loggerFields.add(f);
        }
    }

    if (loggerFields.size() == 0) {
        return Collections.singleton(ErrorDescriptionFactory.forName(
                ctx,
                ctx.getPath(),
                NbBundle.getMessage(NoLoggers.class, "MSG_NoLoggers_checkNoLoggers", cls), //NOI18N
                new NoLoggersFix(NbBundle.getMessage(NoLoggers.class, "MSG_NoLoggers_checkNoLoggers_Fix", cls), TreePathHandle.create(cls, ctx.getInfo())).toEditorFix() //NOI18N
        ));
    } else {
        return null;
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:62,代码来源:NoLoggers.java

示例15: visitThrows

import javax.lang.model.element.TypeElement; //导入方法依赖的package包/类
@Override
    public Void visitThrows(ThrowsTree tree, List<ErrorDescription> errors) {
        boolean oldInheritDoc = foundInheritDoc;
        ReferenceTree exName = tree.getExceptionName();
        DocTreePath refPath = new DocTreePath(getCurrentPath(), tree.getExceptionName());
        Element ex = javac.getDocTrees().getElement(refPath);
        Types types = javac.getTypes();
        Elements elements = javac.getElements();
        final TypeElement throwableEl = elements.getTypeElement("java.lang.Throwable");
        final TypeElement errorEl = elements.getTypeElement("java.lang.Error");
        final TypeElement runtimeEl = elements.getTypeElement("java.lang.RuntimeException");
        if(throwableEl == null || errorEl == null || runtimeEl == null) {
            LOG.warning("Broken java-platform, cannot resolve " + throwableEl == null? "java.lang.Throwable" : errorEl == null? "java.lang.Error" : "java.lang.RuntimeException"); //NOI18N
            return null;
        }
        TypeMirror throwable = throwableEl.asType();
        TypeMirror error = errorEl.asType();
        TypeMirror runtime = runtimeEl.asType();
        DocTreePath currentDocPath = getCurrentPath();
        DocTreePathHandle dtph = DocTreePathHandle.create(currentDocPath, javac);
        if(dtph == null) {
            return null;
        }
        DocSourcePositions sp = (DocSourcePositions) javac.getTrees().getSourcePositions();
        int start = (int) sp.getStartPosition(javac.getCompilationUnit(), currentDocPath.getDocComment(), tree);
        int end = (int) sp.getEndPosition(javac.getCompilationUnit(), currentDocPath.getDocComment(), tree);
        if (ex == null || (ex.asType().getKind() == TypeKind.DECLARED
                && types.isAssignable(ex.asType(), throwable))) {
            switch (currentElement.getKind()) {
                case CONSTRUCTOR:
                case METHOD:
                    if (ex == null || !(types.isAssignable(ex.asType(), error)
                            || types.isAssignable(ex.asType(), runtime))) {
                        ExecutableElement ee = (ExecutableElement) currentElement;
                        String fqn;
                        if (ex != null) {
                            fqn = ((TypeElement) ex).getQualifiedName().toString();
                        } else {
                            ExpressionTree referenceClass = javac.getTreeUtilities().getReferenceClass(new DocTreePath(currentDocPath, exName));
                            if(referenceClass == null) break;
                            fqn = referenceClass.toString();
                        }
                        checkThrowsDeclared(tree, ex, fqn, ee.getThrownTypes(), dtph, start, end, errors);
                    }
                    break;
                default:
//                        env.messages.error(REFERENCE, tree, "dc.invalid.throws");
            }
        } else {
//                env.messages.error(REFERENCE, tree, "dc.invalid.throws");
        }
        warnIfEmpty(tree, tree.getDescription());
        super.visitThrows(tree, errors);
        foundInheritDoc = oldInheritDoc;
        return null;
    }
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:57,代码来源:Analyzer.java


注:本文中的javax.lang.model.element.TypeElement.asType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。