use of com.sun.tools.javac.code.Type.ClassType in project checker-framework by typetools.
the class WholeProgramInferenceScenes method getEnclosingClassSymbol.
/**
* Returns the ClassSymbol of the class encapsulating the node n passed as parameter.
*
* <p>If the receiver of field is an instance of "this", the implementation obtains the
* ClassSymbol by using classTree. Otherwise, the ClassSymbol is from the field's receiver.
*/
// TODO: These methods below could be moved somewhere else.
private ClassSymbol getEnclosingClassSymbol(ClassTree classTree, Node field) {
Node receiverNode = null;
if (field instanceof FieldAccessNode) {
receiverNode = ((FieldAccessNode) field).getReceiver();
} else if (field instanceof LocalVariableNode) {
receiverNode = ((LocalVariableNode) field).getReceiver();
} else {
ErrorReporter.errorAbort("Unexpected type: " + field.getClass());
}
if ((receiverNode == null || receiverNode instanceof ImplicitThisLiteralNode) && classTree != null) {
return (ClassSymbol) TreeUtils.elementFromTree(classTree);
}
TypeMirror type = receiverNode.getType();
if (type instanceof ClassType) {
TypeSymbol tsym = ((ClassType) type).asElement();
return tsym.enclClass();
}
return getEnclosingClassSymbol(receiverNode.getTree());
}
use of com.sun.tools.javac.code.Type.ClassType in project checker-framework by typetools.
the class FlowExpressionParseUtil method getTypeOfEnclosingClass.
/**
* Returns the type of the inner most enclosing class.Type.noType is returned if no enclosing
* class is found. This is in contrast to {@link DeclaredType#getEnclosingType()} which returns
* the type of the inner most instance. If the inner most enclosing class is static this method
* will return the type of that class where as {@link DeclaredType#getEnclosingType()} will
* return the type of the inner most enclosing class that is not static.
*
* @param type a DeclaredType
* @return the type of the innermost enclosing class or Type.noType
*/
private static TypeMirror getTypeOfEnclosingClass(DeclaredType type) {
if (type instanceof ClassType) {
// enclClass() needs to be called on tsym.owner,
// otherwise it simply returns tsym.
Symbol sym = ((ClassType) type).tsym.owner;
if (sym == null) {
return Type.noType;
}
ClassSymbol cs = sym.enclClass();
if (cs == null) {
return Type.noType;
}
return cs.asType();
} else {
return type.getEnclosingType();
}
}
use of com.sun.tools.javac.code.Type.ClassType in project checker-framework by typetools.
the class FlowExpressionParseUtil method parseSuper.
private static Receiver parseSuper(String s, Types types, FlowExpressionContext context) throws FlowExpressionParseException {
// super literal
List<? extends TypeMirror> superTypes = types.directSupertypes(context.receiver.getType());
// find class supertype
TypeMirror superType = null;
for (TypeMirror t : superTypes) {
// ignore interface types
if (!(t instanceof ClassType)) {
continue;
}
ClassType tt = (ClassType) t;
if (!tt.isInterface()) {
superType = t;
break;
}
}
if (superType == null) {
throw constructParserException(s, "super class not found");
}
return new ThisReference(superType);
}
use of com.sun.tools.javac.code.Type.ClassType in project lombok by rzwitserloot.
the class JavacResolution method typeToJCTree0.
private static JCExpression typeToJCTree0(Type type, JavacAST ast, boolean allowCompound, boolean allowVoid) throws TypeNotConvertibleException {
// NB: There's such a thing as maker.Type(type), but this doesn't work very well; it screws up anonymous classes, captures, and adds an extra prefix dot for some reason too.
// -- so we write our own take on that here.
JavacTreeMaker maker = ast.getTreeMaker();
if (CTC_BOT.equals(typeTag(type)))
return createJavaLangObject(ast);
if (CTC_VOID.equals(typeTag(type)))
return allowVoid ? primitiveToJCTree(type.getKind(), maker) : createJavaLangObject(ast);
if (type.isPrimitive())
return primitiveToJCTree(type.getKind(), maker);
if (type.isErroneous())
throw new TypeNotConvertibleException("Type cannot be resolved");
TypeSymbol symbol = type.asElement();
List<Type> generics = type.getTypeArguments();
JCExpression replacement = null;
if (symbol == null)
throw new TypeNotConvertibleException("Null or compound type");
if (symbol.name.length() == 0) {
// Anonymous inner class
if (type instanceof ClassType) {
List<Type> ifaces = ((ClassType) type).interfaces_field;
Type supertype = ((ClassType) type).supertype_field;
if (ifaces != null && ifaces.length() == 1) {
return typeToJCTree(ifaces.get(0), ast, allowCompound, allowVoid);
}
if (supertype != null)
return typeToJCTree(supertype, ast, allowCompound, allowVoid);
}
throw new TypeNotConvertibleException("Anonymous inner class");
}
if (type instanceof CapturedType || type instanceof WildcardType) {
Type lower, upper;
if (type instanceof WildcardType) {
upper = ((WildcardType) type).getExtendsBound();
lower = ((WildcardType) type).getSuperBound();
} else {
lower = type.getLowerBound();
upper = type.getUpperBound();
}
if (allowCompound) {
if (lower == null || CTC_BOT.equals(typeTag(lower))) {
if (upper == null || upper.toString().equals("java.lang.Object")) {
return maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null);
}
if (upper.getTypeArguments().contains(type)) {
return maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null);
}
return maker.Wildcard(maker.TypeBoundKind(BoundKind.EXTENDS), typeToJCTree(upper, ast, false, false));
} else {
return maker.Wildcard(maker.TypeBoundKind(BoundKind.SUPER), typeToJCTree(lower, ast, false, false));
}
}
if (upper != null) {
if (upper.getTypeArguments().contains(type)) {
return maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null);
}
return typeToJCTree(upper, ast, allowCompound, allowVoid);
}
return createJavaLangObject(ast);
}
String qName;
if (symbol.isLocal()) {
qName = symbol.getSimpleName().toString();
} else if (symbol.type != null && symbol.type.getEnclosingType() != null && typeTag(symbol.type.getEnclosingType()).equals(typeTag("CLASS"))) {
replacement = typeToJCTree0(type.getEnclosingType(), ast, false, false);
qName = symbol.getSimpleName().toString();
} else {
qName = symbol.getQualifiedName().toString();
}
if (qName.isEmpty())
throw new TypeNotConvertibleException("unknown type");
if (qName.startsWith("<"))
throw new TypeNotConvertibleException(qName);
String[] baseNames = qName.split("\\.");
int i = 0;
if (replacement == null) {
replacement = maker.Ident(ast.toName(baseNames[0]));
i = 1;
}
for (; i < baseNames.length; i++) {
replacement = maker.Select(replacement, ast.toName(baseNames[i]));
}
return genericsToJCTreeNodes(generics, ast, replacement);
}
use of com.sun.tools.javac.code.Type.ClassType in project lombok by rzwitserloot.
the class HandleDelegate method addMethodBindings.
public void addMethodBindings(List<MethodSig> signatures, ClassType ct, JavacTypes types, Set<String> banList) throws DelegateRecursion {
TypeSymbol tsym = ct.asElement();
if (tsym == null)
return;
for (Symbol member : tsym.getEnclosedElements()) {
for (Compound am : member.getAnnotationMirrors()) {
String name = null;
try {
name = am.type.tsym.flatName().toString();
} catch (Exception ignore) {
}
if ("lombok.Delegate".equals(name) || "lombok.experimental.Delegate".equals(name)) {
throw new DelegateRecursion(ct.tsym.name.toString(), member.name.toString());
}
}
if (member.getKind() != ElementKind.METHOD)
continue;
if (member.isStatic())
continue;
if (member.isConstructor())
continue;
ExecutableElement exElem = (ExecutableElement) member;
if (!exElem.getModifiers().contains(Modifier.PUBLIC))
continue;
ExecutableType methodType = (ExecutableType) types.asMemberOf(ct, member);
String sig = printSig(methodType, member.name, types);
// If add returns false, it was already in there
if (!banList.add(sig))
continue;
boolean isDeprecated = (member.flags() & DEPRECATED) != 0;
signatures.add(new MethodSig(member.name, methodType, isDeprecated, exElem));
}
if (ct.supertype_field instanceof ClassType)
addMethodBindings(signatures, (ClassType) ct.supertype_field, types, banList);
if (ct.interfaces_field != null)
for (Type iface : ct.interfaces_field) {
if (iface instanceof ClassType)
addMethodBindings(signatures, (ClassType) iface, types, banList);
}
}
Aggregations