Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some convert/binding fixes #442

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.sun.tools.javac.code.Type.PackageType;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCAnnotatedType;
import com.sun.tools.javac.tree.JCTree.JCArrayTypeTree;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCExpression;
Expand Down Expand Up @@ -254,6 +255,9 @@ ITypeBinding resolveType(Type type) {
if (jcTree instanceof JCTypeApply jcta && jcta.type != null) {
return this.bindings.getTypeBinding(jcta.type);
}
if (jcTree instanceof JCAnnotatedType annotated && annotated.type != null) {
return this.bindings.getTypeBinding(annotated.type);
}

// return this.flowResult.stream().map(env -> env.enclClass)
// .filter(Objects::nonNull)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ private ASTNode convertBodyDeclaration(JCTree tree, ASTNode parent) {
res.setBody(convertBlock(block));
return res;
}
if (tree instanceof JCErroneous erroneous) {
if (tree instanceof JCErroneous erroneous || tree instanceof JCSkip) {
return null;
}
ILog.get().error("Unsupported " + tree + " of type" + tree.getClass());
Expand Down Expand Up @@ -828,7 +828,7 @@ private AbstractTypeDeclaration findSurroundingTypeDeclaration(ASTNode parent) {
}

private VariableDeclaration convertVariableDeclarationForLambda(JCVariableDecl javac) {
if( javac.type == null ) {
if( javac.getType() == null ) {
return createVariableDeclarationFragment(javac);
} else {
return convertVariableDeclaration(javac);
Expand Down Expand Up @@ -869,8 +869,15 @@ private VariableDeclaration convertVariableDeclaration(JCVariableDecl javac) {
res.setType(convertToType(unwrapDimensions(jcatt, dims)));
}
} else if ( (javac.mods.flags & VARARGS) != 0) {
JCTree type = javac.getType();
if (type instanceof JCAnnotatedType annotatedType) {
annotatedType.getAnnotations().stream()
.map(this::convert)
.forEach(res.varargsAnnotations()::add);
type = annotatedType.getUnderlyingType();
}
// We have varity
if( javac.getType() instanceof JCArrayTypeTree arr) {
if(type instanceof JCArrayTypeTree arr) {
res.setType(convertToType(arr.elemtype));
}
if( this.ast.apiLevel > AST.JLS2_INTERNAL) {
Expand Down Expand Up @@ -1372,6 +1379,9 @@ private Expression convertExpressionImpl(JCExpression javac) {
.map(JCVariableDecl.class::cast)
.map(this::convertVariableDeclarationForLambda)
.forEach(res.parameters()::add);
int arrowIndex = this.rawText.indexOf("->", jcLambda.getStartPosition());
int parenthesisIndex = this.rawText.indexOf(")", jcLambda.getStartPosition());
res.setParentheses(parenthesisIndex >= 0 && parenthesisIndex < arrowIndex);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if there's no opening ( ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we most likely receive a JCErroneous instead of a JCLambda

ASTNode body = jcLambda.getBody() instanceof JCExpression expr ? convertExpression(expr) :
jcLambda.getBody() instanceof JCStatement stmt ? convertStatement(stmt, res) :
null;
Expand Down Expand Up @@ -2173,13 +2183,18 @@ Type convertToType(JCTree javac) {
}
// case of not translatable name, eg because of generics
// TODO find a better check instead of relying on exception
if( this.ast.apiLevel > AST.JLS2_INTERNAL) {
QualifiedType res = this.ast.newQualifiedType(convertToType(qualified.getExpression()), (SimpleName)convertName(qualified.getIdentifier()));
commonSettings(res, qualified);
Type qualifierType = convertToType(qualified.getExpression());
if(qualifierType instanceof SimpleType simpleType && (ast.apiLevel() < AST.JLS8 || simpleType.annotations().isEmpty())) {
simpleType.delete();
Name parentName = simpleType.getName();
parentName.setParent(null, null);
QualifiedName name = this.ast.newQualifiedName(simpleType.getName(), (SimpleName)convertName(qualified.getIdentifier()));
SimpleType res = this.ast.newSimpleType(name);
commonSettings(res, javac);
return res;
} else {
SimpleType res = this.ast.newSimpleType(toName(qualified));
commonSettings(res, javac);
QualifiedType res = this.ast.newQualifiedType(qualifierType, (SimpleName)convertName(qualified.getIdentifier()));
commonSettings(res, qualified);
return res;
}
}
Expand Down Expand Up @@ -2252,7 +2267,7 @@ Type convertToType(JCTree javac) {
if( createNameQualifiedType && this.ast.apiLevel >= AST.JLS8_INTERNAL) {
JCExpression jcpe = jcAnnotatedType.underlyingType;
if( jcpe instanceof JCFieldAccess jcfa2) {
if( jcfa2.selected instanceof JCAnnotatedType) {
if( jcfa2.selected instanceof JCAnnotatedType || jcfa2.selected instanceof JCTypeApply) {
QualifiedType nameQualifiedType = new QualifiedType(this.ast);
commonSettings(nameQualifiedType, javac);
nameQualifiedType.setQualifier(convertToType(jcfa2.selected));
Expand All @@ -2265,13 +2280,16 @@ Type convertToType(JCTree javac) {
nameQualifiedType.setName(this.ast.newSimpleName(jcfa2.name.toString()));
res = nameQualifiedType;
}
} else if (jcpe instanceof JCIdent simpleType) {
res = this.ast.newSimpleType(convertName(simpleType.getName()));
commonSettings(res, javac);
}
} else {
convertToType(jcAnnotatedType.getUnderlyingType());
res = convertToType(jcAnnotatedType.getUnderlyingType());
}
if (res instanceof AnnotatableType annotatableType) {
if (res instanceof AnnotatableType annotatableType && this.ast.apiLevel() >= AST.JLS8) {
for (JCAnnotation annotation : jcAnnotatedType.getAnnotations()) {
annotatableType.annotations.add(convert(annotation));
annotatableType.annotations().add(convert(annotation));
}
} else if (res instanceof ArrayType arrayType) {
if (!arrayType.dimensions().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ private MethodRefParameter toMethodRefParam(JCTree type, int fromOffset) {
res.accept(new ASTVisitor(true) {
@Override
public void preVisit(ASTNode node) {
node.setSourceRange(node.getStartPosition() + fromOffset, node.toString().length());
node.setSourceRange(Math.max(0, node.getStartPosition()) + fromOffset, node.toString().length());
}
});
return res;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,8 @@ public String[] getNameComponents() {
return isUnnamed()? new String[0] : this.packageSymbol.getQualifiedName().toString().split("."); //$NON-NLS-1$
}

@Override
public String toString() {
return "package " + getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,16 @@ public String getQualifiedName() {
return at.elemtype.tsym.getQualifiedName().toString() + "[]";
}

return this.typeSymbol.getQualifiedName().toString();
StringBuilder res = new StringBuilder(this.type.toString());
// remove annotations here
int annotationIndex = -1;
while ((annotationIndex = res.lastIndexOf("@")) >= 0) {
int nextSpace = res.indexOf(" ", annotationIndex);
if (nextSpace >= 0) {
res.delete(annotationIndex, nextSpace + 1);
}
}
return res.toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,9 @@ private static int toModelFlags(int domModifiers, boolean isDeprecated) {
if (isDeprecated) res |= org.eclipse.jdt.core.Flags.AccDeprecated;
return res;
}

@Override
public String toString() {
return getType().getQualifiedName() + " " + getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -542,15 +542,14 @@ public org.eclipse.jdt.core.dom.CompilationUnit getOrBuildAST(WorkingCopyOwner w
return this.ast;
}
Map<String, String> options = getOptions(true);
int jlsLevel = Integer.parseInt(options.getOrDefault(JavaCore.COMPILER_SOURCE, Integer.toString(AST.getJLSLatest())));
ASTParser parser = ASTParser.newParser(jlsLevel);
ASTParser parser = ASTParser.newParser(new AST(options).apiLevel()); // go through AST constructor to convert options to apiLevel
parser.setWorkingCopyOwner(workingCopyOwner);
parser.setSource(this);
// greedily enable everything assuming the AST will be used extensively for edition
parser.setResolveBindings(true);
parser.setStatementsRecovery(true);
parser.setBindingsRecovery(true);
parser.setCompilerOptions(getOptions(true));
parser.setCompilerOptions(options);
if (parser.createAST(null) instanceof org.eclipse.jdt.core.dom.CompilationUnit newAST) {
if (storeAST) {
return this.ast = newAST;
Expand Down
Loading