com.sun.tools.javac.code.BoundKind Java Examples
The following examples show how to use
com.sun.tools.javac.code.BoundKind.
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: VarTypePrinter.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
protected Type makeWildcard(Type upper, Type lower) { BoundKind bk; Type bound; if (upper.hasTag(BOT)) { upper = syms.objectType; } boolean isUpperObject = types.isSameType(upper, syms.objectType); if (!lower.hasTag(BOT) && isUpperObject) { bound = lower; bk = SUPER; } else { bound = upper; bk = isUpperObject ? UNBOUND : EXTENDS; } return new WildcardType(bound, bk, syms.boundClass); }
Example #2
Source File: TreeFactory.java From netbeans with Apache License 2.0 | 6 votes |
public WildcardTree Wildcard(Kind kind, Tree type) { final BoundKind boundKind; switch (kind) { case UNBOUNDED_WILDCARD: boundKind = BoundKind.UNBOUND; break; case EXTENDS_WILDCARD: boundKind = BoundKind.EXTENDS; break; case SUPER_WILDCARD: boundKind = BoundKind.SUPER; break; default: throw new IllegalArgumentException("Unknown wildcard bound " + kind); } TypeBoundKind tbk = make.at(NOPOS).TypeBoundKind(boundKind); return make.at(NOPOS).Wildcard(tbk, (JCExpression)type); }
Example #3
Source File: TestProcessor.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
@Override public void visitWildcard(JCWildcard tree) { if (tree.kind.kind == BoundKind.UNBOUND) result = tree.kind.toString(); else result = tree.kind + " " + print(tree.inner); }
Example #4
Source File: TestProcessor.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
@Override public void visitWildcard(JCWildcard tree) { if (tree.kind.kind == BoundKind.UNBOUND) result = tree.kind.toString(); else result = tree.kind + " " + print(tree.inner); }
Example #5
Source File: TestProcessor.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
@Override public void visitWildcard(JCWildcard tree) { if (tree.kind.kind == BoundKind.UNBOUND) result = tree.kind.toString(); else result = tree.kind + " " + print(tree.inner); }
Example #6
Source File: TestProcessor.java From hottub with GNU General Public License v2.0 | 5 votes |
@Override public void visitWildcard(JCWildcard tree) { if (tree.kind.kind == BoundKind.UNBOUND) result = tree.kind.toString(); else result = tree.kind + " " + print(tree.inner); }
Example #7
Source File: TestProcessor.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Override public void visitWildcard(JCWildcard tree) { if (tree.kind.kind == BoundKind.UNBOUND) result = tree.kind.toString(); else result = tree.kind + " " + print(tree.inner); }
Example #8
Source File: TestProcessor.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
@Override public void visitWildcard(JCWildcard tree) { if (tree.kind.kind == BoundKind.UNBOUND) result = tree.kind.toString(); else result = tree.kind + " " + print(tree.inner); }
Example #9
Source File: TemplatingTest.java From Refaster with Apache License 2.0 | 5 votes |
@Test public void genericMethodInvocation() { compile( "import java.util.Collections;", "import java.util.List;", "class GenericTemplateExample {", " public <E> List<E> example(List<E> list) {", " return Collections.unmodifiableList(list);", " }", "}"); UTypeVar tVar = UTypeVar.create("T"); UTypeVar eVar = UTypeVar.create("E"); assertEquals( ExpressionTemplate.create( ImmutableClassToInstanceMap.<Annotation>builder().build(), ImmutableList.of(eVar), ImmutableMap.of("list", UClassType.create("java.util.List", eVar)), UMethodInvocation.create( UStaticIdent.create( "java.util.Collections", "unmodifiableList", UForAll.create( ImmutableList.of(tVar), UMethodType.create( UClassType.create("java.util.List", tVar), UClassType.create( "java.util.List", UWildcardType.create(BoundKind.EXTENDS, tVar))))), UFreeIdent.create("list")), UClassType.create("java.util.List", eVar)), UTemplater.createTemplate(context, getMethodDeclaration("example"))); }
Example #10
Source File: TestProcessor.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
@Override public void visitWildcard(JCWildcard tree) { if (tree.kind.kind == BoundKind.UNBOUND) result = tree.kind.toString(); else result = tree.kind + " " + print(tree.inner); }
Example #11
Source File: UWildcardTypeTest.java From Refaster with Apache License 2.0 | 5 votes |
@Test public void equality() { UType objectType = UClassType.create("java.lang.Object", ImmutableList.<UType>of()); UType setType = UClassType.create("java.util.Set", ImmutableList.<UType>of(objectType)); new EqualsTester() .addEqualityGroup(UWildcardType.create(BoundKind.UNBOUND, objectType)) // ? .addEqualityGroup(UWildcardType.create(BoundKind.EXTENDS, objectType)) // ? extends Object .addEqualityGroup(UWildcardType.create(BoundKind.EXTENDS, setType)) // ? extends Set<Object> .addEqualityGroup(UWildcardType.create(BoundKind.SUPER, setType)) // ? super Set<Object> .testEquals(); }
Example #12
Source File: TestProcessor.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
@Override public void visitWildcard(JCWildcard tree) { if (tree.kind.kind == BoundKind.UNBOUND) result = tree.kind.toString(); else result = tree.kind + " " + print(tree.inner); }
Example #13
Source File: TemplatingTest.java From Refaster with Apache License 2.0 | 4 votes |
@Test public void localVariable() { compile( "import java.util.ArrayList;", "import java.util.Comparator;", "import java.util.Collection;", "import java.util.Collections;", "import java.util.List;", "class LocalVariableExample {", " public <E> void example(Collection<E> collection, Comparator<? super E> comparator) {", " List<E> list = new ArrayList<E>(collection);", " Collections.sort(list, comparator);", " }", "}"); assertEquals( BlockTemplate.create( ImmutableList.of(UTypeVar.create("E")), ImmutableMap.of( "collection", UClassType.create("java.util.Collection", UTypeVar.create("E")), "comparator", UClassType.create("java.util.Comparator", UWildcardType.create(BoundKind.SUPER, UTypeVar.create("E")))), UVariableDecl.create( "list", UTypeApply.create("java.util.List", UTypeVarIdent.create("E")), UNewClass.create( UTypeApply.create("java.util.ArrayList", UTypeVarIdent.create("E")), UFreeIdent.create("collection"))), UExpressionStatement.create(UMethodInvocation.create( UStaticIdent.create( "java.util.Collections", "sort", UForAll.create( ImmutableList.of(UTypeVar.create("T")), UMethodType.create(UPrimitiveType.VOID, UClassType.create("java.util.List", UTypeVar.create("T")), UClassType.create("java.util.Comparator", UWildcardType.create(BoundKind.SUPER, UTypeVar.create("T")))))), ULocalVarIdent.create("list"), UFreeIdent.create("comparator")))), UTemplater.createTemplate(context, getMethodDeclaration("example"))); }
Example #14
Source File: UWildcardType.java From Refaster with Apache License 2.0 | 4 votes |
/** * This corresponds to a plain ? wildcard. */ public static UWildcardType create() { return create(BoundKind.UNBOUND, UClassType.create("java.lang.Object")); }
Example #15
Source File: UWildcardType.java From Refaster with Apache License 2.0 | 4 votes |
public static UWildcardType create(BoundKind boundKind, UType bound) { return new AutoValue_UWildcardType(boundKind, bound); }
Example #16
Source File: JavacHandlerUtil.java From EasyMPermission with MIT License | 4 votes |
private static JCExpression cloneType0(JavacTreeMaker maker, JCTree in) { if (in == null) return null; if (in instanceof JCPrimitiveTypeTree) return (JCExpression) in; if (in instanceof JCIdent) { return maker.Ident(((JCIdent) in).name); } if (in instanceof JCFieldAccess) { JCFieldAccess fa = (JCFieldAccess) in; return maker.Select(cloneType0(maker, fa.selected), fa.name); } if (in instanceof JCArrayTypeTree) { JCArrayTypeTree att = (JCArrayTypeTree) in; return maker.TypeArray(cloneType0(maker, att.elemtype)); } if (in instanceof JCTypeApply) { JCTypeApply ta = (JCTypeApply) in; ListBuffer<JCExpression> lb = new ListBuffer<JCExpression>(); for (JCExpression typeArg : ta.arguments) { lb.append(cloneType0(maker, typeArg)); } return maker.TypeApply(cloneType0(maker, ta.clazz), lb.toList()); } if (in instanceof JCWildcard) { JCWildcard w = (JCWildcard) in; JCExpression newInner = cloneType0(maker, w.inner); TypeBoundKind newKind; switch (w.getKind()) { case SUPER_WILDCARD: newKind = maker.TypeBoundKind(BoundKind.SUPER); break; case EXTENDS_WILDCARD: newKind = maker.TypeBoundKind(BoundKind.EXTENDS); break; default: case UNBOUNDED_WILDCARD: newKind = maker.TypeBoundKind(BoundKind.UNBOUND); break; } return maker.Wildcard(newKind, newInner); } // This is somewhat unsafe, but it's better than outright throwing an exception here. Returning null will just cause an exception down the pipeline. return (JCExpression) in; }
Example #17
Source File: TemplatingTest.java From Refaster with Apache License 2.0 | 4 votes |
@Test public void anonymousClass() { compile( "import java.util.Collections;", "import java.util.Comparator;", "import java.util.List;", "class AnonymousClassExample {", " public void example(List<String> list) {", " Collections.sort(list, new Comparator<String>() {", " @Override public int compare(String a, String b) {", " return a.compareTo(b);", " }", " });", " }", "}"); UTypeVar tVar = UTypeVar.create("T"); assertEquals( BlockTemplate.create( ImmutableMap.of("list", UClassType.create("java.util.List", UClassType.create("java.lang.String"))), UExpressionStatement.create(UMethodInvocation.create( UStaticIdent.create("java.util.Collections", "sort", UForAll.create( ImmutableList.of(tVar), UMethodType.create( UPrimitiveType.VOID, UClassType.create("java.util.List", tVar), UClassType.create("java.util.Comparator", UWildcardType.create(BoundKind.SUPER, tVar))))), UFreeIdent.create("list"), UNewClass.create( null, ImmutableList.<UExpression>of(), UTypeApply.create( "java.util.Comparator", UClassIdent.create("java.lang.String")), ImmutableList.<UExpression>of(), UClassDecl.create(UMethodDecl.create( UModifiers.create( Flags.PUBLIC, UAnnotation.create(UClassIdent.create("java.lang.Override"))), "compare", UPrimitiveTypeTree.INT, ImmutableList.of( UVariableDecl.create("a", UClassIdent.create("java.lang.String")), UVariableDecl.create("b", UClassIdent.create("java.lang.String"))), ImmutableList.<UExpression>of(), UBlock.create(UReturn.create(UMethodInvocation.create( UMemberSelect.create( ULocalVarIdent.create("a"), "compareTo", UMethodType.create( UPrimitiveType.INT, UClassType.create("java.lang.String"))), ULocalVarIdent.create("b")))))))))), UTemplater.createTemplate(context, getMethodDeclaration("example"))); }
Example #18
Source File: UnificationTest.java From Refaster with Apache License 2.0 | 4 votes |
@Test public void returnTypeMatters() { /* Template: * <E> List<E> template(List<E> list) { * return Collections.unmodifiableList(list); * } * * Note that Collections.unmodifiableList takes a List<? extends T>, * but we're restricting to the case where the return type is the same. */ UTypeVar tVar = UTypeVar.create("T"); UTypeVar eVar = UTypeVar.create("E"); ExpressionTemplate template = ExpressionTemplate.create( ImmutableClassToInstanceMap.<Annotation>builder().build(), ImmutableList.of(eVar), ImmutableMap.of("list", UClassType.create("java.util.List", eVar)), UMethodInvocation.create( UStaticIdent.create("java.util.Collections", "unmodifiableList", UForAll.create(ImmutableList.of(tVar), UMethodType.create( UClassType.create("java.util.List", tVar), UClassType.create("java.util.List", UWildcardType.create(BoundKind.EXTENDS, tVar))))), UFreeIdent.create("list")), UClassType.create("java.util.List", eVar)); compile( "import java.util.ArrayList;", "import java.util.Collections;", "import java.util.List;", "class ReturnTypeMattersExample {", " public void example() {", // positive examples " Collections.unmodifiableList(new ArrayList<String>());", " List<Integer> ints = Collections.unmodifiableList(Collections.singletonList(1));", // negative examples " List<CharSequence> seqs = Collections.<CharSequence>unmodifiableList(", " new ArrayList<String>());", " }", "}"); expectMatches(template, Match.create(ImmutableMap.of( "list", "new ArrayList<String>()", "E", "java.lang.String")), Match.create(ImmutableMap.of( "list", "Collections.singletonList(1)", "E", "java.lang.Integer"))); }
Example #19
Source File: JavacResolution.java From EasyMPermission with MIT License | 4 votes |
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); }
Example #20
Source File: JavacTreeMaker.java From EasyMPermission with MIT License | 4 votes |
public TypeBoundKind TypeBoundKind(BoundKind kind) { return invoke(TypeBoundKind, kind); }
Example #21
Source File: TypeHarness.java From openjdk-8 with GNU General Public License v2.0 | 4 votes |
public WildcardType Wildcard(BoundKind bk, Type bound) { return new WildcardType(bound, bk, predef.boundClass); }
Example #22
Source File: UWildcardTypeTest.java From Refaster with Apache License 2.0 | 4 votes |
@Test public void serialization() { UType numberType = UClassType.create("java.lang.Number", ImmutableList.<UType>of()); SerializableTester.reserializeAndAssert(UWildcardType.create(BoundKind.EXTENDS, numberType)); }
Example #23
Source File: TypeHarness.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
public WildcardType Wildcard(BoundKind bk, Type bound) { return new WildcardType(bound, bk, predef.boundClass); }
Example #24
Source File: TypeHarness.java From hottub with GNU General Public License v2.0 | 4 votes |
public WildcardType Wildcard(BoundKind bk, Type bound) { return new WildcardType(bound, bk, predef.boundClass); }
Example #25
Source File: SrcClassUtil.java From manifold with Apache License 2.0 | 4 votes |
private String typeNoAnnotations( Type type ) { if( isJava8() ) { return type.toString(); } StringBuilder sb = new StringBuilder(); if( type instanceof Type.ClassType ) { if( type.getEnclosingType().hasTag( CLASS ) && ReflectUtil.field( type.tsym.owner, "kind" ).get() == ReflectUtil.field( "com.sun.tools.javac.code.Kinds$Kind", "TYP" ).getStatic() ) { sb.append( typeNoAnnotations( type.getEnclosingType() ) ); sb.append( "." ); sb.append( ReflectUtil.method( type, "className", Symbol.class, boolean.class ).invoke( type.tsym, false ) ); } else { sb.append( ReflectUtil.method( type, "className", Symbol.class, boolean.class ).invoke( type.tsym, true ) ); } List<Type> typeArgs = type.getTypeArguments(); if( typeArgs.nonEmpty() ) { sb.append( '<' ); for( int i = 0; i < typeArgs.size(); i++ ) { if( i > 0 ) { sb.append( ", " ); } Type typeArg = typeArgs.get( i ); sb.append( typeNoAnnotations( typeArg ) ); } sb.append( ">" ); } } else if( type instanceof Type.ArrayType ) { sb.append( typeNoAnnotations( ((Type.ArrayType)type).getComponentType() ) ).append( "[]" ); } else if( type instanceof Type.WildcardType ) { Type.WildcardType wildcardType = (Type.WildcardType)type; BoundKind kind = wildcardType.kind; sb.append( kind.toString() ); if( kind != BoundKind.UNBOUND ) { sb.append( typeNoAnnotations( wildcardType.type ) ); } } else { sb.append( type.toString() ); } return sb.toString(); }
Example #26
Source File: TypeHarness.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
public WildcardType Wildcard(BoundKind bk, Type bound) { return new WildcardType(bound, bk, predef.boundClass); }
Example #27
Source File: JCTree.java From javaide with GNU General Public License v3.0 | 4 votes |
protected TypeBoundKind(BoundKind kind) { this.kind = kind; }
Example #28
Source File: TypeHarness.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
public WildcardType Wildcard(BoundKind bk, Type bound) { return new WildcardType(bound, bk, predef.boundClass); }
Example #29
Source File: ClassReader.java From lua-for-android with BSD 3-Clause "New" or "Revised" License | 4 votes |
public Type javaTypeToType(java.lang.reflect.Type type){ if(type instanceof Class) return classToType((Class) type); else if(type instanceof java.lang.reflect.WildcardType){ java.lang.reflect.Type[] lowerBounds=((java.lang.reflect.WildcardType) type).getLowerBounds(); java.lang.reflect.Type[] upperBounds=((java.lang.reflect.WildcardType) type).getUpperBounds(); if(lowerBounds.length>0){ if(upperBounds.length>1||lowerBounds.length!=1) {//always have object as its upper bound throw badClassFile("bad.class.file",type.toString()); } return new WildcardType(javaTypeToType(lowerBounds[0]),BoundKind.SUPER,syms.boundClass); }else if(upperBounds.length>0){ if(upperBounds.length!=1) { throw badClassFile("bad.class.file",type.toString()); } return new WildcardType(javaTypeToType(upperBounds[0]),BoundKind.EXTENDS,syms.boundClass); } return new WildcardType(syms.objectType,BoundKind.UNBOUND,syms.boundClass); }else if(type instanceof ParameterizedType){ java.lang.reflect.Type[] args=((ParameterizedType) type).getActualTypeArguments(); List<Type> params=StreamSupport.stream(Arrays.asList(args)).map(this::javaTypeToType).collect(List.collector()); ClassSymbol symbol=syms.enterClass(currentModule,names.fromString(((Class)((ParameterizedType) type).getRawType()).getName())); return new ClassType(Type.noType,params,symbol){ boolean completed; @Override public Type getEnclosingType() { if(!completed){ completed=true; tsym.complete(); super.setEnclosingType(tsym.type.getEnclosingType()); } return super.getEnclosingType(); } @Override public void setEnclosingType(Type outer) { throw new UnsupportedOperationException(); } }; }else if(type instanceof GenericArrayType){ return types.makeArrayType(javaTypeToType(((GenericArrayType) type).getGenericComponentType())); }else if(type instanceof TypeVariable) return findTypeVar(names.fromString(type.toString())); return null; }
Example #30
Source File: TypeHarness.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
public WildcardType Wildcard(BoundKind bk, Type bound) { return new WildcardType(bound, bk, predef.boundClass); }