Java Code Examples for javax.lang.model.type.TypeKind#TYPEVAR
The following examples show how to use
javax.lang.model.type.TypeKind#TYPEVAR .
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: SignatureFactory.java From buck with Apache License 2.0 | 6 votes |
@Override public Void visitTypeParameter(TypeParameterElement element, SignatureVisitor visitor) { visitor.visitFormalTypeParameter(element.getSimpleName().toString()); for (TypeMirror boundType : element.getBounds()) { boolean isClass; try { if (boundType.getKind() == TypeKind.DECLARED) { isClass = ((DeclaredType) boundType).asElement().getKind().isClass(); } else { isClass = boundType.getKind() == TypeKind.TYPEVAR; } } catch (CannotInferException e) { // We can't know whether an inferred type is a class or interface, but it turns out // the compiler does not distinguish between them when reading signatures, so we can // write inferred types as interface bounds. We go ahead and write all bounds as // interface bounds to make the SourceAbiCompatibleSignatureVisitor possible. isClass = false; } boundType.accept( typeVisitorAdapter, isClass ? visitor.visitClassBound() : visitor.visitInterfaceBound()); } return null; }
Example 2
Source File: DeptectiveTreeVisitor.java From deptective with Apache License 2.0 | 6 votes |
/** * Returns the qualified Package Name of the given Tree object or null if the package could not be determined */ protected String getQualifiedPackageName(Tree tree) { TypeMirror typeMirror = trees.getTypeMirror(getCurrentPath()); if (typeMirror == null) { return null; } if (typeMirror.getKind() != TypeKind.DECLARED && typeMirror.getKind() != TypeKind.TYPEVAR) { return null; } Element typeMirrorElement = types.asElement(typeMirror); if (typeMirrorElement == null) { throw new IllegalStateException("Could not get Element for type '" + typeMirror + "'"); } PackageElement pakkage = elements.getPackageOf(typeMirrorElement); return pakkage.getQualifiedName().toString(); }
Example 3
Source File: AutoImport.java From netbeans with Apache License 2.0 | 6 votes |
@Override public Void visitTypeVariable(TypeVariable type, Void p) { Element e = type.asElement(); if (e != null) { CharSequence name = e.getSimpleName(); if (!CAPTURED_WILDCARD.contentEquals(name)) { builder.append(name); return null; } } builder.append("?"); //NOI18N TypeMirror bound = type.getLowerBound(); if (bound != null && bound.getKind() != TypeKind.NULL) { builder.append(" super "); //NOI18N visit(bound); } else { bound = type.getUpperBound(); if (bound != null && bound.getKind() != TypeKind.NULL) { builder.append(" extends "); //NOI18N if (bound.getKind() == TypeKind.TYPEVAR) bound = ((TypeVariable)bound).getLowerBound(); visit(bound); } } return null; }
Example 4
Source File: IntroduceHint.java From netbeans with Apache License 2.0 | 6 votes |
static void prepareTypeVars(TreePath method, CompilationInfo info, Map<TypeMirror, TreePathHandle> typeVar2Def, List<TreePathHandle> typeVars) throws IllegalArgumentException { if (method.getLeaf().getKind() == Kind.METHOD) { MethodTree mt = (MethodTree) method.getLeaf(); for (TypeParameterTree tv : mt.getTypeParameters()) { TreePath def = new TreePath(method, tv); TypeMirror type = info.getTrees().getTypeMirror(def); if (type != null && type.getKind() == TypeKind.TYPEVAR) { TreePathHandle tph = TreePathHandle.create(def, info); typeVar2Def.put(type, tph); typeVars.add(tph); } } } }
Example 5
Source File: AbstractTestGenerator.java From netbeans with Apache License 2.0 | 6 votes |
/** */ private List<VariableTree> generateParamVariables( WorkingCopy workingCopy, ExecutableType srcMethod, String[] varNames) { TreeMaker maker = workingCopy.getTreeMaker(); List<? extends TypeMirror> params = srcMethod.getParameterTypes(); if ((params == null) || params.isEmpty()) { return Collections.<VariableTree>emptyList(); } Set<Modifier> noModifiers = Collections.<Modifier>emptySet(); List<VariableTree> paramVariables = new ArrayList<VariableTree>(params.size()); int index = 0; for (TypeMirror param : params) { if (param.getKind() == TypeKind.TYPEVAR){ param = getSuperType(workingCopy, param); } paramVariables.add( maker.Variable(maker.Modifiers(noModifiers), varNames[index++], maker.Type(param), getDefaultValue(maker, param))); } return paramVariables; }
Example 6
Source File: AbstractAssignabilityChecker.java From netbeans with Apache License 2.0 | 6 votes |
protected boolean handleWildCard( TypeMirror argType, WildcardType varTypeArg, Types types ) { TypeMirror upperBound = varTypeArg.getExtendsBound(); TypeMirror lowerBound = varTypeArg.getSuperBound(); if ( argType instanceof ReferenceType && argType.getKind()!=TypeKind.TYPEVAR) { return handleWildCardActualType(argType, types, upperBound, lowerBound); } if ( argType.getKind() == TypeKind.TYPEVAR ){ return handleWildCardTypeVar(argType, types, upperBound, lowerBound); } return false; }
Example 7
Source File: AbstractTestGenerator.java From netbeans with Apache License 2.0 | 5 votes |
private boolean containsTypeVar(DeclaredType dt, boolean typeVar) { List<? extends TypeMirror> args = dt.getTypeArguments(); for (TypeMirror arg : args) { if (arg.getKind() == TypeKind.TYPEVAR) { typeVar = true; } if (arg.getKind() == TypeKind.DECLARED) { typeVar = typeVar || containsTypeVar((DeclaredType) arg, typeVar); } } return typeVar; }
Example 8
Source File: SourceUtils.java From netbeans with Apache License 2.0 | 5 votes |
public static boolean checkTypesAssignable(CompilationInfo info, TypeMirror from, TypeMirror to) { Context c = ((JavacTaskImpl) info.impl.getJavacTask()).getContext(); if (from.getKind() == TypeKind.TYPEVAR) { Types types = Types.instance(c); TypeVar t = types.substBound((TypeVar)from, com.sun.tools.javac.util.List.of((Type)from), com.sun.tools.javac.util.List.of(types.boxedTypeOrType((Type)to))); return info.getTypes().isAssignable(t.getUpperBound(), to) || info.getTypes().isAssignable(to, t.getUpperBound()); } if (from.getKind() == TypeKind.WILDCARD) { from = Types.instance(c).wildUpperBound((Type)from); } return Check.instance(c).checkType(null, (Type)from, (Type)to).getKind() != TypeKind.ERROR; }
Example 9
Source File: ScoopsProcesssor.java From Scoops with Apache License 2.0 | 5 votes |
private void parseBindToppingStatus(Element element, Map<TypeElement, BindingClass> targetClassMap){ // Verify that element is of type Class if(element.getKind() != ElementKind.CLASS){ error(element, "Only classes can be annotated with %s", BindToppingStatus.class.getSimpleName()); }else{ // Start by verifying common generated code restrictions. boolean hasError = false; // Verify that the target type extends from View. TypeMirror elementType = element.asType(); if (elementType.getKind() == TypeKind.TYPEVAR) { TypeVariable typeVariable = (TypeVariable) elementType; elementType = typeVariable.getUpperBound(); } if (!isSubtypeOfType(elementType, ACTIVITY_TYPE) && !isInterface(elementType)) { error(element, "@%s classes must extend from Activity(%s.%s)", BindToppingStatus.class.getSimpleName(), element.getSimpleName()); hasError = true; } if (hasError) { return; } BindToppingStatus annotation = element.getAnnotation(BindToppingStatus.class); // Start assembling binding information BindingClass bindingClass = getOrCreateTargetClass(targetClassMap, (TypeElement) element); ClassName interpolator = className(asTypeElement(getInterpolatorTypeMirror(annotation))); ClassStatusBarBinding binding = new ClassStatusBarBinding(annotation.value(), interpolator, annotation.duration()); bindingClass.setStatusBarBinding(binding); } }
Example 10
Source File: ScoopsProcesssor.java From Scoops with Apache License 2.0 | 5 votes |
private void parseBindTopping(Element element, Map<TypeElement, BindingClass> targetClassMap){ TypeElement enclosingElement = (TypeElement) element.getEnclosingElement(); // Start by verifying common generated code restrictions. boolean hasError = isInaccessibleViaGeneratedCode(BindTopping.class, "fields", element) || isBindingInWrongPackage(BindTopping.class, element); // Verify that the target type extends from View. TypeMirror elementType = element.asType(); if (elementType.getKind() == TypeKind.TYPEVAR) { TypeVariable typeVariable = (TypeVariable) elementType; elementType = typeVariable.getUpperBound(); } if (!isSubtypeOfType(elementType, VIEW_TYPE) && !isInterface(elementType)) { error(element, "@%s fields must extend from View or be an interface. (%s.%s)", BindTopping.class.getSimpleName(), enclosingElement.getQualifiedName(), element.getSimpleName()); hasError = true; } if (hasError) { return; } BindTopping annotation = element.getAnnotation(BindTopping.class); // Start assembling binding information BindingClass bindingClass = getOrCreateTargetClass(targetClassMap, enclosingElement); String name = element.getSimpleName().toString(); TypeName type = TypeName.get(elementType); ClassName adapter = className(asTypeElement(getAdapterTypeMirror(annotation))); ClassName interpolator = className(asTypeElement(getInterpolatorTypeMirror(annotation))); FieldViewBinding binding = new FieldViewBinding(annotation.value(), name, adapter, interpolator, annotation.duration()); bindingClass.addViewBinding(binding); }
Example 11
Source File: JavaElementHandler.java From netbeans with Apache License 2.0 | 5 votes |
private MethodSignature getSignature(TypeElement classElement, ExecutableElement element, String[] typeParameters, Types types) { String name = element.getSimpleName().toString(); String[] parameters = new String[element.getParameters().size()]; for (int i = 0; i < parameters.length; i++) { VariableElement var = element.getParameters().get(i); TypeMirror type = var.asType(); String typeString = null; if (type.getKind() == TypeKind.TYPEVAR) { List<? extends TypeParameterElement> declaredTypeParameters = element.getTypeParameters(); if (declaredTypeParameters.isEmpty()) { declaredTypeParameters = classElement.getTypeParameters(); } int j = -1; for (TypeParameterElement typeParam : declaredTypeParameters) { j++; if (typeParam.getSimpleName().toString().equals(type.toString())) { break; } } // FIXME why we were doing this for signatures ?? // if (j >= 0 && j < typeParameters.length) { // typeString = typeParameters[j]; // } else { typeString = types.erasure(type).toString(); // } } else { typeString = type.toString(); } int index = typeString.indexOf('<'); if (index >= 0) { typeString = typeString.substring(0, index); } parameters[i] = typeString; } return new MethodSignature(name, parameters); }
Example 12
Source File: ELTypeUtilities.java From netbeans with Apache License 2.0 | 5 votes |
/** * Gets the return type of the given {@code method}. * @param method * @return */ public static TypeMirror getReturnType(CompilationContext info, final ExecutableElement method, ELElement elElement, List<Node> rootToNode) { TypeKind returnTypeKind = method.getReturnType().getKind(); if (returnTypeKind.isPrimitive()) { return info.info().getTypes().getPrimitiveType(returnTypeKind); } else if (returnTypeKind == TypeKind.VOID) { return info.info().getTypes().getNoType(returnTypeKind); } else if (returnTypeKind == TypeKind.TYPEVAR && rootToNode != null && elElement != null) { return getReturnTypeForGenericClass(info, method, elElement, rootToNode); } else { return method.getReturnType(); } }
Example 13
Source File: JavaUtils.java From netbeans with Apache License 2.0 | 5 votes |
public static boolean isMethodSignatureSame(CompilationInfo cinfo, ExecutableElement method1, ExecutableElement method2){ // check for parameters count int paramCount = method1.getParameters().size(); if (paramCount != method2.getParameters().size()) { return false; } for (int i = 0; i < paramCount; i++) { TypeMirror param1 = method1.getParameters().get(i).asType(); TypeMirror param2 = method2.getParameters().get(i).asType(); if (!cinfo.getTypes().isSameType(param1, param2)) { if (isSameDeclaredType(param1, param2)) { continue; } else if (param2.getKind() == TypeKind.TYPEVAR) { // interface method contains type erasure - see issue #201543 if (isParamEquivalentOfErasure(cinfo, method1, method2, param1.toString(), param2.toString())) { continue; } } return false; } } return true; }
Example 14
Source File: LambdaToMethod.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Erasure destroys the implementation parameter subtype * relationship for intersection types */ boolean interfaceParameterIsIntersectionType() { List<Type> tl = tree.getDescriptorType(types).getParameterTypes(); for (; tl.nonEmpty(); tl = tl.tail) { Type pt = tl.head; if (pt.getKind() == TypeKind.TYPEVAR) { TypeVar tv = (TypeVar) pt; if (tv.bound.getKind() == TypeKind.INTERSECTION) { return true; } } } return false; }
Example 15
Source File: AutoCodecProcessor.java From bazel with Apache License 2.0 | 5 votes |
private Relation findRelationWithGenerics(TypeMirror type1, TypeMirror type2) { if (type1.getKind() == TypeKind.TYPEVAR || type1.getKind() == TypeKind.WILDCARD || type2.getKind() == TypeKind.TYPEVAR || type2.getKind() == TypeKind.WILDCARD) { return Relation.EQUAL_TO; } if (env.getTypeUtils().isAssignable(type1, type2)) { if (env.getTypeUtils().isAssignable(type2, type1)) { return Relation.EQUAL_TO; } return Relation.INSTANCE_OF; } if (env.getTypeUtils().isAssignable(type2, type1)) { return Relation.SUPERTYPE_OF; } // From here on out, we can't detect subtype/supertype, we're only checking for equality. TypeMirror erasedType1 = env.getTypeUtils().erasure(type1); TypeMirror erasedType2 = env.getTypeUtils().erasure(type2); if (!env.getTypeUtils().isSameType(erasedType1, erasedType2)) { // Technically, there could be a relationship, but it's too hard to figure out for now. return Relation.UNRELATED_TO; } List<? extends TypeMirror> genericTypes1 = ((DeclaredType) type1).getTypeArguments(); List<? extends TypeMirror> genericTypes2 = ((DeclaredType) type2).getTypeArguments(); if (genericTypes1.size() != genericTypes2.size()) { return null; } for (int i = 0; i < genericTypes1.size(); i++) { Relation result = findRelationWithGenerics(genericTypes1.get(i), genericTypes2.get(i)); if (result != Relation.EQUAL_TO) { return Relation.UNRELATED_TO; } } return Relation.EQUAL_TO; }
Example 16
Source File: AbstractCtType.java From doma with Apache License 2.0 | 4 votes |
@Override public boolean isTypevar() { return type.getKind() == TypeKind.TYPEVAR; }
Example 17
Source File: AbstractAssignabilityChecker.java From netbeans with Apache License 2.0 | 4 votes |
protected boolean checkParameter( TypeMirror argType, TypeMirror varTypeArg ) { if ( argType == null || varTypeArg == null ){ return false; } Types types = getImplementation().getHelper().getCompilationController(). getTypes(); /* * Implementation of spec item : * the required type parameter and the bean type parameter are actual * types with identical raw type, and, if the type is * parameterized, the bean type parameter is assignable to the required * type parameter according to these rules */ if ( argType.getKind()!= TypeKind.TYPEVAR && varTypeArg.getKind()!= TypeKind.TYPEVAR && (argType instanceof ReferenceType) && (varTypeArg instanceof ReferenceType) ) { return checkIsAssignable(getImplementation().getHelper(). getCompilationController().getTypes(), argType, varTypeArg); } if ( varTypeArg.getKind() == TypeKind.WILDCARD ){ return handleWildCard(argType, (WildcardType)varTypeArg, types); } if ( argType.getKind() == TypeKind.TYPEVAR && varTypeArg.getKind() == TypeKind.TYPEVAR) { return handleBothTypeVars(argType, varTypeArg, types); } if (varTypeArg.getKind() != TypeKind.TYPEVAR && argType.getKind() == TypeKind.TYPEVAR) { return handleBeanTypeVar(argType, varTypeArg, types); } if (argType.getKind() != TypeKind.TYPEVAR && varTypeArg.getKind() == TypeKind.TYPEVAR) { return handleRequiredTypeVar(argType, varTypeArg, types); } return false; }
Example 18
Source File: ButterKnifeProcessor.java From butterknife with Apache License 2.0 | 4 votes |
private void parseBindViews(Element element, Map<TypeElement, BindingSet.Builder> builderMap, Set<TypeElement> erasedTargetNames) { TypeElement enclosingElement = (TypeElement) element.getEnclosingElement(); // Start by verifying common generated code restrictions. boolean hasError = isInaccessibleViaGeneratedCode(BindViews.class, "fields", element) || isBindingInWrongPackage(BindViews.class, element); // Verify that the type is a List or an array. TypeMirror elementType = element.asType(); String erasedType = doubleErasure(elementType); TypeMirror viewType = null; FieldCollectionViewBinding.Kind kind = null; if (elementType.getKind() == TypeKind.ARRAY) { ArrayType arrayType = (ArrayType) elementType; viewType = arrayType.getComponentType(); kind = FieldCollectionViewBinding.Kind.ARRAY; } else if (LIST_TYPE.equals(erasedType)) { DeclaredType declaredType = (DeclaredType) elementType; List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments(); if (typeArguments.size() != 1) { error(element, "@%s List must have a generic component. (%s.%s)", BindViews.class.getSimpleName(), enclosingElement.getQualifiedName(), element.getSimpleName()); hasError = true; } else { viewType = typeArguments.get(0); } kind = FieldCollectionViewBinding.Kind.LIST; } else { error(element, "@%s must be a List or array. (%s.%s)", BindViews.class.getSimpleName(), enclosingElement.getQualifiedName(), element.getSimpleName()); hasError = true; } if (viewType != null && viewType.getKind() == TypeKind.TYPEVAR) { TypeVariable typeVariable = (TypeVariable) viewType; viewType = typeVariable.getUpperBound(); } // Verify that the target type extends from View. if (viewType != null && !isSubtypeOfType(viewType, VIEW_TYPE) && !isInterface(viewType)) { if (viewType.getKind() == TypeKind.ERROR) { note(element, "@%s List or array with unresolved type (%s) " + "must elsewhere be generated as a View or interface. (%s.%s)", BindViews.class.getSimpleName(), viewType, enclosingElement.getQualifiedName(), element.getSimpleName()); } else { error(element, "@%s List or array type must extend from View or be an interface. (%s.%s)", BindViews.class.getSimpleName(), enclosingElement.getQualifiedName(), element.getSimpleName()); hasError = true; } } // Assemble information on the field. String name = element.getSimpleName().toString(); int[] ids = element.getAnnotation(BindViews.class).value(); if (ids.length == 0) { error(element, "@%s must specify at least one ID. (%s.%s)", BindViews.class.getSimpleName(), enclosingElement.getQualifiedName(), element.getSimpleName()); hasError = true; } Integer duplicateId = findDuplicate(ids); if (duplicateId != null) { error(element, "@%s annotation contains duplicate ID %d. (%s.%s)", BindViews.class.getSimpleName(), duplicateId, enclosingElement.getQualifiedName(), element.getSimpleName()); hasError = true; } if (hasError) { return; } TypeName type = TypeName.get(requireNonNull(viewType)); boolean required = isFieldRequired(element); BindingSet.Builder builder = getOrCreateBindingBuilder(builderMap, enclosingElement); builder.addFieldCollection(new FieldCollectionViewBinding(name, type, requireNonNull(kind), new ArrayList<>(elementToIds(element, BindViews.class, ids).values()), required)); erasedTargetNames.add(enclosingElement); }
Example 19
Source File: LambdaToMethod.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
/** * Generate the parameter list for the converted member reference. * * @return The receiver variable symbol, if any */ VarSymbol addParametersReturnReceiver() { Type samDesc = localContext.bridgedRefSig(); List<Type> samPTypes = samDesc.getParameterTypes(); List<Type> descPTypes = tree.getDescriptorType(types).getParameterTypes(); // Determine the receiver, if any VarSymbol rcvr; switch (tree.kind) { case BOUND: // The receiver is explicit in the method reference rcvr = addParameter("rec$", tree.getQualifierExpression().type, false); receiverExpression = attr.makeNullCheck(tree.getQualifierExpression()); break; case UNBOUND: // The receiver is the first parameter, extract it and // adjust the SAM and unerased type lists accordingly rcvr = addParameter("rec$", samDesc.getParameterTypes().head, false); samPTypes = samPTypes.tail; descPTypes = descPTypes.tail; break; default: rcvr = null; break; } List<Type> implPTypes = tree.sym.type.getParameterTypes(); int implSize = implPTypes.size(); int samSize = samPTypes.size(); // Last parameter to copy from referenced method, exclude final var args int last = localContext.needsVarArgsConversion() ? implSize - 1 : implSize; // Failsafe -- assure match-up boolean checkForIntersection = tree.varargsElement != null || implSize == descPTypes.size(); // Use parameter types of the implementation method unless the unerased // SAM parameter type is an intersection type, in that case use the // erased SAM parameter type so that the supertype relationship // the implementation method parameters is not obscured. // Note: in this loop, the lists implPTypes, samPTypes, and descPTypes // are used as pointers to the current parameter type information // and are thus not usable afterwards. for (int i = 0; implPTypes.nonEmpty() && i < last; ++i) { // By default use the implementation method parmeter type Type parmType = implPTypes.head; // If the unerased parameter type is a type variable whose // bound is an intersection (eg. <T extends A & B>) then // use the SAM parameter type if (checkForIntersection && descPTypes.head.getKind() == TypeKind.TYPEVAR) { TypeVar tv = (TypeVar) descPTypes.head; if (tv.bound.getKind() == TypeKind.INTERSECTION) { parmType = samPTypes.head; } } addParameter("x$" + i, parmType, true); // Advance to the next parameter implPTypes = implPTypes.tail; samPTypes = samPTypes.tail; descPTypes = descPTypes.tail; } // Flatten out the var args for (int i = last; i < samSize; ++i) { addParameter("xva$" + i, tree.varargsElement, true); } return rcvr; }
Example 20
Source File: JavaEnvironment.java From j2cl with Apache License 2.0 | 4 votes |
/** * 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); }