Java Code Examples for javax.lang.model.type.TypeKind#UNION

The following examples show how to use javax.lang.model.type.TypeKind#UNION . 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: TreeConverter.java    From j2objc with Apache License 2.0 6 votes vote down vote up
private Type convertType(TypeMirror varType, SourcePosition pos, boolean isVarargs) {
  Type newType;
  if (isVarargs) {
    newType = Type.newType(((javax.lang.model.type.ArrayType) varType).getComponentType());
  } else {
    if (varType.getKind() == TypeKind.DECLARED
        && !((DeclaredType) varType).getTypeArguments().isEmpty()) {
      newType =
          new ParameterizedType()
              .setType((SimpleType) new SimpleType(varType).setPosition(pos))
              .setTypeMirror(varType);
    } else if (varType.getKind() == TypeKind.UNION) {
      newType = new UnionType();
      newType.setTypeMirror(varType);
      for (TypeMirror t : ((javax.lang.model.type.UnionType) varType).getAlternatives()) {
        Type alternative = convertType(t, pos, false);
        alternative.setPosition(pos);
        ((UnionType) newType).addType(alternative);
      }
    } else {
      newType = Type.newType(varType);
    }
  }
  return (Type) newType.setPosition(pos);
}
 
Example 2
Source File: JavacTrees.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public TypeMirror getLub(CatchTree tree) {
    JCCatch ct = (JCCatch) tree;
    JCVariableDecl v = ct.param;
    if (v.type != null && v.type.getKind() == TypeKind.UNION) {
        UnionClassType ut = (UnionClassType) v.type;
        return ut.getLub();
    } else {
        return v.type;
    }
}
 
Example 3
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 4
Source File: JavacTrees.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public TypeMirror getLub(CatchTree tree) {
    JCCatch ct = (JCCatch) tree;
    JCVariableDecl v = ct.param;
    if (v.type != null && v.type.getKind() == TypeKind.UNION) {
        UnionClassType ut = (UnionClassType) v.type;
        return ut.getLub();
    } else {
        return v.type;
    }
}
 
Example 5
Source File: JavacTrees.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public TypeMirror getLub(CatchTree tree) {
    JCCatch ct = (JCCatch) tree;
    JCVariableDecl v = ct.param;
    if (v.type != null && v.type.getKind() == TypeKind.UNION) {
        UnionClassType ut = (UnionClassType) v.type;
        return ut.getLub();
    } else {
        return v.type;
    }
}
 
Example 6
Source File: JavacTrees.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
public TypeMirror getLub(CatchTree tree) {
    JCCatch ct = (JCCatch) tree;
    JCVariableDecl v = ct.param;
    if (v.type != null && v.type.getKind() == TypeKind.UNION) {
        UnionClassType ut = (UnionClassType) v.type;
        return ut.getLub();
    } else {
        return v.type;
    }
}
 
Example 7
Source File: JavacTrees.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override @DefinedBy(Api.COMPILER_TREE)
public TypeMirror getLub(CatchTree tree) {
    JCCatch ct = (JCCatch) tree;
    JCVariableDecl v = ct.param;
    if (v.type != null && v.type.getKind() == TypeKind.UNION) {
        UnionClassType ut = (UnionClassType) v.type;
        return ut.getLub();
    } else {
        return v.type;
    }
}
 
Example 8
Source File: JavacTrees.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public TypeMirror getLub(CatchTree tree) {
    JCCatch ct = (JCCatch) tree;
    JCVariableDecl v = ct.param;
    if (v.type != null && v.type.getKind() == TypeKind.UNION) {
        UnionClassType ut = (UnionClassType) v.type;
        return ut.getLub();
    } else {
        return v.type;
    }
}
 
Example 9
Source File: JavacTrees.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public TypeMirror getLub(CatchTree tree) {
    JCCatch ct = (JCCatch) tree;
    JCVariableDecl v = ct.param;
    if (v.type != null && v.type.getKind() == TypeKind.UNION) {
        UnionClassType ut = (UnionClassType) v.type;
        return ut.getLub();
    } else {
        return v.type;
    }
}
 
Example 10
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 11
Source File: JavacTrees.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override @DefinedBy(Api.COMPILER_TREE)
public TypeMirror getLub(CatchTree tree) {
    JCCatch ct = (JCCatch) tree;
    JCVariableDecl v = ct.param;
    if (v.type != null && v.type.getKind() == TypeKind.UNION) {
        UnionClassType ut = (UnionClassType) v.type;
        return ut.getLub();
    } else {
        return v.type;
    }
}
 
Example 12
Source File: JavacTrees.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public TypeMirror getLub(CatchTree tree) {
    JCCatch ct = (JCCatch) tree;
    JCVariableDecl v = ct.param;
    if (v.type != null && v.type.getKind() == TypeKind.UNION) {
        UnionClassType ut = (UnionClassType) v.type;
        return ut.getLub();
    } else {
        return v.type;
    }
}
 
Example 13
Source File: JavacTrees.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
@Override
public TypeMirror getLub(CatchTree tree) {
    JCCatch ct = (JCCatch) tree;
    JCVariableDecl v = ct.param;
    if (v.type != null && v.type.getKind() == TypeKind.UNION) {
        UnionClassType ut = (UnionClassType) v.type;
        return ut.getLub();
    } else {
        return v.type;
    }
}
 
Example 14
Source File: JavacTrees.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public TypeMirror getLub(CatchTree tree) {
    JCCatch ct = (JCCatch) tree;
    JCVariableDecl v = ct.param;
    if (v.type != null && v.type.getKind() == TypeKind.UNION) {
        UnionClassType ut = (UnionClassType) v.type;
        return ut.getLub();
    } else {
        return v.type;
    }
}
 
Example 15
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 16
Source File: Type.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public TypeKind getKind() {
    return TypeKind.UNION;
}
 
Example 17
Source File: JavaEnvironment.java    From j2cl with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a type descriptor for the given TypeMirror, taking into account nullability.
 *
 * @param typeMirror the type provided by javac, used to create the type descriptor.
 * @param elementAnnotations the annotations on the element
 */
private TypeDescriptor createTypeDescriptorWithNullability(
    TypeMirror typeMirror, List<? extends AnnotationMirror> elementAnnotations) {
  if (typeMirror == null || typeMirror.getKind() == TypeKind.NONE) {
    return null;
  }

  if (typeMirror.getKind().isPrimitive() || typeMirror.getKind() == TypeKind.VOID) {
    return PrimitiveTypes.get(asElement(typeMirror).getSimpleName().toString());
  }

  if (typeMirror.getKind() == TypeKind.INTERSECTION) {
    return createIntersectionType((IntersectionClassType) typeMirror);
  }

  if (typeMirror.getKind() == TypeKind.UNION) {
    return createUnionType((UnionClassType) typeMirror);
  }

  if (typeMirror.getKind() == TypeKind.NULL) {
    return TypeDescriptors.get().javaLangObject;
  }

  if (typeMirror.getKind() == TypeKind.TYPEVAR) {
    return createTypeVariable((javax.lang.model.type.TypeVariable) typeMirror);
  }

  if (typeMirror.getKind() == TypeKind.WILDCARD) {
    return createWildcardTypeVariable(
        ((javax.lang.model.type.WildcardType) typeMirror).getExtendsBound());
  }

  boolean isNullable = isNullable(typeMirror, elementAnnotations);
  if (typeMirror.getKind() == TypeKind.ARRAY) {
    ArrayType arrayType = (ArrayType) typeMirror;
    TypeDescriptor componentTypeDescriptor = createTypeDescriptor(arrayType.getComponentType());
    return ArrayTypeDescriptor.newBuilder()
        .setComponentTypeDescriptor(componentTypeDescriptor)
        .setNullable(isNullable)
        .build();
  }

  return withNullability(createDeclaredType((ClassType) typeMirror), isNullable);
}
 
Example 18
Source File: Type.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
@Override
public TypeKind getKind() {
    return TypeKind.UNION;
}