Skip to content

Commit

Permalink
WIP - fix CreateMembersTest suite
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 Feb 22, 2024
1 parent 5660043 commit 3076098
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ public CompilationUnit toCompilationUnit(org.eclipse.jdt.internal.compiler.env.I
if (initialNeedsToResolveBinding) {
res.getPackage().resolveBinding();
}
// For comparison
CompilationUnit res2 = CompilationUnitResolver.FACADE.toCompilationUnit(sourceUnit, initialNeedsToResolveBinding, project, classpaths, nodeSearcher, apiLevel, compilerOptions, typeRootWorkingCopyOwner, typeRootWorkingCopyOwner, flags, monitor);

String res1a = res.toString();
String res2a = res2.toString();
return res;
}

Expand Down Expand Up @@ -195,7 +200,12 @@ private List<File> classpathEntriesToFiles(JavaProject project, Predicate<IClass
private void configurePaths(JavaProject javaProject, Context context) {
JavacFileManager fileManager = (JavacFileManager)context.get(JavaFileManager.class);
try {
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, List.of(javaProject.getProject().getParent().findMember(javaProject.getOutputLocation()).getLocation().toFile()));
IResource member = javaProject.getProject().getParent().findMember(javaProject.getOutputLocation());
if( member != null ) {
File f = member.getLocation().toFile();
List l = List.of(member);
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, l);
}
fileManager.setLocation(StandardLocation.SOURCE_PATH, classpathEntriesToFiles(javaProject, entry -> entry.getEntryKind() == IClasspathEntry.CPE_SOURCE));
fileManager.setLocation(StandardLocation.CLASS_PATH, classpathEntriesToFiles(javaProject, entry -> entry.getEntryKind() != IClasspathEntry.CPE_SOURCE));
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private Name toName(JCTree expression) {

private AbstractTypeDeclaration convertClassDecl(JCClassDecl javacClassDecl) {
AbstractTypeDeclaration res = switch (javacClassDecl.getKind()) {
case ANNOTATED_TYPE -> this.ast.newAnnotationTypeDeclaration();
case ANNOTATION_TYPE -> this.ast.newAnnotationTypeDeclaration();
case ENUM -> this.ast.newEnumDeclaration();
case RECORD -> this.ast.newRecordDeclaration();
case INTERFACE -> {
Expand Down Expand Up @@ -236,6 +236,50 @@ private AbstractTypeDeclaration convertClassDecl(JCClassDecl javacClassDecl) {
// tagElement.fragments().add(textElement);
// doc.tags().add(tagElement);
// res.setJavadoc(doc);
} else if (res instanceof EnumDeclaration enumDecl) {
List enumStatements= enumDecl.enumConstants();
if (javacClassDecl.getMembers() != null) {
javacClassDecl.getMembers().stream()
.map(this::convertEnumConstantDeclaration)
.filter(t -> t != null)
.forEach(enumStatements::add);
}

List bodyDecl = enumDecl.bodyDeclarations();
if (javacClassDecl.getMembers() != null) {
javacClassDecl.getMembers().stream()
.map(this::convertEnumFieldDeclaration)
.filter(t -> t != null)
.forEach(bodyDecl::add);
}
} else if (res instanceof AnnotationTypeDeclaration annotDecl) {
//setModifiers(annotationTypeMemberDeclaration2, annotationTypeMemberDeclaration);
final SimpleName name = new SimpleName(this.ast);
name.internalSetIdentifier(new String(annotDecl.typeName.toString()));
int start = annotDecl.getStartPosition();
int end = start + annotDecl.getLength();
name.setSourceRange(start, end - start + 1);
res.setName(name);
if( javacClassDecl.defs != null ) {
javacClassDecl.defs.stream().map(this::convertBodyDeclaration).filter(t -> t != null)
.forEach(res.bodyDeclarations::add);
}

// org.eclipse.jdt.internal.compiler.ast.TypeReference typeReference = annotDecl.get
// if (typeReference != null) {
// Type returnType = convertType(typeReference);
// setTypeForMethodDeclaration(annotationTypeMemberDeclaration2, returnType, 0);
// }
// int declarationSourceStart = annotationTypeMemberDeclaration.declarationSourceStart;
// int declarationSourceEnd = annotationTypeMemberDeclaration.bodyEnd;
// annotationTypeMemberDeclaration2.setSourceRange(declarationSourceStart, declarationSourceEnd - declarationSourceStart + 1);
// // The javadoc comment is now got from list store in compilation unit declaration
// convert(annotationTypeMemberDeclaration.javadoc, annotationTypeMemberDeclaration2);
// org.eclipse.jdt.internal.compiler.ast.Expression memberValue = annotationTypeMemberDeclaration.defaultValue;
// if (memberValue != null) {
// annotationTypeMemberDeclaration2.setDefault(convert(memberValue));
// }

}
// TODO Javadoc
return res;
Expand Down Expand Up @@ -1101,4 +1145,29 @@ private int findPositionOfText(String text, ASTNode in, List<ASTNode> excluding)
}
}

private EnumConstantDeclaration convertEnumConstantDeclaration(JCTree var) {
EnumConstantDeclaration enumConstantDeclaration = null;
if( var instanceof JCVariableDecl enumConstant ) {
if( enumConstant.getType() instanceof JCIdent) {
enumConstantDeclaration = new EnumConstantDeclaration(this.ast);
final SimpleName typeName = new SimpleName(this.ast);
typeName.internalSetIdentifier(enumConstant.getName().toString());
int start = enumConstant.getStartPosition();
int end = enumConstant.getEndPosition(this.javacCompilationUnit.endPositions);
enumConstantDeclaration.setSourceRange(start, end-start);
enumConstantDeclaration.setName(typeName);
}
}
return enumConstantDeclaration;
}

private FieldDeclaration convertEnumFieldDeclaration(JCTree var) {
if( var instanceof JCVariableDecl field ) {
if( !(field.getType() instanceof JCIdent)) {
return convertFieldDeclaration(field);
}
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,7 @@ private CompilationUnit internalCreateCompilationUnit(IProgressMonitor monitor)
}

CompilationUnit result = this.unitResolver.toCompilationUnit(sourceUnit, needToResolveBindings, this.project, getClasspath(), searcher, this.apiLevel, this.compilerOptions, this.workingCopyOwner, wcOwner, flags, monitor);

result.setTypeRoot(this.typeRoot);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,11 @@ void setLineEndTable(int[] lineEndTable) {
this.lineEndTable = lineEndTable;
}

// FOR TESTING ONLY, DELETE ME
int[] getLineEndTable() {
return this.lineEndTable;
}

/**
* Sets or clears the module declaration of this compilation unit
* node to the given module declaration node.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@
import org.eclipse.jdt.internal.core.util.DOMFinder;

@SuppressWarnings({ "rawtypes", "unchecked" })
class CompilationUnitResolver extends Compiler {
private static final class ECJCompilationUnitResolver implements ICompilationUnitResolver {
public class CompilationUnitResolver extends Compiler {

static final class ECJCompilationUnitResolver implements ICompilationUnitResolver {

@Override
public void resolve(String[] sourceFilePaths, String[] encodings, String[] bindingKeys,
FileASTRequestor requestor, int apiLevel, Map<String, String> compilerOptions, List<Classpath> classpath,
Expand Down Expand Up @@ -173,7 +173,7 @@ public CompilationUnit toCompilationUnit(org.eclipse.jdt.internal.compiler.env.I
}
}
}

public static final ECJCompilationUnitResolver FACADE = new ECJCompilationUnitResolver();

public static final int RESOLVE_BINDING = 0x1;
Expand Down

0 comments on commit 3076098

Please sign in to comment.