use of com.github.javaparser.ast.type.TypeParameter in project checker-framework by typetools.
the class ToIndexFileConverter method visit.
@Override
public Void visit(MethodDeclaration decl, AElement elem) {
Type type = decl.getType();
List<Parameter> params = decl.getParameters();
List<TypeParameter> typeParams = decl.getTypeParameters();
Optional<ReceiverParameter> rcvrParam = decl.getReceiverParameter();
BlockStmt body = decl.getBody().orElse(null);
StringBuilder sb = new StringBuilder(decl.getNameAsString()).append('(');
AClass clazz = (AClass) elem;
AMethod method;
if (params != null) {
for (Parameter param : params) {
Type ptype = param.getType();
sb.append(getJVML(ptype));
}
}
sb.append(')').append(getJVML(type));
method = clazz.methods.vivify(sb.toString());
visitDecl(decl, method);
visitType(type, method.returnType);
if (params != null) {
for (int i = 0; i < params.size(); i++) {
Parameter param = params.get(i);
AField field = method.parameters.vivify(i);
visitType(param.getType(), field.type);
}
}
if (rcvrParam.isPresent()) {
for (AnnotationExpr expr : rcvrParam.get().getAnnotations()) {
Annotation anno = extractAnnotation(expr);
method.receiver.type.tlAnnotationsHere.add(anno);
}
}
if (typeParams != null) {
for (int i = 0; i < typeParams.size(); i++) {
TypeParameter typeParam = typeParams.get(i);
List<ClassOrInterfaceType> bounds = typeParam.getTypeBound();
if (bounds != null) {
for (int j = 0; j < bounds.size(); j++) {
ClassOrInterfaceType bound = bounds.get(j);
BoundLocation loc = new BoundLocation(i, j);
bound.accept(this, method.bounds.vivify(loc));
}
}
}
}
return body == null ? null : body.accept(this, method);
}
use of com.github.javaparser.ast.type.TypeParameter in project javaparser by javaparser.
the class ClassOrInterfaceDeclarationTransformationsTest method addingTypeParameterAsFirstWhenThereAreSome.
@Test
public void addingTypeParameterAsFirstWhenThereAreSome() throws IOException {
ClassOrInterfaceDeclaration cid = consider("class A<U> {}");
cid.getTypeParameters().addFirst(new TypeParameter("T", new NodeList<>()));
assertTransformedToString("class A<T, U> {}", cid);
}
use of com.github.javaparser.ast.type.TypeParameter in project javaparser by javaparser.
the class JavaParserTypeAdapter method solveType.
public SymbolReference<ResolvedTypeDeclaration> solveType(String name, TypeSolver typeSolver) {
if (wrappedNode instanceof NodeWithTypeParameters<?>) {
NodeList<TypeParameter> typeParameters = ((NodeWithTypeParameters<?>) wrappedNode).getTypeParameters();
for (com.github.javaparser.ast.type.TypeParameter typeParameter : typeParameters) {
if (typeParameter.getName().getId().equals(name)) {
return SymbolReference.solved(new JavaParserTypeVariableDeclaration(typeParameter, typeSolver));
}
}
}
// Internal classes
for (BodyDeclaration<?> member : this.wrappedNode.getMembers()) {
if (member instanceof com.github.javaparser.ast.body.TypeDeclaration) {
com.github.javaparser.ast.body.TypeDeclaration<?> internalType = (com.github.javaparser.ast.body.TypeDeclaration<?>) member;
String prefix = internalType.getName() + ".";
if (internalType.getName().getId().equals(name)) {
if (internalType instanceof ClassOrInterfaceDeclaration) {
return SymbolReference.solved(new JavaParserClassDeclaration((com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) internalType, typeSolver));
} else if (internalType instanceof EnumDeclaration) {
return SymbolReference.solved(new JavaParserEnumDeclaration((com.github.javaparser.ast.body.EnumDeclaration) internalType, typeSolver));
} else {
throw new UnsupportedOperationException();
}
} else if (name.startsWith(prefix) && name.length() > prefix.length()) {
if (internalType instanceof ClassOrInterfaceDeclaration) {
return new JavaParserClassDeclaration((com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) internalType, typeSolver).solveType(name.substring(prefix.length()), typeSolver);
} else if (internalType instanceof EnumDeclaration) {
return new SymbolSolver(typeSolver).solveTypeInType(new JavaParserEnumDeclaration((com.github.javaparser.ast.body.EnumDeclaration) internalType, typeSolver), name.substring(prefix.length()));
} else {
throw new UnsupportedOperationException();
}
}
}
}
return SymbolReference.unsolved(ResolvedTypeDeclaration.class);
}
use of com.github.javaparser.ast.type.TypeParameter in project checker-framework by typetools.
the class StubParser method annotateTypeParameters.
private void annotateTypeParameters(// for debugging
BodyDeclaration<?> decl, // for debugging; TypeElement or ExecutableElement
Object elt, Map<Element, AnnotatedTypeMirror> atypes, List<? extends AnnotatedTypeMirror> typeArguments, List<TypeParameter> typeParameters) {
if (typeParameters == null) {
return;
}
if (typeParameters.size() != typeArguments.size()) {
String msg = String.format("annotateTypeParameters: mismatched sizes: typeParameters (size %d)=%s; typeArguments (size %d)=%s; decl=%s; elt=%s (%s).", typeParameters.size(), typeParameters, typeArguments.size(), typeArguments, decl.toString().replace(LINE_SEPARATOR, " "), elt.toString().replace(LINE_SEPARATOR, " "), elt.getClass());
if (!debugStubParser) {
msg = msg + "%n For more details, run with -AstubDebug";
}
stubWarn(msg);
}
for (int i = 0; i < typeParameters.size(); ++i) {
TypeParameter param = typeParameters.get(i);
AnnotatedTypeVariable paramType = (AnnotatedTypeVariable) typeArguments.get(i);
if (param.getTypeBound() == null || param.getTypeBound().isEmpty()) {
// No bound so annotations are both lower and upper bounds
annotate(paramType, param.getAnnotations());
} else if (param.getTypeBound() != null && param.getTypeBound().size() > 0) {
annotate(paramType.getLowerBound(), param.getAnnotations());
annotate(paramType.getUpperBound(), param.getTypeBound().get(0), null);
if (param.getTypeBound().size() > 1) {
// TODO: add support for intersection types
stubWarnNotFound("Annotations on intersection types are not yet supported");
}
}
putNew(atypes, paramType.getUnderlyingType().asElement(), paramType);
}
}
use of com.github.javaparser.ast.type.TypeParameter in project checker-framework by typetools.
the class StubParser method processType.
/**
* @return list of AnnotatedTypeVariable of the type's type parameter declarations
*/
private List<AnnotatedTypeVariable> processType(ClassOrInterfaceDeclaration decl, TypeElement elt) {
annotateDecl(declAnnos, elt, decl.getAnnotations());
AnnotatedDeclaredType type = atypeFactory.fromElement(elt);
annotate(type, decl.getAnnotations());
final List<? extends AnnotatedTypeMirror> typeArguments = type.getTypeArguments();
final List<TypeParameter> typeParameters = decl.getTypeParameters();
if (debugStubParser) {
int numParams = (typeParameters == null ? 0 : typeParameters.size());
int numArgs = (typeArguments == null ? 0 : typeArguments.size());
if (numParams != numArgs) {
stubDebug(String.format("parseType: mismatched sizes for typeParameters=%s (size %d) and typeArguments=%s (size %d); decl=%s; elt=%s (%s); type=%s (%s); parseState=%s", typeParameters, numParams, typeArguments, numArgs, decl.toString().replace(LINE_SEPARATOR, " "), elt.toString().replace(LINE_SEPARATOR, " "), elt.getClass(), type, type.getClass(), parseState));
stubDebug("Proceeding despite mismatched sizes");
}
}
annotateTypeParameters(decl, elt, atypes, typeArguments, typeParameters);
annotateSupertypes(decl, type);
putNew(atypes, elt, type);
List<AnnotatedTypeVariable> typeVariables = new ArrayList<>();
for (AnnotatedTypeMirror typeV : type.getTypeArguments()) {
if (typeV.getKind() != TypeKind.TYPEVAR) {
stubWarn("expected an AnnotatedTypeVariable but found type kind " + typeV.getKind() + ": " + typeV);
} else {
typeVariables.add((AnnotatedTypeVariable) typeV);
}
}
return typeVariables;
}
Aggregations