Java Code Examples for org.netbeans.api.java.source.CompilationInfo#getTypes()

The following examples show how to use org.netbeans.api.java.source.CompilationInfo#getTypes() . 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: ConvertToLambdaPreconditionChecker.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public ConvertToLambdaPreconditionChecker(TreePath pathToNewClassTree, CompilationInfo info) {

        this.pathToNewClassTree = pathToNewClassTree;
        this.newClassTree = (NewClassTree) pathToNewClassTree.getLeaf();
        this.info = info;
        this.types = info.getTypes();

        Element el = info.getTrees().getElement(pathToNewClassTree);
        if (el != null && el.getKind() == ElementKind.CONSTRUCTOR) {
            createdClass = el.getEnclosingElement();
        } else {
            createdClass = null;
        }
        this.lambdaMethodTree = getMethodFromFunctionalInterface(this.pathToNewClassTree);
        this.localScope = getScopeFromTree(this.pathToNewClassTree);
        this.ownerClass = findFieldOwner();
    }
 
Example 2
Source File: RenameConstructor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public List<Fix> run(CompilationInfo compilationInfo, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
    if (treePath.getLeaf().getKind() == Kind.METHOD) {
        MethodTree mt = (MethodTree) treePath.getLeaf();
        TreePath parentPath = treePath.getParentPath();
        ClassTree ct = (ClassTree) parentPath.getLeaf();
        Trees trees = compilationInfo.getTrees();
        Types types = compilationInfo.getTypes();
        TreeUtilities tu = compilationInfo.getTreeUtilities();
        TypeMirror type = types.erasure(trees.getTypeMirror(treePath));
        if (!Utilities.isValidType(type)) {
            return null;
        }
        for (Tree member : ct.getMembers()) {
            TreePath memberPath = new TreePath(parentPath, member);
            if (member.getKind() == Kind.METHOD && "<init>".contentEquals(((MethodTree)member).getName()) //NOI18N
                    && !tu.isSynthetic(memberPath) && types.isSameType(types.erasure(trees.getTypeMirror(memberPath)), type)) {
                return null;
            }
        }
        RenameConstructorFix fix = new RenameConstructorFix(compilationInfo.getSnapshot().getSource(), TreePathHandle.create(treePath, compilationInfo), offset, mt.getName(), ct.getSimpleName());
        return Collections.<Fix>singletonList(fix);
    }
    return null;
}
 
Example 3
Source File: JavadocCompletionQuery.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void addInnerClasses(TypeElement te, EnumSet<ElementKind> kinds, DeclaredType baseType, Set<? extends Element> toExclude, String prefix, int substitutionOffset, JavadocContext jdctx) {
    CompilationInfo controller = jdctx.javac;
    Element srcEl = jdctx.handle.resolve(controller);
    Elements elements = controller.getElements();
    Types types = controller.getTypes();
    Trees trees = controller.getTrees();
    TreeUtilities tu = controller.getTreeUtilities();
    TreePath docpath = srcEl != null ? trees.getPath(srcEl) : null;
    Scope scope = docpath != null ? trees.getScope(docpath) : tu.scopeFor(caretOffset);
    for (Element e : controller.getElementUtilities().getMembers(te.asType(), null)) {
        if ((e.getKind().isClass() || e.getKind().isInterface()) && (toExclude == null || !toExclude.contains(e))) {
            String name = e.getSimpleName().toString();
                if (Utilities.startsWith(name, prefix) && (Utilities.isShowDeprecatedMembers() || !elements.isDeprecated(e)) && trees.isAccessible(scope, (TypeElement)e) && isOfKindAndType(e.asType(), e, kinds, baseType, scope, trees, types)) {
                    items.add(JavadocCompletionItem.createTypeItem(jdctx.javac, (TypeElement) e, substitutionOffset, null, elements.isDeprecated(e)/*, isOfSmartType(env, e.asType(), smartTypes)*/));
            }
        }
    }
}
 
Example 4
Source File: JavadocCompletionQuery.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void addPackageContent(PackageElement pe, EnumSet<ElementKind> kinds, DeclaredType baseType, Set<? extends Element> toExclude, String prefix, int substitutionOffset, JavadocContext jdctx) {
    CompilationInfo controller = jdctx.javac;
    Element srcEl = jdctx.handle.resolve(controller);
    Elements elements = controller.getElements();
    Types types = controller.getTypes();
    Trees trees = controller.getTrees();
    TreeUtilities tu = controller.getTreeUtilities();
    ElementUtilities eu = controller.getElementUtilities();
    TreePath docpath = srcEl != null ? trees.getPath(srcEl) : null;
    Scope scope = docpath != null ? trees.getScope(docpath) : tu.scopeFor(caretOffset);
    for(Element e : pe.getEnclosedElements()) {
        if ((e.getKind().isClass() || e.getKind().isInterface()) && (toExclude == null || !toExclude.contains(e))) {
            String name = e.getSimpleName().toString();
                if (Utilities.startsWith(name, prefix) && (Utilities.isShowDeprecatedMembers() || !elements.isDeprecated(e))
                    && trees.isAccessible(scope, (TypeElement)e)
                    && isOfKindAndType(e.asType(), e, kinds, baseType, scope, trees, types)
                    && !Utilities.isExcluded(eu.getElementName(e, true))) {
                    items.add(JavadocCompletionItem.createTypeItem(jdctx.javac, (TypeElement) e, substitutionOffset, null, elements.isDeprecated(e)/*, isOfSmartType(env, e.asType(), smartTypes)*/));
            }
        }
    }
}
 
Example 5
Source File: EqualsHashCodeGenerator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static KindOfType detectKind(CompilationInfo info, TypeMirror tm) {
    if (tm.getKind().isPrimitive()) {
        return KindOfType.valueOf(tm.getKind().name());
    }

    if (tm.getKind() == TypeKind.ARRAY) {
        return ((ArrayType) tm).getComponentType().getKind().isPrimitive() ? KindOfType.ARRAY_PRIMITIVE : KindOfType.ARRAY;
    }

    if (tm.getKind() == TypeKind.DECLARED) {
        Types t = info.getTypes();
        TypeElement en = info.getElements().getTypeElement("java.lang.Enum");

        if (en != null) {
            if (t.isSubtype(tm, t.erasure(en.asType()))) {
                return KindOfType.ENUM;
            }
        }

        if (((DeclaredType)tm).asElement().getKind().isClass() && ((TypeElement) ((DeclaredType) tm).asElement()).getQualifiedName().contentEquals("java.lang.String")) {
            return KindOfType.STRING;
        }
    }

    return KindOfType.OTHER;
}
 
Example 6
Source File: AbstractTestGenerator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 */
private static boolean throwsNonRuntimeExceptions(CompilationInfo compInfo,
                                                  ExecutableElement method) {
    List<? extends TypeMirror> thrownTypes = method.getThrownTypes();
    if (thrownTypes.isEmpty()) {
        return false;
    }

    String runtimeExcName = "java.lang.RuntimeException";           //NOI18N
    TypeElement runtimeExcElement = compInfo.getElements()
                                    .getTypeElement(runtimeExcName);
    if (runtimeExcElement == null) {
        Logger.getLogger("testng").log(                              //NOI18N
                Level.WARNING,
                "Could not find TypeElement for "                   //NOI18N
                        + runtimeExcName);
        return true;
    }

    Types types = compInfo.getTypes();
    TypeMirror runtimeExcType = runtimeExcElement.asType();
    for (TypeMirror exceptionType : thrownTypes) {
        if (!types.isSubtype(exceptionType, runtimeExcType)) {
            return true;
        }
    }

    return false;
}
 
Example 7
Source File: AbstractTestGenerator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 */
private static boolean throwsNonRuntimeExceptions(CompilationInfo compInfo,
                                                  ExecutableElement method) {
    List<? extends TypeMirror> thrownTypes = method.getThrownTypes();
    if (thrownTypes.isEmpty()) {
        return false;
    }

    String runtimeExcName = "java.lang.RuntimeException";           //NOI18N
    TypeElement runtimeExcElement = compInfo.getElements()
                                    .getTypeElement(runtimeExcName);
    if (runtimeExcElement == null) {
        Logger.getLogger("junit").log(                              //NOI18N
                Level.WARNING,
                "Could not find TypeElement for "                   //NOI18N
                        + runtimeExcName);
        return true;
    }

    Types types = compInfo.getTypes();
    TypeMirror runtimeExcType = runtimeExcElement.asType();
    for (TypeMirror exceptionType : thrownTypes) {
        if (!types.isSubtype(exceptionType, runtimeExcType)) {
            return true;
        }
    }

    return false;
}
 
Example 8
Source File: Unbalanced.java    From netbeans with Apache License 2.0 3 votes vote down vote up
private static boolean testType(CompilationInfo info, TypeMirror actualType, String superClass) {
    TypeElement juCollection = info.getElements().getTypeElement(superClass);

    if (juCollection == null) return false;

    Types t = info.getTypes();

    return t.isAssignable(t.erasure(actualType), t.erasure(juCollection.asType()));
}