Java Code Examples for javax.lang.model.type.DeclaredType#getEnclosingType()

The following examples show how to use javax.lang.model.type.DeclaredType#getEnclosingType() . 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: MoveMembersTransformer.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean enclosedBy(TypeMirror t1, TypeMirror t2) {
    if(t1.getKind() == TypeKind.DECLARED) {
        if(workingCopy.getTypes().isSameType(t1, t2)) {
            return true;
        }
        DeclaredType dt = (DeclaredType) t1;
        TypeMirror enclosingType = dt.getEnclosingType();
        if(enclosingType.getKind() == TypeKind.NONE) {
            return false;
        } else {
            return enclosedBy(enclosingType, t2);
        }
    } else {
        return false;
    }
}
 
Example 2
Source File: ExpectedTypeResolver.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public List<? extends TypeMirror> visitNewClass(NewClassTree node, Object p) {
    TypeMirror tm = info.getTrees().getTypeMirror(getCurrentPath());
    if (tm == null || tm.getKind() != TypeKind.DECLARED) {
        return null;
    }
    Element el = info.getTrees().getElement(getCurrentPath());
    if (el == null) {
        return null;
    }
    if (theExpression.getLeaf() != node.getEnclosingExpression()) {
        ExecutableType execType = (ExecutableType)info.getTypes().asMemberOf((DeclaredType)tm, el);
        return visitMethodOrNew(node, p, node.getArguments(), execType);
    } else {
        DeclaredType dt = (DeclaredType)tm;
        if (dt.getEnclosingType() == null) {
            return null;
        }
        return Collections.singletonList(dt.getEnclosingType());
    }
}
 
Example 3
Source File: ControllerGenerator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public TypeMirror visitDeclared(DeclaredType t, CompilationInfo p) {
    if (t.getTypeArguments().isEmpty()) {
        return t;
    }
    List<TypeMirror> newArgs = new ArrayList<TypeMirror>(t.getTypeArguments().size());
    for (TypeMirror tm : t.getTypeArguments()) {
        newArgs.add(visit(tm, p));
    }
    
    TypeMirror enclosing = t.getEnclosingType();
    if (enclosing != null) {
        enclosing = visit(enclosing, p);
    }
    
    return p.getTypes().getDeclaredType(
        (DeclaredType)enclosing,
        (TypeElement)t.asElement(), 
        newArgs.toArray(new TypeMirror[newArgs.size()]));
}
 
Example 4
Source File: JavaEnvironment.java    From j2cl with Apache License 2.0 6 votes vote down vote up
private static List<TypeMirror> getTypeArguments(DeclaredType declaredType) {
  List<TypeMirror> typeArguments = new ArrayList<>();
  DeclaredType currentType = declaredType;
  do {
    typeArguments.addAll(currentType.getTypeArguments());
    Element enclosingElement = currentType.asElement().getEnclosingElement();
    if (enclosingElement.getKind() == ElementKind.METHOD
        || enclosingElement.getKind() == ElementKind.CONSTRUCTOR) {
      typeArguments.addAll(
          ((Parameterizable) enclosingElement)
              .getTypeParameters().stream().map(Element::asType).collect(toImmutableList()));
    }
    currentType =
        currentType.getEnclosingType() instanceof DeclaredType
            ? (DeclaredType) currentType.getEnclosingType()
            : null;
  } while (currentType != null);
  return typeArguments;
}
 
Example 5
Source File: GraphBuilder.java    From j2objc with Apache License 2.0 6 votes vote down vote up
private void followEnclosingType(DeclaredType type, TypeNode typeNode) {
  TypeMirror enclosingType = type.getEnclosingType();
  if (TypeUtil.isNone(enclosingType)) {
    return;
  }
  TypeNode enclosingTypeNode = getOrCreateNode(enclosingType);
  TypeElement element = (TypeElement) type.asElement();
  TypeNode declarationType = getOrCreateNode(element.asType());
  if (declarationType != null && enclosingTypeNode != null
      && ElementUtil.hasOuterContext(element)
      && !isWeakOuterType(element)
      && !whitelist.containsType(enclosingTypeNode)
      && !whitelist.hasOuterForType(typeNode)) {
    possibleOuterEdges.put(
        declarationType, Edge.newOuterClassEdge(typeNode, enclosingTypeNode));
  }
}
 
Example 6
Source File: TreeBackedTypes.java    From buck with Apache License 2.0 6 votes vote down vote up
private TypeMirror erasure(StandaloneTypeMirror t) {
  switch (t.getKind()) {
    case ARRAY:
      {
        ArrayType arrayType = (ArrayType) t;
        return getArrayType(erasure(arrayType.getComponentType()));
      }
    case DECLARED:
      {
        DeclaredType declaredType = (DeclaredType) t;
        TypeElement typeElement = (TypeElement) declaredType.asElement();
        if (declaredType.getEnclosingType().getKind() == TypeKind.DECLARED) {
          DeclaredType enclosingType = (DeclaredType) declaredType.getEnclosingType();

          return getDeclaredType((DeclaredType) erasure(enclosingType), typeElement);
        }

        return getDeclaredType(typeElement);
      }
      // $CASES-OMITTED$
    default:
      throw new UnsupportedOperationException();
  }
}
 
Example 7
Source File: TreeBackedTypeElementTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testAsType() throws IOException {
  compile("class Foo { }");

  TypeElement fooElement = elements.getTypeElement("Foo");
  TypeMirror fooTypeMirror = fooElement.asType();

  assertEquals(TypeKind.DECLARED, fooTypeMirror.getKind());
  DeclaredType fooDeclaredType = (DeclaredType) fooTypeMirror;
  assertSame(fooElement, fooDeclaredType.asElement());
  assertEquals(0, fooDeclaredType.getTypeArguments().size());

  TypeMirror enclosingType = fooDeclaredType.getEnclosingType();
  assertEquals(TypeKind.NONE, enclosingType.getKind());
  assertTrue(enclosingType instanceof NoType);
}
 
Example 8
Source File: TreeBackedTypeElementTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testAsTypeGeneric() throws IOException {
  compile("class Foo<T> { }");

  TypeElement fooElement = elements.getTypeElement("Foo");
  TypeMirror fooTypeMirror = fooElement.asType();

  assertEquals(TypeKind.DECLARED, fooTypeMirror.getKind());
  DeclaredType fooDeclaredType = (DeclaredType) fooTypeMirror;
  assertSame(fooElement, fooDeclaredType.asElement());
  List<? extends TypeMirror> typeArguments = fooDeclaredType.getTypeArguments();
  assertEquals("T", ((TypeVariable) typeArguments.get(0)).asElement().getSimpleName().toString());
  assertEquals(1, typeArguments.size());

  TypeMirror enclosingType = fooDeclaredType.getEnclosingType();
  assertEquals(TypeKind.NONE, enclosingType.getKind());
  assertTrue(enclosingType instanceof NoType);
}
 
Example 9
Source File: TreeBackedTypesTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetDeclaredTypeTopLevelNoGenerics() throws IOException {
  compile("class Foo { }");

  TypeElement fooElement = elements.getTypeElement("Foo");
  DeclaredType fooTypeMirror = types.getDeclaredType(fooElement);

  assertEquals(TypeKind.DECLARED, fooTypeMirror.getKind());
  DeclaredType fooDeclaredType = fooTypeMirror;
  assertNotSame(fooElement.asType(), fooDeclaredType);
  assertSame(fooElement, fooDeclaredType.asElement());
  assertEquals(0, fooDeclaredType.getTypeArguments().size());

  TypeMirror enclosingType = fooDeclaredType.getEnclosingType();
  assertEquals(TypeKind.NONE, enclosingType.getKind());
  assertTrue(enclosingType instanceof NoType);
}
 
Example 10
Source File: TreeBackedTypesTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetDeclaredTypeTopLevelRawType() throws IOException {
  compile("class Foo<T> { }");

  TypeElement fooElement = elements.getTypeElement("Foo");
  DeclaredType fooTypeMirror = types.getDeclaredType(fooElement);

  assertEquals(TypeKind.DECLARED, fooTypeMirror.getKind());
  DeclaredType fooDeclaredType = fooTypeMirror;
  assertNotSame(fooElement.asType(), fooDeclaredType);
  assertSame(fooElement, fooDeclaredType.asElement());
  assertEquals(0, fooDeclaredType.getTypeArguments().size());

  TypeMirror enclosingType = fooDeclaredType.getEnclosingType();
  assertEquals(TypeKind.NONE, enclosingType.getKind());
  assertTrue(enclosingType instanceof NoType);
}
 
Example 11
Source File: EclipseHack.java    From auto with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the enclosing type of {@code type}, if {@code type} is an inner class. Otherwise
 * returns a {@code NoType}. This is what {@link DeclaredType#getEnclosingType()} is supposed to
 * do. However, some versions of Eclipse have a bug where, for example, asking for the enclosing
 * type of {@code PrimitiveIterator.OfInt} will return {@code PrimitiveIterator<T, T_CONS>} rather
 * than plain {@code PrimitiveIterator}, as if {@code OfInt} were an inner class rather than a
 * static one. This would lead us to write a reference to {@code OfInt} as {@code
 * PrimitiveIterator<T, T_CONS>.OfInt}, which would obviously break. We attempt to avert this by
 * detecting that:
 *
 * <ul>
 *   <li>there is an enclosing type that is a {@code DeclaredType}, which should mean that {@code
 *       type} is an inner class;
 *   <li>we are in the Eclipse compiler;
 *   <li>the type arguments of the purported enclosing type are all type variables with the same
 *       names as the corresponding type parameters.
 * </ul>
 *
 * <p>If all these conditions are met, we assume we're hitting the Eclipse bug, and we return no
 * enclosing type instead. That does mean that in the unlikely event where we really do have an
 * inner class of an instantiation of the outer class with type arguments that happen to be type
 * variables with the same names as the corresponding parameters, we will do the wrong thing on
 * Eclipse. But doing the wrong thing in that case is better than doing the wrong thing in the
 * usual case.
 */
static TypeMirror getEnclosingType(DeclaredType type) {
  TypeMirror enclosing = type.getEnclosingType();
  if (!enclosing.getKind().equals(TypeKind.DECLARED)
      || !enclosing.getClass().getName().contains("eclipse")) {
    // If the class representing the enclosing type comes from the Eclipse compiler, it will be
    // something like org.eclipse.jdt.internal.compiler.apt.model.DeclaredTypeImpl. If we're not
    // in the Eclipse compiler then we don't expect to see "eclipse" in the name of this
    // implementation class.
    return enclosing;
  }
  DeclaredType declared = MoreTypes.asDeclared(enclosing);
  List<? extends TypeMirror> arguments = declared.getTypeArguments();
  if (!arguments.isEmpty()) {
    boolean allVariables = arguments.stream().allMatch(t -> t.getKind().equals(TypeKind.TYPEVAR));
    if (allVariables) {
      List<Name> argumentNames =
          arguments.stream()
              .map(t -> MoreTypes.asTypeVariable(t).asElement().getSimpleName())
              .collect(toList());
      TypeElement enclosingElement = MoreTypes.asTypeElement(declared);
      List<Name> parameterNames =
          enclosingElement.getTypeParameters().stream()
              .map(Element::getSimpleName)
              .collect(toList());
      if (argumentNames.equals(parameterNames)) {
        // We're going to return a NoType. We don't have a Types to hand so we can't call
        // Types.getNoType(). Instead, just keep going through outer types until we get to
        // the outside, which will be a NoType.
        while (enclosing.getKind().equals(TypeKind.DECLARED)) {
          enclosing = MoreTypes.asDeclared(enclosing).getEnclosingType();
        }
        return enclosing;
      }
    }
  }
  return declared;
}
 
Example 12
Source File: MoreTypes.java    From auto with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the type of the innermost enclosing instance, or null if there is none. This is the
 * same as {@link DeclaredType#getEnclosingType()} except that it returns null rather than
 * NoType for a static type. We need this because of
 * <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=508222">this bug</a> whereby
 * the Eclipse compiler returns a value for static classes that is not NoType.
 */
private static TypeMirror enclosingType(DeclaredType t) {
  TypeMirror enclosing = t.getEnclosingType();
  if (enclosing.getKind().equals(TypeKind.NONE)
      || t.asElement().getModifiers().contains(Modifier.STATIC)) {
    return null;
  }
  return enclosing;
}
 
Example 13
Source File: TypeUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Note: may return {@code null}, if an intersection type is encountered, to indicate a 
 * real type cannot be created.
 */
private static TypeMirror resolveCapturedTypeInt(CompilationInfo info, TypeMirror tm) {
    if (tm == null) return tm;
    
    TypeMirror orig = SourceUtils.resolveCapturedType(tm);

    if (orig != null) {
        tm = orig;
    }
    
    if (tm.getKind() == TypeKind.WILDCARD) {
        TypeMirror extendsBound = ((WildcardType) tm).getExtendsBound();
        TypeMirror superBound = ((WildcardType) tm).getSuperBound();
        if (extendsBound != null || superBound != null) {
            TypeMirror rct = resolveCapturedTypeInt(info, extendsBound != null ? extendsBound : superBound);
            if (rct != null) {
                switch (rct.getKind()) {
                    case WILDCARD:
                        return rct;
                    case ARRAY:
                    case DECLARED:
                    case ERROR:
                    case TYPEVAR:
                    case OTHER:
                        return info.getTypes().getWildcardType(
                                extendsBound != null ? rct : null, superBound != null ? rct : null);
                }
            } else {
                // propagate failure out of all wildcards
                return null;
            }
        }
    } else if (tm.getKind() == TypeKind.INTERSECTION) {
        return null;
    }
    
    if (tm.getKind() == TypeKind.DECLARED) {
        DeclaredType dt = (DeclaredType) tm;
        List<TypeMirror> typeArguments = new LinkedList<TypeMirror>();
        
        for (TypeMirror t : dt.getTypeArguments()) {
            TypeMirror targ = resolveCapturedTypeInt(info, t);
            if (targ == null) {
                // bail out, if the type parameter is a wildcard, it's probably not possible
                // to create a proper parametrized type from it
                if (t.getKind() == TypeKind.WILDCARD || t.getKind() == TypeKind.INTERSECTION) {
                    return null;
                }
                // use rawtype
                typeArguments.clear();
                break;
            }
            typeArguments.add(targ);
        }
        
        final TypeMirror enclosingType = dt.getEnclosingType();
        if (enclosingType.getKind() == TypeKind.DECLARED) {
            return info.getTypes().getDeclaredType((DeclaredType) enclosingType, (TypeElement) dt.asElement(), typeArguments.toArray(new TypeMirror[0]));
        } else {
            if (dt.asElement() == null) return dt;
            return info.getTypes().getDeclaredType((TypeElement) dt.asElement(), typeArguments.toArray(new TypeMirror[0]));
        }
    }

    if (tm.getKind() == TypeKind.ARRAY) {
        ArrayType at = (ArrayType) tm;
        TypeMirror tm2 = resolveCapturedTypeInt(info, at.getComponentType());
        return info.getTypes().getArrayType(tm2 != null ? tm2 : tm);
    }
    
    return tm;
}
 
Example 14
Source File: SourceUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static TypeMirror resolveCapturedTypeInt(CompilationInfo info, TypeMirror tm) {
    if (tm == null) {
        return tm;
    }
    
    TypeMirror orig = resolveCapturedType(tm);

    if (orig != null) {
        tm = orig;
    }
    
    if (tm.getKind() == TypeKind.WILDCARD) {
        TypeMirror extendsBound = ((WildcardType) tm).getExtendsBound();
        TypeMirror rct = resolveCapturedTypeInt(info, extendsBound != null ? extendsBound : ((WildcardType) tm).getSuperBound());
        if (rct != null) {
            return rct.getKind() == TypeKind.WILDCARD ? rct : info.getTypes().getWildcardType(extendsBound != null ? rct : null, extendsBound == null ? rct : null);
        }
    }
    
    if (tm.getKind() == TypeKind.DECLARED) {
        DeclaredType dt = (DeclaredType) tm;
        TypeElement el = (TypeElement) dt.asElement();
        if (((DeclaredType)el.asType()).getTypeArguments().size() != dt.getTypeArguments().size()) {
            return info.getTypes().getDeclaredType(el);
        }
        
        List<TypeMirror> typeArguments = new LinkedList<>();
        
        for (TypeMirror t : dt.getTypeArguments()) {
            typeArguments.add(resolveCapturedTypeInt(info, t));
        }
        
        final TypeMirror enclosingType = dt.getEnclosingType();
        if (enclosingType.getKind() == TypeKind.DECLARED) {
            return info.getTypes().getDeclaredType((DeclaredType) enclosingType, el, typeArguments.toArray(new TypeMirror[0]));
        } else {
            return info.getTypes().getDeclaredType(el, typeArguments.toArray(new TypeMirror[0]));
        }
    }

    if (tm.getKind() == TypeKind.ARRAY) {
        ArrayType at = (ArrayType) tm;
        TypeMirror componentType = resolveCapturedTypeInt(info, at.getComponentType());
        switch (componentType.getKind()) {
            case VOID:
            case EXECUTABLE:
            case WILDCARD:  // heh!
            case PACKAGE:
                break;
            default:
                return info.getTypes().getArrayType(componentType);
        }
    }
    
    return tm;
}
 
Example 15
Source File: Utilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Note: may return {@code null}, if an intersection type is encountered, to indicate a 
 * real type cannot be created.
 */
private static TypeMirror resolveCapturedTypeInt(CompilationInfo info, TypeMirror tm) {
    if (tm == null) return tm;
    
    TypeMirror orig = SourceUtils.resolveCapturedType(tm);

    if (orig != null) {
        tm = orig;
    }
    
    if (tm.getKind() == TypeKind.WILDCARD) {
        TypeMirror extendsBound = ((WildcardType) tm).getExtendsBound();
        TypeMirror superBound = ((WildcardType) tm).getSuperBound();
        if (extendsBound != null || superBound != null) {
            TypeMirror rct = resolveCapturedTypeInt(info, extendsBound != null ? extendsBound : superBound);
            if (rct != null) {
                switch (rct.getKind()) {
                    case WILDCARD:
                        return rct;
                    case ARRAY:
                    case DECLARED:
                    case ERROR:
                    case TYPEVAR:
                    case OTHER:
                        return info.getTypes().getWildcardType(
                                extendsBound != null ? rct : null, superBound != null ? rct : null);
                }
            } else {
                // propagate failure out of all wildcards
                return null;
            }
        }
    } else if (tm.getKind() == TypeKind.INTERSECTION) {
        return null;
    }
    
    if (tm.getKind() == TypeKind.DECLARED) {
        DeclaredType dt = (DeclaredType) tm;
        List<TypeMirror> typeArguments = new LinkedList<TypeMirror>();
        
        for (TypeMirror t : dt.getTypeArguments()) {
            TypeMirror targ = resolveCapturedTypeInt(info, t);
            if (targ == null) {
                // bail out, if the type parameter is a wildcard, it's probably not possible
                // to create a proper parametrized type from it
                if (t.getKind() == TypeKind.WILDCARD || t.getKind() == TypeKind.INTERSECTION) {
                    return null;
                }
                // use rawtype
                typeArguments.clear();
                break;
            }
            typeArguments.add(targ);
        }
        
        final TypeMirror enclosingType = dt.getEnclosingType();
        if (enclosingType.getKind() == TypeKind.DECLARED) {
            return info.getTypes().getDeclaredType((DeclaredType) enclosingType, (TypeElement) dt.asElement(), typeArguments.toArray(new TypeMirror[0]));
        } else {
            if (dt.asElement() == null) return dt;
            return info.getTypes().getDeclaredType((TypeElement) dt.asElement(), typeArguments.toArray(new TypeMirror[0]));
        }
    }

    if (tm.getKind() == TypeKind.ARRAY) {
        ArrayType at = (ArrayType) tm;
        TypeMirror tm2 = resolveCapturedTypeInt(info, at.getComponentType());
        return info.getTypes().getArrayType(tm2 != null ? tm2 : tm);
    }
    
    return tm;
}