Skip to content

Commit e27f16d

Browse files
akurtakovRob Stryker
authored andcommitted
Remove pre JLS 8 convertions
Not needed as neither Javac nor ECJ nor JDT makes use of anything pre Java 8 nowadays. VariableDeclaration.extraArrayDeclarataion is not supported in Java 8+ but was used without guards which shouldn't have happened.
1 parent 0f287b3 commit e27f16d

File tree

1 file changed

+84
-143
lines changed

1 file changed

+84
-143
lines changed

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacConverter.java

Lines changed: 84 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.List;
2323
import java.util.Map;
2424
import java.util.Objects;
25-
import java.util.Optional;
2625
import java.util.OptionalInt;
2726
import java.util.PriorityQueue;
2827
import java.util.Set;
@@ -747,7 +746,7 @@ private TypeParameter convert(JCTypeParameter typeParameter) {
747746
ret.typeBounds().add(type);
748747
end = typeParameter.getEndPosition(this.javacCompilationUnit.endPositions);
749748
}
750-
if (typeParameter.getAnnotations() != null && this.ast.apiLevel() >= AST.JLS8_INTERNAL) {
749+
if (typeParameter.getAnnotations() != null) {
751750
typeParameter.getAnnotations().stream()
752751
.map(this::convert)
753752
.forEach(ret.modifiers()::add);
@@ -943,11 +942,7 @@ private MethodDeclaration convertMethodDecl(JCMethodDecl javac, ASTNode parent)
943942
var dims = convertDimensionsAfterPosition(retTypeTree, javac.pos);
944943
if (!dims.isEmpty() && retTypeTree.pos > javac.pos ) {
945944
// The array dimensions are part of the variable name
946-
if( this.ast.apiLevel < AST.JLS8_INTERNAL) {
947-
res.setExtraDimensions(dims.size());
948-
} else {
949-
res.extraDimensions().addAll(dims);
950-
}
945+
res.extraDimensions().addAll(dims);
951946
retType = convertToType(unwrapDimensions(retTypeTree, dims.size()));
952947
}
953948

@@ -994,10 +989,9 @@ private MethodDeclaration convertMethodDecl(JCMethodDecl javac, ASTNode parent)
994989
boolean isInterface = td instanceof TypeDeclaration td1 && td1.isInterface();
995990
long modFlags = javac.getModifiers() == null ? 0 : javac.getModifiers().flags;
996991
boolean isAbstractOrNative = (modFlags & (Flags.ABSTRACT | Flags.NATIVE)) != 0;
997-
boolean isJlsBelow8 = this.ast.apiLevel < AST.JLS8_INTERNAL;
998992
boolean isJlsAbove8 = this.ast.apiLevel > AST.JLS8_INTERNAL;
999993
long flagsToCheckForAboveJLS8 = Flags.STATIC | Flags.DEFAULT | (isJlsAbove8 ? Flags.PRIVATE : 0);
1000-
boolean notAllowed = (isAbstractOrNative || (isInterface && (isJlsBelow8 || (modFlags & flagsToCheckForAboveJLS8) == 0)));
994+
boolean notAllowed = (isAbstractOrNative || (isInterface && (modFlags & flagsToCheckForAboveJLS8) == 0));
1001995
if (notAllowed) {
1002996
res.setFlags(res.getFlags() | ASTNode.MALFORMED);
1003997
}
@@ -1014,13 +1008,9 @@ private MethodDeclaration convertMethodDecl(JCMethodDecl javac, ASTNode parent)
10141008
}
10151009

10161010
for (JCExpression thrown : javac.getThrows()) {
1017-
if (this.ast.apiLevel < AST.JLS8_INTERNAL) {
1018-
res.thrownExceptions().add(toName(thrown));
1019-
} else {
1020-
Type type = convertToType(thrown);
1021-
if (type != null) {
1022-
res.thrownExceptionTypes().add(type);
1023-
}
1011+
Type type = convertToType(thrown);
1012+
if (type != null) {
1013+
res.thrownExceptionTypes().add(type);
10241014
}
10251015
}
10261016
if( malformed ) {
@@ -1084,11 +1074,7 @@ private VariableDeclaration convertVariableDeclaration(JCVariableDecl javac) {
10841074
List<Dimension> dims = convertDimensionsAfterPosition(javac.getType(), javac.getPreferredPosition()); // +1 to exclude part of the type declared before name
10851075
if(!dims.isEmpty() ) {
10861076
// Some of the array dimensions are part of the variable name
1087-
if( this.ast.apiLevel < AST.JLS8_INTERNAL) {
1088-
res.setExtraDimensions(dims.size()); // the type is 1-dim array
1089-
} else {
1090-
res.extraDimensions().addAll(dims);
1091-
}
1077+
res.extraDimensions().addAll(dims);
10921078
type = unwrapDimensions(type, dims.size());
10931079
}
10941080

@@ -1128,11 +1114,7 @@ private VariableDeclarationFragment createVariableDeclarationFragment(JCVariable
11281114
fragment.setName(simpleName);
11291115
}
11301116
var dims = convertDimensionsAfterPosition(javac.getType(), fragmentStart);
1131-
if( this.ast.apiLevel < AST.JLS8_INTERNAL) {
1132-
fragment.setExtraDimensions(dims.size());
1133-
} else {
1134-
fragment.extraDimensions().addAll(dims);
1135-
}
1117+
fragment.extraDimensions().addAll(dims);
11361118
if (javac.getInitializer() != null) {
11371119
Expression initializer = convertExpression(javac.getInitializer());
11381120
if( initializer != null ) {
@@ -1637,69 +1619,59 @@ private Expression convertExpressionImpl(JCExpression javac) {
16371619
ArrayType arrayType;
16381620
if (type instanceof ArrayType childArrayType) {
16391621
arrayType = childArrayType;
1640-
if( this.ast.apiLevel >= AST.JLS8_INTERNAL) {
1641-
var extraDimensions = jcNewArray.getDimAnnotations().stream()
1642-
.map(annotations -> annotations.stream().map(this::convert).toList())
1643-
.map(annotations -> {
1644-
Dimension dim = this.ast.newDimension();
1645-
dim.annotations().addAll(annotations);
1646-
int startOffset = annotations.stream().mapToInt(Annotation::getStartPosition).min().orElse(-1);
1647-
int endOffset = annotations.stream().mapToInt(ann -> ann.getStartPosition() + ann.getLength()).max().orElse(-1);
1648-
dim.setSourceRange(startOffset, endOffset - startOffset);
1649-
return dim;
1650-
})
1651-
.toList();
1652-
if (arrayType.dimensions().isEmpty()) {
1653-
arrayType.dimensions().addAll(extraDimensions);
1654-
} else {
1655-
var lastDimension = arrayType.dimensions().removeFirst();
1656-
arrayType.dimensions().addAll(extraDimensions);
1657-
arrayType.dimensions().add(lastDimension);
1658-
}
1659-
int totalRequiredDims = countDimensions(jcNewArray.getType()) + 1;
1660-
int totalCreated = arrayType.dimensions().size();
1661-
if( totalCreated < totalRequiredDims) {
1662-
int endPos = jcNewArray.getEndPosition(this.javacCompilationUnit.endPositions);
1663-
int startPos = jcNewArray.getStartPosition();
1664-
String raw = this.rawText.substring(startPos, endPos);
1665-
for( int i = 0; i < totalRequiredDims; i++ ) {
1666-
int absoluteEndChar = startPos + ordinalIndexOf(raw, "]", i+1);
1667-
int absoluteEnd = absoluteEndChar + 1;
1668-
int absoluteStart = startPos + ordinalIndexOf(raw, "[", i+1);
1669-
boolean found = false;
1670-
if( absoluteEnd != -1 && absoluteStart != -1 ) {
1671-
for( int j = 0; j < totalCreated && !found; j++ ) {
1672-
Dimension d = (Dimension)arrayType.dimensions().get(j);
1673-
if( d.getStartPosition() == absoluteStart && (d.getStartPosition() + d.getLength()) == absoluteEnd) {
1674-
found = true;
1675-
}
1676-
}
1677-
if( !found ) {
1678-
// Need to make a new one
1679-
Dimension d = this.ast.newDimension();
1680-
d.setSourceRange(absoluteStart, absoluteEnd - absoluteStart);
1681-
arrayType.dimensions().add(i, d);
1682-
totalCreated++;
1622+
var extraDimensions = jcNewArray.getDimAnnotations().stream()
1623+
.map(annotations -> annotations.stream().map(this::convert).toList())
1624+
.map(annotations -> {
1625+
Dimension dim = this.ast.newDimension();
1626+
dim.annotations().addAll(annotations);
1627+
int startOffset = annotations.stream().mapToInt(Annotation::getStartPosition).min().orElse(-1);
1628+
int endOffset = annotations.stream().mapToInt(ann -> ann.getStartPosition() + ann.getLength()).max().orElse(-1);
1629+
dim.setSourceRange(startOffset, endOffset - startOffset);
1630+
return dim;
1631+
})
1632+
.toList();
1633+
if (arrayType.dimensions().isEmpty()) {
1634+
arrayType.dimensions().addAll(extraDimensions);
1635+
} else {
1636+
var lastDimension = arrayType.dimensions().removeFirst();
1637+
arrayType.dimensions().addAll(extraDimensions);
1638+
arrayType.dimensions().add(lastDimension);
1639+
}
1640+
int totalRequiredDims = countDimensions(jcNewArray.getType()) + 1;
1641+
int totalCreated = arrayType.dimensions().size();
1642+
if( totalCreated < totalRequiredDims) {
1643+
int endPos = jcNewArray.getEndPosition(this.javacCompilationUnit.endPositions);
1644+
int startPos = jcNewArray.getStartPosition();
1645+
String raw = this.rawText.substring(startPos, endPos);
1646+
for( int i = 0; i < totalRequiredDims; i++ ) {
1647+
int absoluteEndChar = startPos + ordinalIndexOf(raw, "]", i+1);
1648+
int absoluteEnd = absoluteEndChar + 1;
1649+
int absoluteStart = startPos + ordinalIndexOf(raw, "[", i+1);
1650+
boolean found = false;
1651+
if( absoluteEnd != -1 && absoluteStart != -1 ) {
1652+
for( int j = 0; j < totalCreated && !found; j++ ) {
1653+
Dimension d = (Dimension)arrayType.dimensions().get(j);
1654+
if( d.getStartPosition() == absoluteStart && (d.getStartPosition() + d.getLength()) == absoluteEnd) {
1655+
found = true;
16831656
}
16841657
}
1658+
if( !found ) {
1659+
// Need to make a new one
1660+
Dimension d = this.ast.newDimension();
1661+
d.setSourceRange(absoluteStart, absoluteEnd - absoluteStart);
1662+
arrayType.dimensions().add(i, d);
1663+
totalCreated++;
1664+
}
16851665
}
16861666
}
1687-
} else {
1688-
// JLS < 8, just wrap underlying type
1689-
arrayType = this.ast.newArrayType(childArrayType);
16901667
}
16911668
} else if(jcNewArray.dims != null && jcNewArray.dims.size() > 0 ){
16921669
// Child is not array type
16931670
arrayType = this.ast.newArrayType(type);
16941671
int dims = jcNewArray.dims.size();
16951672
for( int i = 0; i < dims - 1; i++ ) {
1696-
if( this.ast.apiLevel >= AST.JLS8_INTERNAL) {
1697-
// TODO, this dimension needs source range
1698-
arrayType.dimensions().addFirst(this.ast.newDimension());
1699-
} else {
1700-
// JLS < 8, wrap underlying
1701-
arrayType = this.ast.newArrayType(arrayType);
1702-
}
1673+
// TODO, this dimension needs source range
1674+
arrayType.dimensions().addFirst(this.ast.newDimension());
17031675
}
17041676
} else {
17051677
// Child is not array type, and 0 dims for underlying
@@ -2025,31 +1997,23 @@ private List<Dimension> convertDimensionsAfterPosition(JCTree tree, int pos) {
20251997
do {
20261998
if( elem.pos >= pos) {
20271999
if (elem instanceof JCArrayTypeTree arrayType) {
2028-
if (this.ast.apiLevel < AST.JLS8_INTERNAL) {
2029-
res.add(null);
2030-
} else {
2031-
Dimension dimension = this.ast.newDimension();
2032-
res.add(dimension);
2033-
// Would be better to use a Tokenizer here that is capable of skipping comments
2034-
int startPosition = this.rawText.indexOf('[', arrayType.pos);
2035-
int endPosition = this.rawText.indexOf(']', startPosition);
2036-
dimension.setSourceRange(startPosition, endPosition - startPosition + 1);
2037-
}
2000+
Dimension dimension = this.ast.newDimension();
2001+
res.add(dimension);
2002+
// Would be better to use a Tokenizer here that is capable of skipping comments
2003+
int startPosition = this.rawText.indexOf('[', arrayType.pos);
2004+
int endPosition = this.rawText.indexOf(']', startPosition);
2005+
dimension.setSourceRange(startPosition, endPosition - startPosition + 1);
20382006
elem = arrayType.getType();
20392007
} else if (elem instanceof JCAnnotatedType annotated && annotated.getUnderlyingType() instanceof JCArrayTypeTree arrayType) {
2040-
if (this.ast.apiLevel < AST.JLS8_INTERNAL) {
2041-
res.add(null);
2042-
} else {
2043-
Dimension dimension = this.ast.newDimension();
2044-
annotated.getAnnotations().stream()
2045-
.map(this::convert)
2046-
.forEach(dimension.annotations()::add);
2047-
// Would be better to use a Tokenizer here that is capable of skipping comments
2048-
int startPosition = this.rawText.indexOf('[', arrayType.pos);
2049-
int endPosition = this.rawText.indexOf(']', startPosition);
2050-
dimension.setSourceRange(startPosition, endPosition - startPosition + 1);
2051-
res.add(dimension);
2052-
}
2008+
Dimension dimension = this.ast.newDimension();
2009+
annotated.getAnnotations().stream()
2010+
.map(this::convert)
2011+
.forEach(dimension.annotations()::add);
2012+
// Would be better to use a Tokenizer here that is capable of skipping comments
2013+
int startPosition = this.rawText.indexOf('[', arrayType.pos);
2014+
int endPosition = this.rawText.indexOf(']', startPosition);
2015+
dimension.setSourceRange(startPosition, endPosition - startPosition + 1);
2016+
res.add(dimension);
20532017
elem = arrayType.getType();
20542018
} else {
20552019
elem = null;
@@ -2336,9 +2300,7 @@ private Statement convertStatement(JCStatement javac, ASTNode parent) {
23362300
if (jcVariableDecl.vartype != null) {
23372301
if( jcVariableDecl.vartype instanceof JCArrayTypeTree jcatt) {
23382302
int extraDims = 0;
2339-
if( fragment.extraArrayDimensions > 0 ) {
2340-
extraDims = fragment.extraArrayDimensions;
2341-
} else if( this.ast.apiLevel > AST.JLS4_INTERNAL && fragment.extraDimensions() != null && fragment.extraDimensions().size() > 0 ) {
2303+
if(fragment.extraDimensions() != null && fragment.extraDimensions().size() > 0 ) {
23422304
extraDims = fragment.extraDimensions().size();
23432305
}
23442306
res.setType(convertToType(unwrapDimensions(jcatt, extraDims)));
@@ -2646,9 +2608,7 @@ private Expression convertStatementToExpression(JCStatement javac, ASTNode paren
26462608
if (javac instanceof JCVariableDecl jcvd && jcvd.vartype != null) {
26472609
if( jcvd.vartype instanceof JCArrayTypeTree jcatt) {
26482610
int extraDims = 0;
2649-
if( fragment.extraArrayDimensions > 0 ) {
2650-
extraDims = fragment.extraArrayDimensions;
2651-
} else if( this.ast.apiLevel > AST.JLS4_INTERNAL && fragment.extraDimensions() != null && fragment.extraDimensions().size() > 0 ) {
2611+
if(fragment.extraDimensions() != null && fragment.extraDimensions().size() > 0 ) {
26522612
extraDims = fragment.extraDimensions().size();
26532613
}
26542614
jdtVariableDeclarationExpression.setType(convertToType(unwrapDimensions(jcatt, extraDims)));
@@ -2726,11 +2686,9 @@ private Expression convertTryResource(JCTree javac, ASTNode parent) {
27262686
initializer.delete();
27272687
fragment.setInitializer(initializer);
27282688
}
2729-
if (parent.getAST().apiLevel() > AST.JLS4) {
2730-
for (Dimension extraDimension : (List<Dimension>)single.extraDimensions()) {
2731-
extraDimension.delete();
2732-
fragment.extraDimensions().add(extraDimension);
2733-
}
2689+
for (Dimension extraDimension : (List<Dimension>)single.extraDimensions()) {
2690+
extraDimension.delete();
2691+
fragment.extraDimensions().add(extraDimension);
27342692
}
27352693
} else {
27362694
fragment = this.ast.newVariableDeclarationFragment();
@@ -2860,7 +2818,7 @@ Type convertToType(JCTree javac) {
28602818
// or empty (eg `test.`)
28612819
simpleName.setSourceRange(qualifierType.getStartPosition(), 0);
28622820
}
2863-
if(qualifierType instanceof SimpleType simpleType && (ast.apiLevel() < AST.JLS8 || simpleType.annotations().isEmpty())) {
2821+
if(qualifierType instanceof SimpleType simpleType && simpleType.annotations().isEmpty()) {
28642822
simpleType.delete();
28652823
Name parentName = simpleType.getName();
28662824
parentName.setParent(null, null);
@@ -2888,27 +2846,21 @@ Type convertToType(JCTree javac) {
28882846
return res;
28892847
}
28902848
if (javac instanceof JCTypeUnion union) {
2891-
if (this.ast.apiLevel() > AST.JLS3) {
2892-
UnionType res = this.ast.newUnionType();
2893-
commonSettings(res, javac);
2894-
union.getTypeAlternatives().stream()
2895-
.map(this::convertToType)
2896-
.filter(Objects::nonNull)
2897-
.forEach(res.types()::add);
2898-
return res;
2899-
} else {
2900-
Optional<Type> lastType = union.getTypeAlternatives().reverse().stream().map(this::convertToType).filter(Objects::nonNull).findFirst();
2901-
lastType.ifPresent(a -> a.setFlags(a.getFlags() | ASTNode.MALFORMED));
2902-
return lastType.get();
2903-
}
2849+
UnionType res = this.ast.newUnionType();
2850+
commonSettings(res, javac);
2851+
union.getTypeAlternatives().stream()
2852+
.map(this::convertToType)
2853+
.filter(Objects::nonNull)
2854+
.forEach(res.types()::add);
2855+
return res;
29042856
}
29052857
if (javac instanceof JCArrayTypeTree jcArrayType) {
29062858
Type t = convertToType(jcArrayType.getType());
29072859
if (t == null) {
29082860
return null;
29092861
}
29102862
ArrayType res;
2911-
if (t instanceof ArrayType childArrayType && this.ast.apiLevel > AST.JLS4_INTERNAL) {
2863+
if (t instanceof ArrayType childArrayType) {
29122864
res = childArrayType;
29132865
res.dimensions().addFirst(this.ast.newDimension());
29142866
commonSettings(res, jcArrayType);
@@ -2932,10 +2884,8 @@ Type convertToType(JCTree javac) {
29322884
}
29332885
if( ordinalEnd != -1 ) {
29342886
commonSettings(res, jcArrayType, ordinalEnd + 1, true);
2935-
if( this.ast.apiLevel >= AST.JLS8_INTERNAL ) {
2936-
if( res.dimensions().size() > 0 ) {
2937-
((Dimension)res.dimensions().get(0)).setSourceRange(startPos + ordinalStart, ordinalEnd - ordinalStart + 1);
2938-
}
2887+
if( res.dimensions().size() > 0 ) {
2888+
((Dimension)res.dimensions().get(0)).setSourceRange(startPos + ordinalStart, ordinalEnd - ordinalStart + 1);
29392889
}
29402890
return res;
29412891
}
@@ -2981,7 +2931,6 @@ Type convertToType(JCTree javac) {
29812931
JCExpression jcpe = jcAnnotatedType.getUnderlyingType();
29822932
if( jcAnnotatedType.getAnnotations() != null //
29832933
&& !jcAnnotatedType.getAnnotations().isEmpty() //
2984-
&& this.ast.apiLevel >= AST.JLS8_INTERNAL
29852934
&& !(jcpe instanceof JCWildcard)) {
29862935
if( jcpe instanceof JCFieldAccess jcfa2) {
29872936
if( jcfa2.selected instanceof JCAnnotatedType || jcfa2.selected instanceof JCTypeApply) {
@@ -3005,12 +2954,12 @@ Type convertToType(JCTree javac) {
30052954
if (res == null) { // nothing specific
30062955
res = convertToType(jcAnnotatedType.getUnderlyingType());
30072956
}
3008-
if (res instanceof AnnotatableType annotatableType && this.ast.apiLevel() >= AST.JLS8_INTERNAL) {
2957+
if (res instanceof AnnotatableType annotatableType) {
30092958
for (JCAnnotation annotation : jcAnnotatedType.getAnnotations()) {
30102959
annotatableType.annotations().add(convert(annotation));
30112960
}
30122961
} else if (res instanceof ArrayType arrayType) {
3013-
if (this.ast.apiLevel() >= AST.JLS8 && !arrayType.dimensions().isEmpty()) {
2962+
if (!arrayType.dimensions().isEmpty()) {
30142963
for (JCAnnotation annotation : jcAnnotatedType.getAnnotations()) {
30152964
((Dimension)arrayType.dimensions().get(0)).annotations().add(convert(annotation));
30162965
}
@@ -3269,14 +3218,6 @@ private ModifierKeyword modifierToKeyword(javax.lang.model.element.Modifier java
32693218
private Modifier modifierToDom(javax.lang.model.element.Modifier javac) {
32703219
return this.ast.newModifier(modifierToKeyword(javac));
32713220
}
3272-
private int modifierToFlagVal(javax.lang.model.element.Modifier javac) {
3273-
ModifierKeyword m = modifierToKeyword(javac);
3274-
if( m != null ) {
3275-
return m.toFlagValue();
3276-
}
3277-
return 0;
3278-
}
3279-
32803221

32813222
private Modifier convert(javax.lang.model.element.Modifier javac, int startPos, int endPos) {
32823223
Modifier res = modifierToDom(javac);

0 commit comments

Comments
 (0)