javax.lang.model.type.UnionType Java Examples

The following examples show how to use javax.lang.model.type.UnionType. 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: Util.java    From revapi with Apache License 2.0 5 votes vote down vote up
@Override
public final T visitUnion(UnionType t, StringBuilderAndState<S> st) {
    try {
        st.depth++;
        return doVisitUnion(t, st);
    } finally {
        st.depth--;
    }
}
 
Example #2
Source File: TypeVariables.java    From auto with Apache License 2.0 5 votes vote down vote up
@Override
public ImmutableSet<TypeVariable> visitUnion(
    UnionType t, Set<Element> visited) {
  ImmutableSet.Builder<TypeVariable> typeVariables = ImmutableSet.builder();
  for (TypeMirror unionType : t.getAlternatives()) {
    typeVariables.addAll(unionType.accept(this, visited));
  }
  return typeVariables.build();
}
 
Example #3
Source File: StandaloneTypeMirror.java    From buck with Apache License 2.0 5 votes vote down vote up
@Override
public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  switch (kind) {
    case BOOLEAN:
    case BYTE:
    case SHORT:
    case INT:
    case LONG:
    case CHAR:
    case FLOAT:
    case DOUBLE:
      return v.visitPrimitive((PrimitiveType) this, p);
    case PACKAGE:
    case VOID:
    case NONE:
      return v.visitNoType((NoType) this, p);
    case NULL:
      return v.visitNull((NullType) this, p);
    case ARRAY:
      return v.visitArray((ArrayType) this, p);
    case DECLARED:
      return v.visitDeclared((DeclaredType) this, p);
    case ERROR:
      return v.visitError((ErrorType) this, p);
    case TYPEVAR:
      return v.visitTypeVariable((TypeVariable) this, p);
    case WILDCARD:
      return v.visitWildcard((WildcardType) this, p);
    case EXECUTABLE:
      return v.visitExecutable((ExecutableType) this, p);
    case OTHER:
      return v.visit(this, p);
    case UNION:
      return v.visitUnion((UnionType) this, p);
    case INTERSECTION:
      return v.visitIntersection((IntersectionType) this, p);
    default:
      throw new AssertionError(String.format("Unknown TypeKind: %s", kind));
  }
}
 
Example #4
Source File: TreeUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public Void visitThrow(ThrowTree node, Set<TypeMirror> p) {
    super.visitThrow(node, p);
    TypeMirror tm = info.getTrees().getTypeMirror(new TreePath(getCurrentPath(), node.getExpression()));
    if (tm != null) {
        if (tm.getKind() == TypeKind.DECLARED)
            p.add(tm);
        else if (tm.getKind() == TypeKind.UNION)
            p.addAll(((UnionType)tm).getAlternatives());
    }
    return null;
}
 
Example #5
Source File: TypeUtil.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Maps the given type to it's Objective-C equivalent. Array types are mapped to their equivalent
 * IOSArray type and common Java classes like String and Object are mapped to NSString and
 * NSObject.
 */
public TypeElement getObjcClass(TypeMirror t) {
  if (isArray(t)) {
    return getIosArray(((ArrayType) t).getComponentType());
  } else if (isDeclaredType(t)) {
    return getObjcClass((TypeElement) ((DeclaredType) t).asElement());
  } else if (t.getKind() == TypeKind.UNION) {
    TypeMirror lub = leastUpperBound(((UnionType)t).getAlternatives());
    return getObjcClass(asTypeElement(lub));
  }
  return null;
}
 
Example #6
Source File: ExportNonAccessibleElement.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Boolean visitUnion(UnionType tm, Void p) {
    for (TypeMirror t : tm.getAlternatives()) {
        if (stop) {
            return false;
        }
        
        if (t.accept(this, p)) {
            return true;
        }
    }
    return false;
}
 
Example #7
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static List<? extends TypeMirror> getCaughtExceptions(TypeMirror caught) {
    if (caught == null) {
        return Collections.emptyList();
    }
    switch (caught.getKind()) {
        case UNION: {
            boolean cloned = false;
            List<? extends TypeMirror> types = ((UnionType) caught).getAlternatives();
            int i = types.size() - 1;
            for (; i >= 0; i--) {
                TypeMirror m = types.get(i);
                TypeKind mk = m.getKind();
                if (mk == null || mk != TypeKind.DECLARED) {
                    if (!cloned) {
                        types = new ArrayList<TypeMirror>(types);
                    }
                    types.remove(i);
                }
            }
            
            return types;
        }
        case DECLARED:
            return Collections.singletonList(caught);
        default:
            return Collections.emptyList();
    }
}
 
Example #8
Source File: JoinCatches.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected void performRewrite(TransformationContext ctx) {
    WorkingCopy wc = ctx.getWorkingCopy();
    TreePath tp = ctx.getPath();
    List<Tree> disjointTypes = new LinkedList<Tree>();
    TryTree tt = (TryTree) tp.getLeaf();
    int first = duplicates.get(0);
    List<Integer> remainingDuplicates = duplicates.subList(1, duplicates.size());
    
    addDisjointType(disjointTypes, tt.getCatches().get(first).getParameter().getType());

    for (Integer d : remainingDuplicates) {
        addDisjointType(disjointTypes, tt.getCatches().get((int) d).getParameter().getType());
    }

    List<CatchTree> newCatches = new LinkedList<CatchTree>();
    int c = 0;

    for (CatchTree ct : tt.getCatches()) {
        if (c == first) {
            wc.rewrite(ct.getParameter().getType(), wc.getTreeMaker().UnionType(disjointTypes));
        }
        
        if (remainingDuplicates.contains(c++)) continue;

        newCatches.add(ct);
    }

    TryTree nue = wc.getTreeMaker().Try(tt.getResources(), tt.getBlock(), newCatches, tt.getFinallyBlock());

    wc.rewrite(tt, nue);
}
 
Example #9
Source File: DeclaredTypeCollector.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Void visitUnion(UnionType t, Collection<DeclaredType> p) {
    for (TypeMirror tm : t.getAlternatives()) {
        visit(tm, p);
    }
    return DEFAULT_VALUE;
}
 
Example #10
Source File: ExpectedTypeResolver.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Computes possible types for throw expression. Throw can safely throw an exception, which is
 * either declared by method as a thrown type, or catched within method, by an upper try-catch block.
 * Unchecked exceptions are permitted (derivatives of RuntimeException or Error).
 */
@Override
public List<? extends TypeMirror> visitThrow(ThrowTree node, Object p) {
    List<TypeMirror> result = new ArrayList<TypeMirror>();
    TreePath parents = getCurrentPath();
    Tree prev = null;
    while (parents != null && parents.getLeaf().getKind() != Tree.Kind.METHOD) {
        Tree l = parents.getLeaf();
        if (l.getKind() == Tree.Kind.TRY) {
            TryTree tt = (TryTree) l;
            if (prev == tt.getBlock()) {
                for (CatchTree ct : tt.getCatches()) {
                    TypeMirror ex = info.getTrees().getTypeMirror(new TreePath(getCurrentPath(), ct.getParameter().getType()));
                    if (ex != null) {
                        switch (ex.getKind()) {
                            case DECLARED:
                                if (!result.contains(ex)) {
                                    result.add(ex);
                                }
                                break;
                            case UNION:
                                for (TypeMirror t : ((UnionType) ex).getAlternatives()) {
                                    if (!result.contains(t)) {
                                        result.add(t);
                                    }
                                }
                                break;
                        }
                    }
                }
            }
        }
        prev = l;
        parents = parents.getParentPath();
    }
    if (parents != null) {
        MethodTree mt = (MethodTree) parents.getLeaf();
        for (ExpressionTree etree : mt.getThrows()) {
            TypeMirror m = info.getTrees().getTypeMirror(new TreePath(parents, etree));
            if (m != null && !result.contains(m)) {
                result.add(m);
            }
        }
    }
    TypeMirror jlre = info.getElements().getTypeElement("java.lang.RuntimeException").asType(); // NOI18N
    TypeMirror jler = info.getElements().getTypeElement("java.lang.Error").asType(); // NOI18N
    for (TypeMirror em : result) {
        if (jlre != null && info.getTypes().isAssignable(jlre, em)) {
            jlre = null;
        }
        if (jler != null && info.getTypes().isAssignable(jler, em)) {
            jler = null;
        }
        if (jlre == null && jler == null) {
            break;
        }
    }
    if (jlre != null) {
        result.add(jlre);
    }
    if (jler != null) {
        result.add(jler);
    }
    return result;
}
 
Example #11
Source File: IsInvalidTypeVisitor.java    From FreeBuilder with Apache License 2.0 4 votes vote down vote up
@Override
public Boolean visitUnion(UnionType t, Void p) {
  return any(t.getAlternatives(), this);
}
 
Example #12
Source File: UncaughtException.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private List<? extends TypeMirror> findUncaughtExceptions(CompilationInfo info, TreePath path, List<? extends TypeMirror> exceptions) {
    List<TypeMirror> result = new ArrayList<TypeMirror>();
    
    result.addAll(exceptions);
    
    Tree lastTree = null;
    
    while (path != null) {
        Tree currentTree = path.getLeaf();

        if (currentTree.getKind() == Tree.Kind.METHOD) {
            TypeMirror tm = info.getTrees().getTypeMirror(path);
            if (tm != null && tm.getKind() == TypeKind.EXECUTABLE) {
                for (TypeMirror mirr : ((ExecutableType) tm).getThrownTypes()) {
                    for (Iterator<TypeMirror> it = result.iterator(); it.hasNext();)
                        if (info.getTypes().isSameType(it.next(), mirr))
                            it.remove();
                }
                break;
            }
        }            
        
        if (currentTree.getKind() == Tree.Kind.LAMBDA_EXPRESSION) {
            // no checked exceptions can be thrown out of Lambda, #243106
            break;
        }
        
        if (currentTree.getKind() == Kind.TRY) {
            TryTree tt = (TryTree) currentTree;
            
            if (tt.getBlock() == lastTree) {
                for (CatchTree c : tt.getCatches()) {
                    TreePath catchPath = new TreePath(new TreePath(path, c), c.getParameter());
                    VariableElement variable = (VariableElement) info.getTrees().getElement(catchPath);
                    if (variable == null) {
                        continue;
                    }
                    TypeMirror variableType = variable.asType();
                    if (variableType.getKind() == TypeKind.UNION) {
                        result.removeAll(((UnionType)variableType).getAlternatives());
                    } else {
                        result.remove(variableType);
                    }
                }
            }
        }
        
        lastTree = path.getLeaf();
        path = path.getParentPath();
    }
    
    List<TypeMirror> filtered = new ArrayList<>();
    
    OUTER: for (Iterator<TypeMirror> sourceIt = result.iterator(); sourceIt.hasNext(); ) {
        TypeMirror sourceType = sourceIt.next();
        
        for (Iterator<TypeMirror> filteredIt = filtered.iterator(); filteredIt.hasNext(); ) {
            TypeMirror filteredType = filteredIt.next();
            
            if (info.getTypes().isSubtype(sourceType, filteredType)) {
                sourceIt.remove();
                continue OUTER;
            }
            
            if (info.getTypes().isSubtype(filteredType, sourceType)) {
                filteredIt.remove();
                break;
            }
        }
        
        filtered.add(sourceType);
    }
    
    return filtered;
}
 
Example #13
Source File: Util.java    From revapi with Apache License 2.0 4 votes vote down vote up
protected T doVisitUnion(UnionType t, StringBuilderAndState<S> st) {
    return defaultAction(t, st);
}
 
Example #14
Source File: TypeMirrorPairVisitor.java    From revapi with Apache License 2.0 4 votes vote down vote up
@Override
public final R visitUnion(UnionType type, TypeMirror otherType) {
    return otherType instanceof UnionType ? visitUnion(type, (UnionType) otherType) :
        unmatchedAction(type, otherType);
}
 
Example #15
Source File: TypeMirrorPairVisitor.java    From revapi with Apache License 2.0 4 votes vote down vote up
protected R visitUnion(UnionType type, UnionType otherType) {
    return defaultMatchAction(type, otherType);
}
 
Example #16
Source File: TypeScanner8.java    From buck with Apache License 2.0 4 votes vote down vote up
@Override
@Nullable
public R visitUnion(UnionType t, P p) {
  throw new UnsupportedOperationException("NYI");
}
 
Example #17
Source File: StandaloneTypeMirror.java    From buck with Apache License 2.0 4 votes vote down vote up
@Override
public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  switch (kind) {
    case BOOLEAN:
    case BYTE:
    case SHORT:
    case INT:
    case LONG:
    case CHAR:
    case FLOAT:
    case DOUBLE:
      return v.visitPrimitive((PrimitiveType) this, p);
    case PACKAGE:
    case MODULE:
    case VOID:
    case NONE:
      return v.visitNoType((NoType) this, p);
    case NULL:
      return v.visitNull((NullType) this, p);
    case ARRAY:
      return v.visitArray((ArrayType) this, p);
    case DECLARED:
      return v.visitDeclared((DeclaredType) this, p);
    case ERROR:
      return v.visitError((ErrorType) this, p);
    case TYPEVAR:
      return v.visitTypeVariable((TypeVariable) this, p);
    case WILDCARD:
      return v.visitWildcard((WildcardType) this, p);
    case EXECUTABLE:
      return v.visitExecutable((ExecutableType) this, p);
    case OTHER:
      return v.visit(this, p);
    case UNION:
      return v.visitUnion((UnionType) this, p);
    case INTERSECTION:
      return v.visitIntersection((IntersectionType) this, p);
    default:
      throw new AssertionError(String.format("Unknown TypeKind: %s", kind));
  }
}
 
Example #18
Source File: TypeScanner8Test.java    From buck with Apache License 2.0 4 votes vote down vote up
@Override
public Void visitUnion(UnionType t, Void aVoid) {
  scans.add(t.toString());
  return super.visitUnion(t, aVoid);
}
 
Example #19
Source File: TypeExtractor.java    From immutables with Apache License 2.0 4 votes vote down vote up
@Override
public Type visitUnion(UnionType t, Type.Parameters p) {
  throw new UnsupportedOperationException("UnionType type not supported");
}