Skip to content

Commit

Permalink
Add support for some generics and varargs
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Stryker <stryker@redhat.com>
  • Loading branch information
Rob Stryker committed Mar 7, 2024
1 parent 11bca04 commit 82fa7f2
Showing 1 changed file with 64 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*******************************************************************************/
package org.eclipse.jdt.core.dom;

import static com.sun.tools.javac.tree.JCTree.Tag.ANNOTATED_TYPE;
import static com.sun.tools.javac.code.Flags.VARARGS;
import static com.sun.tools.javac.tree.JCTree.Tag.TYPEARRAY;

import java.io.IOException;
Expand All @@ -34,14 +34,14 @@
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.PrimitiveType.Code;
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
import org.eclipse.jdt.internal.compiler.problem.DefaultProblem;
import org.eclipse.jdt.internal.compiler.problem.ProblemSeverities;

import com.sun.source.tree.CaseTree.CaseKind;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.parser.Tokens.Comment;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCAnnotatedType;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
import com.sun.tools.javac.tree.JCTree.JCAnyPattern;
import com.sun.tools.javac.tree.JCTree.JCArrayAccess;
Expand Down Expand Up @@ -91,6 +91,7 @@
import com.sun.tools.javac.tree.JCTree.JCTypeApply;
import com.sun.tools.javac.tree.JCTree.JCTypeCast;
import com.sun.tools.javac.tree.JCTree.JCTypeIntersection;
import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
import com.sun.tools.javac.tree.JCTree.JCTypeUnion;
import com.sun.tools.javac.tree.JCTree.JCUnary;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
Expand Down Expand Up @@ -236,17 +237,14 @@ private AbstractTypeDeclaration convertClassDecl(JCClassDecl javacClassDecl, AST
String pack = jcfa.selected == null ? null : jcfa.selected.toString();
typeDeclaration.setSuperclass(convert(jcfa.name, pack));
}

}
}
if( this.ast.apiLevel != AST.JLS2_INTERNAL) {
if (javacClassDecl.getImplementsClause() != null) {
if (javacClassDecl.getImplementsClause() != null) {
if( this.ast.apiLevel != AST.JLS2_INTERNAL) {
javacClassDecl.getImplementsClause().stream()
.map(this::convertToType)
.forEach(typeDeclaration.superInterfaceTypes()::add);
}
} else {
if (javacClassDecl.getImplementsClause() != null) {
} else {
Iterator<JCExpression> it = javacClassDecl.getImplementsClause().iterator();
while(it.hasNext()) {
JCExpression next = it.next();
Expand All @@ -257,6 +255,15 @@ private AbstractTypeDeclaration convertClassDecl(JCClassDecl javacClassDecl, AST
}
}
}

if( javacClassDecl.getTypeParameters() != null ) {
Iterator<JCTypeParameter> i = javacClassDecl.getTypeParameters().iterator();
while(i.hasNext()) {
JCTypeParameter next = i.next();
typeDeclaration.typeParameters().add(convert(next));
}
}

if (javacClassDecl.getPermitsClause() != null) {
if( this.ast.apiLevel >= AST.JLS17_INTERNAL) {
javacClassDecl.getPermitsClause().stream()
Expand Down Expand Up @@ -335,6 +342,48 @@ private AbstractTypeDeclaration convertClassDecl(JCClassDecl javacClassDecl, AST
return res;
}

private TypeParameter convert(JCTypeParameter typeParameter) {
final TypeParameter typeParameter2 = new TypeParameter(this.ast);
final SimpleName simpleName = new SimpleName(this.ast);
simpleName.internalSetIdentifier(typeParameter.getName().toString());
int start = typeParameter.pos;
int end = typeParameter.pos + typeParameter.getName().length();
simpleName.setSourceRange(start, end - start + 1);
typeParameter2.setName(simpleName);
int annotationsStart = start;
// org.eclipse.jdt.internal.compiler.ast.Annotation[] annotations = typeParameter.annotations;
// if (annotations != null) {
// if (annotations[0] != null)
// annotationsStart = annotations[0].sourceStart;
// annotateTypeParameter(typeParameter2, typeParameter.annotations);
// }
// final TypeReference superType = typeParameter.type;
// end = typeParameter.declarationSourceEnd;
// if (superType != null) {
// Type type = convertType(superType);
// typeParameter2.typeBounds().add(type);
// end = type.getStartPosition() + type.getLength() - 1;
// }
// TypeReference[] bounds = typeParameter.bounds;
// if (bounds != null) {
// Type type = null;
// for (int index = 0, length = bounds.length; index < length; index++) {
// type = convertType(bounds[index]);
// typeParameter2.typeBounds().add(type);
// end = type.getStartPosition() + type.getLength() - 1;
// }
// }
// start = annotationsStart < typeParameter.declarationSourceStart ? annotationsStart : typeParameter.declarationSourceStart;
// end = retrieveClosingAngleBracketPosition(end);
typeParameter2.setSourceRange(start, end - start + 1);
// if (this.resolveBindings) {
// recordName(simpleName, typeParameter);
// recordNodes(typeParameter2, typeParameter);
// typeParameter2.resolveBinding();
// }
return typeParameter2;
}

private ASTNode convertBodyDeclaration(JCTree tree, ASTNode parent) {
if( parent instanceof AnnotationTypeDeclaration && tree instanceof JCMethodDecl methodDecl) {
return convertMethodInAnnotationTypeDecl(methodDecl, parent);
Expand Down Expand Up @@ -400,6 +449,7 @@ private MethodDeclaration convertMethodDecl(JCMethodDecl javac, ASTNode parent)
private VariableDeclaration convertVariableDeclaration(JCVariableDecl javac) {
// if (singleDecl) {
SingleVariableDeclaration res = this.ast.newSingleVariableDeclaration();
String z = javac.toString();
commonSettings(res, javac);
if (convert(javac.getName()) instanceof SimpleName simpleName) {
res.setName(simpleName);
Expand All @@ -420,6 +470,12 @@ private VariableDeclaration convertVariableDeclaration(JCVariableDecl javac) {
res.setExtraDimensions(countDimensions(jcatt));
}
}
} else if ( (javac.mods.flags & VARARGS) != 0) {
// We have varity
if( javac.getType() instanceof JCArrayTypeTree arr) {
res.setType(convertToType(arr.elemtype));
}
res.setVarargs(true);
} else {
// the array dimensions are part of the type
if (javac.getType() != null) {
Expand Down

0 comments on commit 82fa7f2

Please sign in to comment.