Skip to content

Commit

Permalink
Work on SortCompilationUnitElementsTests test004 and others, WIP
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 6, 2024
1 parent 932b708 commit 33fa8c1
Showing 1 changed file with 73 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
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.JCAnnotation;
Expand Down Expand Up @@ -208,14 +209,23 @@ private AbstractTypeDeclaration convertClassDecl(JCClassDecl javacClassDecl, AST
default -> throw new IllegalStateException();
};
commonSettings(res, javacClassDecl);
res.setName((SimpleName)convert(javacClassDecl.getSimpleName()));
SimpleName simpName = (SimpleName)convert(javacClassDecl.getSimpleName());
if( simpName != null )
res.setName(simpName);
if( this.ast.apiLevel != AST.JLS2_INTERNAL) {
res.modifiers().addAll(convert(javacClassDecl.mods));
}
if (res instanceof TypeDeclaration typeDeclaration) {
if (javacClassDecl.getExtendsClause() != null) {
if( this.ast.apiLevel != AST.JLS2_INTERNAL) {
typeDeclaration.setSuperclassType(convertToType(javacClassDecl.getExtendsClause()));
} else {
JCExpression e = javacClassDecl.getExtendsClause();
if( e instanceof JCFieldAccess jcfa) {
String pack = jcfa.selected == null ? null : jcfa.selected.toString();
typeDeclaration.setSuperclass(convert(jcfa.name, pack));
}

}
}
if( this.ast.apiLevel != AST.JLS2_INTERNAL) {
Expand All @@ -224,6 +234,17 @@ private AbstractTypeDeclaration convertClassDecl(JCClassDecl javacClassDecl, AST
.map(this::convertToType)
.forEach(typeDeclaration.superInterfaceTypes()::add);
}
} else {
if (javacClassDecl.getImplementsClause() != null) {
Iterator<JCExpression> it = javacClassDecl.getImplementsClause().iterator();
while(it.hasNext()) {
JCExpression next = it.next();
if( next instanceof JCFieldAccess jcfa ) {
String pack = jcfa.selected == null ? null : jcfa.selected.toString();
typeDeclaration.superInterfaces().add(convert(jcfa.name, pack));
}
}
}
}
if (javacClassDecl.getPermitsClause() != null) {
if( this.ast.apiLevel >= AST.JLS17_INTERNAL) {
Expand Down Expand Up @@ -342,7 +363,8 @@ private MethodDeclaration convertMethodDecl(JCMethodDecl javac, ASTNode parent)
if( this.ast.apiLevel != AST.JLS2_INTERNAL) {
res.modifiers().addAll(convert(javac.getModifiers()));
}
res.setConstructor(Objects.equals(javac.getName(), Names.instance(this.context).init));
boolean isConstructor = Objects.equals(javac.getName(), Names.instance(this.context).init);
res.setConstructor(isConstructor);
if (!res.isConstructor()) {
res.setName((SimpleName)convert(javac.getName()));
} else {
Expand All @@ -353,6 +375,8 @@ private MethodDeclaration convertMethodDecl(JCMethodDecl javac, ASTNode parent)
if (javac.getReturnType() != null) {
if( this.ast.apiLevel != AST.JLS2_INTERNAL) {
res.setReturnType2(convertToType(javac.getReturnType()));
} else {
res.internalSetReturnType(convertToType(javac.getReturnType()));
}
}
javac.getParameters().stream().map(this::convertVariableDeclaration).forEach(res.parameters()::add);
Expand All @@ -371,6 +395,21 @@ private VariableDeclaration convertVariableDeclaration(JCVariableDecl javac) {
}
if( this.ast.apiLevel != AST.JLS2_INTERNAL) {
res.modifiers().addAll(convert(javac.getModifiers()));
} else {
int flags = 0;
if( (javac.mods.flags & Flags.PUBLIC) > 0) flags += Flags.PUBLIC;
if( (javac.mods.flags & Flags.PRIVATE) > 0) flags += Flags.PRIVATE;
if( (javac.mods.flags & Flags.PROTECTED) > 0) flags += Flags.PROTECTED;
if( (javac.mods.flags & Flags.STATIC) > 0) flags += Flags.STATIC;
if( (javac.mods.flags & Flags.FINAL) > 0) flags += Flags.FINAL;
if( (javac.mods.flags & Flags.SYNCHRONIZED) > 0) flags += Flags.SYNCHRONIZED;
if( (javac.mods.flags & Flags.VOLATILE) > 0) flags += Flags.VOLATILE;
if( (javac.mods.flags & Flags.TRANSIENT) > 0) flags += Flags.TRANSIENT;
if( (javac.mods.flags & Flags.NATIVE) > 0) flags += Flags.NATIVE;
if( (javac.mods.flags & Flags.INTERFACE) > 0) flags += Flags.INTERFACE;
if( (javac.mods.flags & Flags.ABSTRACT) > 0) flags += Flags.ABSTRACT;
if( (javac.mods.flags & Flags.STRICTFP) > 0) flags += Flags.STRICTFP;
res.internalSetModifiers(flags);
}
if (javac.getType() != null) {
res.setType(convertToType(javac.getType()));
Expand Down Expand Up @@ -757,6 +796,10 @@ private Expression convertLiteral(JCLiteral literal) {
}

private Statement convertStatement(JCStatement javac) {
return convertStatement(javac, null);
}

private Statement convertStatement(JCStatement javac, ASTNode parent) {
if (javac instanceof JCReturn returnStatement) {
ReturnStatement res = this.ast.newReturnStatement();
commonSettings(res, javac);
Expand Down Expand Up @@ -911,6 +954,11 @@ private Statement convertStatement(JCStatement javac) {
res.setExpression(convertExpression(jcAssert.getCondition()));
return res;
}
if (javac instanceof JCClassDecl jcclass) {
TypeDeclarationStatement res = this.ast.newTypeDeclarationStatement(convertClassDecl(jcclass, parent));
commonSettings(res, javac);
return res;
}
throw new UnsupportedOperationException("Missing support to convert " + javac + "of type " + javac.getClass().getName());
}

Expand All @@ -934,9 +982,12 @@ private Block convertBlock(JCBlock javac) {
Block res = this.ast.newBlock();
commonSettings(res, javac);
if (javac.getStatements() != null) {
javac.getStatements().stream()
.map(this::convertStatement)
.forEach(res.statements()::add);
for( Iterator i = javac.getStatements().iterator(); i.hasNext();) {
Statement s = convertStatement((JCStatement)i.next());
if( s != null ) {
res.statements().add(s);
}
}
}
return res;
}
Expand Down Expand Up @@ -987,9 +1038,11 @@ private Type convertToType(JCTree javac) {
return res;
}
if (javac instanceof JCFieldAccess qualified) {
QualifiedType res = this.ast.newQualifiedType(convertToType(qualified.getExpression()), (SimpleName)convert(qualified.name));
commonSettings(res, qualified);
return res;
if( this.ast.apiLevel != AST.JLS2_INTERNAL ) {
QualifiedType res = this.ast.newQualifiedType(convertToType(qualified.getExpression()), (SimpleName)convert(qualified.name));
commonSettings(res, qualified);
return res;
}
}
if (javac instanceof JCPrimitiveTypeTree primitiveTypeTree) {
PrimitiveType res = this.ast.newPrimitiveType(convert(primitiveTypeTree.getPrimitiveTypeKind()));
Expand Down Expand Up @@ -1084,14 +1137,25 @@ private Name convert(com.sun.tools.javac.util.Name javac) {
}
int lastDot = javac.lastIndexOf((byte)'.');
if (lastDot < 0) {
String ts = javac.toString();
return this.ast.newSimpleName(javac.toString());
} else {
return this.ast.newQualifiedName(convert(javac.subName(0, lastDot)), (SimpleName)convert(javac.subName(lastDot + 1, javac.length() - 1)));
}
// position is set later, in FixPositions, as computing them depends on the sibling
}

private Name convert(com.sun.tools.javac.util.Name javac, String selected) {
if (javac == null || Objects.equals(javac, Names.instance(this.context).error) || Objects.equals(javac, Names.instance(this.context).empty)) {
return null;
}
if (selected == null) {
return this.ast.newSimpleName(javac.toString());
} else {
return this.ast.newQualifiedName(this.ast.newName(selected), this.ast.newSimpleName(javac.toString()));
}
// position is set later, in FixPositions, as computing them depends on the sibling
}

public org.eclipse.jdt.core.dom.Comment convert(Comment javac, int pos, int endPos) {
org.eclipse.jdt.core.dom.Comment jdt = switch (javac.getStyle()) {
case LINE -> this.ast.newLineComment();
Expand Down

0 comments on commit 33fa8c1

Please sign in to comment.