Skip to content

Commit

Permalink
Merge pull request javaparser#1696 from ftomassetti/issue1695
Browse files Browse the repository at this point in the history
add possibility to resolve array.length
  • Loading branch information
matozoid authored Jul 16, 2018
2 parents 3da53a0 + 1da476e commit 480b131
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.github.javaparser.resolution.SymbolResolver;
import com.github.javaparser.resolution.UnsolvedSymbolException;
import com.github.javaparser.resolution.declarations.*;
import com.github.javaparser.resolution.types.ResolvedPrimitiveType;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFactory;
Expand All @@ -29,6 +30,25 @@
*/
public class JavaSymbolSolver implements SymbolResolver {

private static class ArrayLengthValueDeclaration implements ResolvedValueDeclaration {

private static final ArrayLengthValueDeclaration INSTANCE = new ArrayLengthValueDeclaration();

private ArrayLengthValueDeclaration() {

}

@Override
public String getName() {
return "length";
}

@Override
public ResolvedType getType() {
return ResolvedPrimitiveType.INT;
}
}

private TypeSolver typeSolver;

public JavaSymbolSolver(TypeSolver typeSolver) {
Expand Down Expand Up @@ -142,6 +162,14 @@ public <T> T resolveDeclaration(Node node, Class<T> resultClass) {
return resultClass.cast(result.getCorrespondingDeclaration());
}
} else {
if (((FieldAccessExpr) node).getName().getId().equals("length")) {
ResolvedType scopeType = ((FieldAccessExpr) node).getScope().calculateResolvedType();
if (scopeType.isArray()) {
if (resultClass.isInstance(ArrayLengthValueDeclaration.INSTANCE)) {
return resultClass.cast(ArrayLengthValueDeclaration.INSTANCE);
}
}
}
throw new UnsolvedSymbolException("We are unable to find the value declaration corresponding to " + node);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package com.github.javaparser.symbolsolver.resolution;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseStart;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StringProvider;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.expr.FieldAccessExpr;
import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration;
import com.github.javaparser.resolution.types.ResolvedPrimitiveType;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

import static org.junit.Assert.assertEquals;

/**
Expand All @@ -23,4 +34,17 @@ public void verifyAnArrayAccessExprTypeIsCalculatedProperly() {
assertEquals(true, type.isReferenceType());
assertEquals("java.lang.String", type.asReferenceType().getQualifiedName());
}

@Test
public void arrayLengthValueDeclaration() {
String code = "class A { String[] arrSQL; int l = arrSQL.length; }";
ParserConfiguration parserConfiguration = new ParserConfiguration();
parserConfiguration.setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver()));
CompilationUnit cu = new JavaParser(parserConfiguration).parse(ParseStart.COMPILATION_UNIT, new StringProvider(code)).getResult().get();
FieldDeclaration field = cu.getClassByName("A").get().getFieldByName("l").get();

ResolvedValueDeclaration resolvedValueDeclaration = ((FieldAccessExpr)field.getVariables().get(0).getInitializer().get()).resolve();
assertEquals("length", resolvedValueDeclaration.getName());
assertEquals(ResolvedPrimitiveType.INT, resolvedValueDeclaration.getType());
}
}

0 comments on commit 480b131

Please sign in to comment.