From 7d5ca4d34c3267a0f49084b31147c12fd0922f53 Mon Sep 17 00:00:00 2001 From: Sarika Sinha Date: Tue, 7 Jan 2020 16:20:42 +0530 Subject: Bug 553154 - [14] Records - DOM AST Support Change-Id: I17f6946382125b5d4625b7f5d6d78d99a3166ee8 --- .../compiler/ast/AbstractMethodDeclaration.java | 7 +- .../internal/compiler/ast/MethodDeclaration.java | 3 +- .../dom/org/eclipse/jdt/core/dom/ASTConverter.java | 82 ++++++++++++++++++++++ 3 files changed, 89 insertions(+), 3 deletions(-) diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AbstractMethodDeclaration.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AbstractMethodDeclaration.java index 0648075a5b..2d41fd440b 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AbstractMethodDeclaration.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AbstractMethodDeclaration.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2019 IBM Corporation and others. + * Copyright (c) 2000, 2020 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -76,6 +76,7 @@ public abstract class AbstractMethodDeclaration public int bodyStart; public int bodyEnd = -1; public CompilationResult compilationResult; + protected boolean isImplicit; // used in Java 14 Records AbstractMethodDeclaration(CompilationResult compilationResult){ this.compilationResult = compilationResult; @@ -464,6 +465,10 @@ public abstract class AbstractMethodDeclaration return (this.modifiers & ClassFileConstants.AccStatic) != 0; } + public boolean isImplicit() { + return this.isImplicit; + } + /** * Fill up the method body with statement * @param parser diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/MethodDeclaration.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/MethodDeclaration.java index a3d7db6f4b..62abff4d7a 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/MethodDeclaration.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/MethodDeclaration.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2019 IBM Corporation and others. + * Copyright (c) 2000, 2020 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -61,7 +61,6 @@ public class MethodDeclaration extends AbstractMethodDeclaration { public TypeReference returnType; public TypeParameter[] typeParameters; - public boolean isImplicit; // used in Java 14 Records /** * MethodDeclaration constructor comment. diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java index 3d9c559fc1..f1b0e529cc 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java @@ -224,6 +224,88 @@ class ASTConverter { // Convert javadoc convert(typeDeclaration.javadoc, typeDecl); } + + protected void buildBodyDeclarations( + org.eclipse.jdt.internal.compiler.ast.TypeDeclaration typeDeclaration, + RecordDeclaration recordDeclaration, + boolean isInterface) { + // add body declaration in the lexical order + org.eclipse.jdt.internal.compiler.ast.TypeDeclaration[] members = typeDeclaration.memberTypes; + org.eclipse.jdt.internal.compiler.ast.FieldDeclaration[] fields = typeDeclaration.fields; + org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration[] methods = typeDeclaration.methods; + + int fieldsLength = fields == null? 0 : fields.length; + int methodsLength = methods == null? 0 : methods.length; + int membersLength = members == null ? 0 : members.length; + int fieldsIndex = 0; + int methodsIndex = 0; + int membersIndex = 0; + + while ((fieldsIndex < fieldsLength) + || (membersIndex < membersLength) + || (methodsIndex < methodsLength)) { + org.eclipse.jdt.internal.compiler.ast.FieldDeclaration nextFieldDeclaration = null; + org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration nextMethodDeclaration = null; + org.eclipse.jdt.internal.compiler.ast.TypeDeclaration nextMemberDeclaration = null; + + int position = Integer.MAX_VALUE; + int nextDeclarationType = -1; + if (fieldsIndex < fieldsLength) { + nextFieldDeclaration = fields[fieldsIndex]; + if (!nextFieldDeclaration.isARecordComponent) { + if (nextFieldDeclaration.declarationSourceStart < position) { + position = nextFieldDeclaration.declarationSourceStart; + nextDeclarationType = 0; // FIELD + } + } else { + fieldsIndex++; + } + + } + if (methodsIndex < methodsLength) { + nextMethodDeclaration = methods[methodsIndex]; + if (!nextMethodDeclaration.isImplicit()) { + if (nextMethodDeclaration.declarationSourceStart < position) { + position = nextMethodDeclaration.declarationSourceStart; + nextDeclarationType = 1; // METHOD + } + + } else { + methodsIndex++; + } + + } + if (membersIndex < membersLength) { + nextMemberDeclaration = members[membersIndex]; + if (nextMemberDeclaration.declarationSourceStart < position) { + position = nextMemberDeclaration.declarationSourceStart; + nextDeclarationType = 2; // MEMBER + } + } + switch (nextDeclarationType) { + case 0 : + checkAndAddMultipleFieldDeclaration(fields, fieldsIndex, recordDeclaration.bodyDeclarations()); + fieldsIndex++; + break; + case 1 : + methodsIndex++; + if (!nextMethodDeclaration.isDefaultConstructor() && !nextMethodDeclaration.isClinit()) { + recordDeclaration.bodyDeclarations().add(convert(isInterface, nextMethodDeclaration)); + } + break; + case 2 : + membersIndex++; + ASTNode node = convert(nextMemberDeclaration); + if (node == null) { + recordDeclaration.setFlags(recordDeclaration.getFlags() | ASTNode.MALFORMED); + } else { + recordDeclaration.bodyDeclarations().add(node); + } + } + } + // Convert javadoc + convert(typeDeclaration.javadoc, recordDeclaration); + } protected void buildBodyDeclarations(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration enumDeclaration2, EnumDeclaration enumDeclaration) { // add body declaration in the lexical order -- cgit v1.2.3