src: moved Java indexer from JavaSymbolSolver to Eclipse JDT

* added some more tests
* changed 3rd party licenses respectively

fortune cookie message = Your present plans are going to succeed.
This commit is contained in:
mlangkabel
2017-09-07 12:40:34 +02:00
parent 6cf0b87f7b
commit 5c614c77e2
51 changed files with 2518 additions and 3480 deletions
@@ -1,14 +1,19 @@
package com.sourcetrail.name;
import java.io.File;
import java.util.List;
public class JavaDeclName
import com.sourcetrail.Position;
public class JavaDeclName implements JavaSymbolName
{
private JavaDeclName m_parent = null;
private String m_name = "";
private List<String> m_typeParameterNames = null;
private boolean m_isUnsolved = false;
private boolean m_isAnonymous = false;
private boolean m_isLocal = false;
private boolean m_isGlobal = false;
public static JavaDeclName unsolved()
{
@@ -17,13 +22,33 @@ public class JavaDeclName
return declName;
}
public static JavaDeclName anonymousClass(String fileName, int line, int col)
public static JavaDeclName anonymousClass(File filePath, int line, int col)
{
JavaDeclName declName = new JavaDeclName("anonymous class (" + fileName + "<" + line + ":" + col + ">)");
JavaDeclName declName = new JavaDeclName("anonymous class (" + filePath.getName() + "<" + line + ":" + col + ">)");
declName.m_isAnonymous = true;
return declName;
}
public static JavaDeclName localSymbol(JavaDeclName methodContextName, int id)
{
JavaDeclName declName = new JavaDeclName(methodContextName + "<" + id + ">");
declName.m_isLocal = true;
return declName;
}
public static JavaDeclName globalSymbol(File fileContext, int id)
{
JavaDeclName declName = new JavaDeclName(fileContext.getName() + "<" + id + ">");
declName.m_isGlobal = true;
return declName;
}
public static JavaDeclName scope(File fileContext, Position begin)
{
JavaDeclName declName = new JavaDeclName(fileContext.getName() + "<" + begin.line + ":" + begin.column + ">");
return declName;
}
public static JavaDeclName fromDotSeparatedString(String s)
{
JavaDeclName declName = null;
@@ -78,6 +103,16 @@ public class JavaDeclName
return m_isAnonymous;
}
public boolean getIsLocal()
{
return m_isLocal;
}
public boolean getIsGlobal()
{
return m_isGlobal;
}
public NameHierarchy toNameHierarchy()
{
NameHierarchy nameHierarchy;
@@ -129,4 +164,8 @@ public class JavaDeclName
}
return string;
}
public List<String> getTypeParameterNames() {
return m_typeParameterNames;
}
}
@@ -0,0 +1,29 @@
package com.sourcetrail.name;
import java.io.File;
public class JavaFileName implements JavaSymbolName
{
private File m_filePath = null;
public JavaFileName(File filePath)
{
m_filePath = filePath;
}
@Override
public NameHierarchy toNameHierarchy()
{
NameHierarchy nameHierarchy;
if (m_filePath != null)
{
nameHierarchy = new NameHierarchy(m_filePath.toString().replaceAll("\\\\", "/"));
}
else
{
nameHierarchy = new NameHierarchy("unsolved-file");
}
nameHierarchy.setSeparator('/');
return nameHierarchy;
}
}
@@ -7,24 +7,24 @@ import java.util.Optional;
public class JavaFunctionDeclName extends JavaDeclName
{
private JavaTypeName m_returnTypeName = null;
private List<JavaTypeName> m_parameterNames = new ArrayList<>();
private List<JavaTypeName> m_parameterTypeNames = new ArrayList<>();
private boolean m_isStatic = false;
public JavaFunctionDeclName(String name, JavaTypeName returnTypeName, List<JavaTypeName> parameterNames, boolean isStatic)
public JavaFunctionDeclName(String name, JavaTypeName returnTypeName, List<JavaTypeName> parameterTypeNames, boolean isStatic)
{
super(name);
m_returnTypeName = returnTypeName;
if (m_parameterNames != null) m_parameterNames = parameterNames;
if (parameterTypeNames != null) m_parameterTypeNames = parameterTypeNames;
m_isStatic = isStatic;
}
public JavaFunctionDeclName(String name, List<String> typeParameterNames, JavaTypeName returnTypeName, List<JavaTypeName> parameterNames, boolean isStatic)
public JavaFunctionDeclName(String name, List<String> typeParameterNames, JavaTypeName returnTypeName, List<JavaTypeName> parameterTypeNames, boolean isStatic)
{
super(name, typeParameterNames);
m_returnTypeName = returnTypeName;
m_parameterNames = parameterNames;
if (parameterTypeNames != null) m_parameterTypeNames = parameterTypeNames;
m_isStatic = isStatic;
}
@@ -60,15 +60,15 @@ public class JavaFunctionDeclName extends JavaDeclName
private String getParameterString()
{
String string = "(";
if (m_parameterNames != null)
if (m_parameterTypeNames != null)
{
for (int i = 0; i < m_parameterNames.size(); i++)
for (int i = 0; i < m_parameterTypeNames.size(); i++)
{
if (i != 0)
{
string += ", ";
}
string += m_parameterNames.get(i).toString();
string += m_parameterTypeNames.get(i).toString();
}
}
string += ")";
@@ -0,0 +1,6 @@
package com.sourcetrail.name;
public interface JavaSymbolName
{
public NameHierarchy toNameHierarchy();
}
@@ -2,15 +2,19 @@ package com.sourcetrail.name;
import java.util.List;
public class JavaTypeName
public class JavaTypeName implements JavaSymbolName
{
private JavaDeclName m_parent = null;
private String m_name = "";
private List<JavaTypeName> m_typeArgumentNames = null;
private List<String> m_typeParameterNames = null;
private List<JavaTypeName> m_typeArguments = null;
private boolean m_isUnsolved = false;
public static JavaTypeName unsolved()
{
return new JavaTypeName("unsolved-type", null);
JavaTypeName typeName = new JavaTypeName("unsolved-type", null);
typeName.m_isUnsolved = true;
return typeName;
}
public static JavaTypeName fromDotSeparatedString(String s)
@@ -36,11 +40,12 @@ public class JavaTypeName
m_name = name;
}
public JavaTypeName(String name, List<JavaTypeName> typeArgumentNames, JavaDeclName parent)
public JavaTypeName(String name, List<String> typeParameterNames, List<JavaTypeName> typeArguments, JavaDeclName parent)
{
m_parent = parent;
m_name = name;
m_typeArgumentNames = typeArgumentNames;
m_typeParameterNames = typeParameterNames;
m_typeArguments = typeArguments;
}
public JavaDeclName getParent()
@@ -53,6 +58,13 @@ public class JavaTypeName
return m_name;
}
public JavaDeclName toDeclName()
{
JavaDeclName declName = new JavaDeclName(m_name, m_typeParameterNames);
declName.setParent(m_parent);
return declName;
}
public NameHierarchy toNameHierarchy()
{
NameHierarchy nameHierarchy;
@@ -89,16 +101,16 @@ public class JavaTypeName
private String getTypeArgumentString()
{
String string = "";
if (m_typeArgumentNames != null && !m_typeArgumentNames.isEmpty())
if (m_typeArguments != null && !m_typeArguments.isEmpty())
{
string += "<";
for (int i = 0; i < m_typeArgumentNames.size(); i++)
for (int i = 0; i < m_typeArguments.size(); i++)
{
if (i != 0)
{
string += ", ";
}
string += m_typeArgumentNames.get(i).toString();
string += m_typeArguments.get(i).toString();
}
string += ">";
}
@@ -7,6 +7,7 @@ import java.util.Optional;
public class NameHierarchy
{
private List<NameElement> m_elements = new ArrayList<>();
private char m_separatpr = '.';
public NameHierarchy()
{
@@ -27,6 +28,11 @@ public class NameHierarchy
m_elements.addAll(names);
}
public void setSeparator(char separator)
{
m_separatpr = separator;
}
public void push(NameElement element)
{
m_elements.add(element);
@@ -51,7 +57,7 @@ public class NameHierarchy
public String serialize()
{
String serialized = ".\tm";
String serialized = m_separatpr + "\tm";
for (int i = 0; i < m_elements.size(); i++)
{
@@ -0,0 +1,341 @@
package com.sourcetrail.name.resolver;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.IPackageBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.Modifier;
import com.sourcetrail.ContextList;
import com.sourcetrail.name.JavaDeclName;
import com.sourcetrail.name.JavaFunctionDeclName;
import com.sourcetrail.name.JavaTypeName;
import com.sourcetrail.name.JavaVariableDeclName;
public class BindingNameResolver extends NameResolver
{
public static IBinding getParentBinding(IBinding binding)
{
if (binding instanceof ITypeBinding)
{
return getParentBinding((ITypeBinding) binding);
}
else if (binding instanceof IMethodBinding)
{
return ((IMethodBinding) binding).getDeclaringClass();
}
else if (binding instanceof IVariableBinding)
{
return ((IVariableBinding) binding).getDeclaringClass();
}
return null;
}
public static IBinding getParentBinding(ITypeBinding binding)
{
if (binding.isLocal() || binding.isAnonymous())
{
if (binding.getDeclaringMember() instanceof IVariableBinding)
{
IVariableBinding variableBinding = (IVariableBinding) binding.getDeclaringMember();
if (variableBinding.getDeclaringClass() != null)
{
return variableBinding.getDeclaringClass();
}
else
{
return variableBinding.getDeclaringMethod();
}
}
else
{
return binding.getDeclaringMethod();
}
}
else if (binding.isMember())
{
return binding.getDeclaringClass();
}
else if (binding.isTypeVariable())
{
if (binding.getDeclaringClass() != null)
{
return binding.getDeclaringClass();
}
else
{
return binding.getDeclaringMethod();
}
}
else if (binding.isTopLevel())
{
return binding.getPackage();
}
return null; // we don't have a parent (like void doesn't have a parent)
}
public BindingNameResolver(File currentFile, CompilationUnit compilationUnit, ContextList ignoredContexts)
{
super(currentFile, compilationUnit, ignoredContexts);
}
public static Optional<JavaTypeName> getQualifiedName(ITypeBinding binding, File currentFile, CompilationUnit compilationUnit)
{
return getQualifiedName(binding, currentFile, compilationUnit, null);
}
public static Optional<JavaTypeName> getQualifiedName(ITypeBinding binding, File currentFile, CompilationUnit compilationUnit, ContextList ignoredContexts)
{
BindingNameResolver resolver = new BindingNameResolver(currentFile, compilationUnit, ignoredContexts);
return resolver.getQualifiedName(binding);
}
public Optional<JavaTypeName> getQualifiedName(ITypeBinding binding)
{
if (binding == null)
{
return Optional.empty();
}
if (binding.isArray())
{
return getQualifiedName(binding.getElementType());
}
else if (binding.isAnonymous())
{
ASTNode node = m_compilationUnit.findDeclaringNode(binding);
if (node instanceof AnonymousClassDeclaration)
{
JavaDeclName decl = DeclNameResolver.getQualifiedDeclName((AnonymousClassDeclaration) node, m_currentFile, m_compilationUnit, m_ignoredContexts);
return Optional.of(new JavaTypeName(decl.getName(), decl.getTypeParameterNames(), null, decl.getParent()));
}
else
{
return Optional.empty();
}
}
if (binding.isParameterizedType())
{
List<JavaTypeName> typeArguments = new ArrayList<>();
for (ITypeBinding typeArgumentBinding: binding.getTypeArguments())
{
Optional<JavaTypeName> typeArgument = getQualifiedName(typeArgumentBinding);
if (typeArgument.isPresent())
{
typeArguments.add(typeArgument.get());
}
}
Optional<JavaTypeName> typeName = getQualifiedName(binding.getTypeDeclaration());
if (typeName.isPresent())
{
JavaDeclName declName = typeName.get().toDeclName();
return Optional.of(new JavaTypeName(declName.getName(), declName.getTypeParameterNames(), typeArguments, declName.getParent()));
}
else
{
return Optional.empty();
}
}
String name = binding.getName();
List<String> typeParameterNames = null;
if (binding.isGenericType())
{
typeParameterNames = new ArrayList<>();
for (ITypeBinding typeParameter: binding.getTypeParameters())
{
typeParameterNames.add(typeParameter.getName());
}
}
JavaDeclName parentDeclName = getQualifiedContextName(binding);
if (parentDeclName != null && parentDeclName.getIsUnsolved())
{
return Optional.empty();
}
// type arguments will be set in caller (which is this same method)
return Optional.of(new JavaTypeName(name, typeParameterNames, null, parentDeclName));
}
public static Optional<JavaDeclName> getQualifiedName(IMethodBinding binding, File currentFile, CompilationUnit compilationUnit)
{
return getQualifiedName(binding, currentFile, compilationUnit, null);
}
public static Optional<JavaDeclName> getQualifiedName(IMethodBinding binding, File currentFile, CompilationUnit compilationUnit, ContextList ignoredContexts)
{
BindingNameResolver resolver = new BindingNameResolver(currentFile, compilationUnit, ignoredContexts);
return resolver.getQualifiedName(binding);
}
public Optional<JavaDeclName> getQualifiedName(IMethodBinding binding)
{
if (binding == null)
{
return Optional.empty();
}
String name = binding.getName();
List<String> typeParameterNames = null;
if (binding.isGenericMethod())
{
typeParameterNames = new ArrayList<>();
for (ITypeBinding typeParameter: binding.getTypeParameters())
{
typeParameterNames.add(typeParameter.getName());
}
}
ContextList ignoredContexts = m_ignoredContexts.copy();
ignoredContexts.add(binding);
JavaTypeName returnTypeName = binding.isConstructor() ? null : getQualifiedName(binding.getReturnType(), m_currentFile, m_compilationUnit, ignoredContexts).orElse(JavaTypeName.unsolved());
List<JavaTypeName> parameterTypeNames = new ArrayList<>();
for (ITypeBinding parameterType: binding.getParameterTypes())
{
parameterTypeNames.add(getQualifiedName(parameterType, m_currentFile, m_compilationUnit, ignoredContexts).orElse(JavaTypeName.unsolved()));
}
boolean isStatic = Modifier.isStatic(binding.getModifiers());
JavaFunctionDeclName declName = new JavaFunctionDeclName(name, typeParameterNames, returnTypeName, parameterTypeNames, isStatic);
JavaDeclName parentDeclName = getQualifiedContextName(binding);
if (parentDeclName != null)
{
if (!parentDeclName.getIsUnsolved())
{
declName.setParent(parentDeclName);
}
else
{
return Optional.empty();
}
}
return Optional.of(declName);
}
public static Optional<JavaDeclName> getQualifiedName(IPackageBinding binding, File currentFile, CompilationUnit compilationUnit)
{
return getQualifiedName(binding, currentFile, compilationUnit, null);
}
public static Optional<JavaDeclName> getQualifiedName(IPackageBinding binding, File currentFile, CompilationUnit compilationUnit, ContextList ignoredContexts)
{
BindingNameResolver resolver = new BindingNameResolver(currentFile, compilationUnit, ignoredContexts);
return resolver.getQualifiedName(binding);
}
public Optional<JavaDeclName> getQualifiedName(IPackageBinding binding)
{
if (!binding.isUnnamed())
{
return Optional.of(JavaDeclName.fromDotSeparatedString(binding.getName()));
}
return Optional.empty();
}
public static JavaDeclName getQualifiedName(IVariableBinding binding, File currentFile, CompilationUnit compilationUnit)
{
return getQualifiedName(binding, currentFile, compilationUnit, null);
}
public static JavaDeclName getQualifiedName(IVariableBinding binding, File currentFile, CompilationUnit compilationUnit, ContextList ignoredContexts)
{
BindingNameResolver resolver = new BindingNameResolver(currentFile, compilationUnit, ignoredContexts);
return resolver.getQualifiedName(binding);
}
public JavaDeclName getQualifiedName(IVariableBinding binding)
{
if (binding == null)
{
return JavaDeclName.unsolved();
}
if (binding.isField() || binding.isEnumConstant())
{
JavaDeclName declName;
if (binding.isEnumConstant())
{
declName = new JavaDeclName(binding.getName());
}
else
{
JavaTypeName typeName = getQualifiedName(binding.getType()).orElse(JavaTypeName.unsolved());
declName = new JavaVariableDeclName(binding.getName(), typeName, Modifier.isStatic(binding.getModifiers()));
}
JavaDeclName parentDeclName = getQualifiedContextName(binding);
if (parentDeclName != null)
{
if (!parentDeclName.getIsUnsolved())
{
declName.setParent(parentDeclName);
}
else
{
declName = JavaDeclName.unsolved();
}
}
return declName;
}
else
{
if (binding.getDeclaringMethod() != null)
{
return JavaDeclName.localSymbol(getQualifiedName(binding.getDeclaringMethod()).orElse(JavaDeclName.unsolved()), binding.getVariableId());
}
else
{
return JavaDeclName.globalSymbol(m_currentFile, binding.getVariableId());
}
}
}
private JavaDeclName getQualifiedContextName(IBinding binding)
{
IBinding parentBinding = getParentBinding(binding);
if (parentBinding == null || m_ignoredContexts.contains(parentBinding))
{
return null;
}
if (parentBinding instanceof ITypeBinding)
{
return getQualifiedName((ITypeBinding) parentBinding).map(tn ->tn.toDeclName()).orElse(JavaDeclName.unsolved());
}
else if (parentBinding instanceof IMethodBinding)
{
return getQualifiedName((IMethodBinding) parentBinding).orElse(JavaDeclName.unsolved());
}
else if (parentBinding instanceof IVariableBinding)
{
return getQualifiedName((IVariableBinding) parentBinding);
}
else if (parentBinding instanceof IPackageBinding)
{
return getQualifiedName((IPackageBinding) parentBinding).orElse(null);
}
return null;
}
}
@@ -0,0 +1,318 @@
package com.sourcetrail.name.resolver;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration;
import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration;
import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
import org.eclipse.jdt.core.dom.BodyDeclaration;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
import org.eclipse.jdt.core.dom.EnumDeclaration;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.Initializer;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.PackageDeclaration;
import org.eclipse.jdt.core.dom.QualifiedName;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.TypeParameter;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import com.sourcetrail.ContextList;
import com.sourcetrail.Position;
import com.sourcetrail.Utility;
import com.sourcetrail.name.JavaDeclName;
import com.sourcetrail.name.JavaFunctionDeclName;
import com.sourcetrail.name.JavaTypeName;
import com.sourcetrail.name.JavaVariableDeclName;
public class DeclNameResolver extends NameResolver
{
public DeclNameResolver(File currentFile, CompilationUnit compilationUnit, ContextList ignoredContexts)
{
super(currentFile, compilationUnit, ignoredContexts);
}
public static JavaDeclName getQualifiedDeclName(VariableDeclarationFragment decl, File currentFile, CompilationUnit compilationUnit)
{
return getQualifiedDeclName(decl, currentFile, compilationUnit, null);
}
public static JavaDeclName getQualifiedDeclName(VariableDeclarationFragment decl, File currentFile, CompilationUnit compilationUnit, ContextList ignoredContexts)
{
DeclNameResolver resolver = new DeclNameResolver(currentFile, compilationUnit, ignoredContexts);
return resolver.getQualifiedDeclName(decl);
}
public JavaDeclName getQualifiedDeclName(VariableDeclarationFragment decl)
{
JavaDeclName declName = JavaDeclName.unsolved();
if (decl != null)
{
declName = getDeclName(decl);
if (!declName.getIsUnsolved())
{
JavaDeclName parentDeclName = getQualifiedContextName(decl);
if (parentDeclName != null)
{
if (!parentDeclName.getIsUnsolved())
{
declName.setParent(parentDeclName);
}
else
{
declName = JavaDeclName.unsolved();
}
}
}
}
return declName;
}
public JavaDeclName getDeclName(VariableDeclarationFragment decl)
{
JavaTypeName typeName = JavaTypeName.unsolved();
boolean isStatic = false;
Optional<FieldDeclaration> fieldDeclaration = getAncestorOfType(decl, FieldDeclaration.class);
if (fieldDeclaration.isPresent())
{
if (decl.resolveBinding() instanceof IVariableBinding)
{
isStatic = Modifier.isStatic(decl.resolveBinding().getModifiers());
}
else
{
isStatic = Modifier.isStatic(fieldDeclaration.get().getModifiers());
}
typeName = BindingNameResolver.getQualifiedName(
fieldDeclaration.get().getType().resolveBinding(),
m_currentFile,
m_compilationUnit,
m_ignoredContexts.copy()).orElse(JavaTypeName.unsolved());
}
return new JavaVariableDeclName(decl.getName().getIdentifier(), typeName, isStatic);
}
public static JavaDeclName getQualifiedDeclName(BodyDeclaration decl, File currentFile, CompilationUnit compilationUnit)
{
return getQualifiedDeclName(decl, currentFile, compilationUnit, null);
}
public static JavaDeclName getQualifiedDeclName(BodyDeclaration decl, File currentFile, CompilationUnit compilationUnit, ContextList ignoredContexts)
{
DeclNameResolver resolver = new DeclNameResolver(currentFile, compilationUnit, ignoredContexts);
return resolver.getQualifiedDeclName(decl);
}
public JavaDeclName getQualifiedDeclName(BodyDeclaration decl)
{
JavaDeclName declName = JavaDeclName.unsolved();
if (decl != null)
{
declName = getDeclName(decl);
if (!declName.getIsUnsolved())
{
JavaDeclName parentDeclName = getQualifiedContextName(decl);
if (parentDeclName != null)
{
if (!parentDeclName.getIsUnsolved())
{
declName.setParent(parentDeclName);
}
else
{
declName = JavaDeclName.unsolved();
}
}
}
}
return declName;
}
public JavaDeclName getDeclName(BodyDeclaration decl)
{
JavaDeclName declName = JavaDeclName.unsolved();
if (decl != null)
{
if (decl instanceof AnnotationTypeDeclaration)
{
declName = new JavaDeclName("AnnotationTypeDeclaration");
}
else if (decl instanceof EnumDeclaration)
{
declName = new JavaDeclName(((EnumDeclaration) decl).getName().getIdentifier());
}
else if (decl instanceof TypeDeclaration)
{
TypeDeclaration typeDeclaration = (TypeDeclaration) decl;
List<String> typeParameterNames = new ArrayList<>();
for (Object typeParameter: typeDeclaration.typeParameters())
{
if (typeParameter instanceof TypeParameter)
{
typeParameterNames.add(((TypeParameter) typeParameter).getName().getIdentifier());
}
}
declName = new JavaDeclName(typeDeclaration.getName().getIdentifier(), typeParameterNames);
}
else if (decl instanceof AnnotationTypeMemberDeclaration)
{
declName = new JavaDeclName("AnnotationTypeMemberDeclaration");
}
else if (decl instanceof EnumConstantDeclaration)
{
declName = new JavaDeclName(((EnumConstantDeclaration) decl).getName().getIdentifier());
}
else if (decl instanceof FieldDeclaration)
{
declName = new JavaDeclName("FieldDeclaration");
}
else if (decl instanceof Initializer)
{
declName = new JavaDeclName("Initializer");
}
else if (decl instanceof MethodDeclaration)
{
MethodDeclaration methodDeclaration = (MethodDeclaration) decl;
List<String> typeParameterNames = new ArrayList<>();
for (Object typeParameter: methodDeclaration.typeParameters())
{
if (typeParameter instanceof TypeParameter)
{
typeParameterNames.add(((TypeParameter) typeParameter).getName().getIdentifier());
}
}
ContextList ignoredContexts = m_ignoredContexts.copy();
ignoredContexts.add(((MethodDeclaration) decl).resolveBinding());
JavaTypeName returnTypeName = methodDeclaration.isConstructor() ? null : BindingNameResolver.getQualifiedName(
methodDeclaration.getReturnType2().resolveBinding(), m_currentFile, m_compilationUnit, ignoredContexts).orElse(JavaTypeName.unsolved());
List<JavaTypeName> parameterTypeNames = new ArrayList<>();
for (Object parameter: methodDeclaration.parameters())
{
if (parameter instanceof SingleVariableDeclaration)
{
parameterTypeNames.add(BindingNameResolver.getQualifiedName(
((SingleVariableDeclaration) parameter).getType().resolveBinding(), m_currentFile, m_compilationUnit, ignoredContexts).orElse(JavaTypeName.unsolved()));
}
}
declName = new JavaFunctionDeclName(
methodDeclaration.getName().getIdentifier(),
typeParameterNames,
returnTypeName,
parameterTypeNames,
Modifier.isStatic(methodDeclaration.getModifiers()));
}
}
return declName;
}
public static JavaDeclName getQualifiedDeclName(AnonymousClassDeclaration decl, File currentFile, CompilationUnit compilationUnit)
{
return getQualifiedDeclName(decl, currentFile, compilationUnit, null);
}
public static JavaDeclName getQualifiedDeclName(AnonymousClassDeclaration decl, File currentFile, CompilationUnit compilationUnit, ContextList ignoredContexts)
{
DeclNameResolver resolver = new DeclNameResolver(currentFile, compilationUnit, ignoredContexts);
return resolver.getQualifiedDeclName(decl);
}
public JavaDeclName getQualifiedDeclName(AnonymousClassDeclaration decl)
{
JavaDeclName declName = JavaDeclName.unsolved();
if (decl != null)
{
declName = getDeclName(decl);
if (!declName.getIsUnsolved())
{
JavaDeclName parentDeclName = getQualifiedContextName(decl);
if (parentDeclName != null)
{
if (!parentDeclName.getIsUnsolved())
{
declName.setParent(parentDeclName);
}
else
{
declName = JavaDeclName.unsolved();
}
}
}
}
return declName;
}
public JavaDeclName getDeclName(AnonymousClassDeclaration decl)
{
Position pos = Utility.getRange(decl, m_compilationUnit).begin;
return JavaDeclName.anonymousClass(m_currentFile, pos.line, pos.column);
}
public static JavaDeclName getQualifiedName(Name name)
{
if (name.isSimpleName())
{
return new JavaDeclName(((SimpleName) name).getIdentifier());
}
else
{
JavaDeclName declName = new JavaDeclName(((QualifiedName) name).getName().getIdentifier());
declName.setParent(getQualifiedName(((QualifiedName) name).getQualifier()));
return declName;
}
}
private JavaDeclName getQualifiedContextName(ASTNode decl)
{
ASTNode parentNode = decl.getParent();
if (parentNode == null)
{
return null;
}
if (parentNode instanceof BodyDeclaration && !(parentNode instanceof FieldDeclaration))
{
return getQualifiedDeclName((BodyDeclaration) parentNode);
}
else if (parentNode instanceof AnonymousClassDeclaration)
{
return getQualifiedDeclName((AnonymousClassDeclaration) parentNode);
}
else if (parentNode instanceof CompilationUnit)
{
PackageDeclaration packageDecl = ((CompilationUnit) parentNode).getPackage();
if (packageDecl != null)
{
return getQualifiedName(packageDecl.getName());
}
return null;
}
return getQualifiedContextName(parentNode);
}
}
@@ -0,0 +1,45 @@
package com.sourcetrail.name.resolver;
import java.io.File;
import java.util.Optional;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.CompilationUnit;
import com.sourcetrail.ContextList;
public abstract class NameResolver
{
protected File m_currentFile = null;
protected ContextList m_ignoredContexts = null;
protected CompilationUnit m_compilationUnit = null;
static protected <N> Optional<N> getAncestorOfType(ASTNode node, Class<N> classType)
{
ASTNode parent = node.getParent();
while (parent != null)
{
if (classType.isAssignableFrom(parent.getClass()))
{
return Optional.of(classType.cast(parent));
}
parent = parent.getParent();
}
return Optional.empty();
}
public NameResolver(File currentFile, CompilationUnit compilationUnit, ContextList ignoredContexts)
{
m_currentFile = currentFile;
m_compilationUnit = compilationUnit;
if (ignoredContexts != null)
{
m_ignoredContexts = ignoredContexts;
}
else
{
m_ignoredContexts = new ContextList();
}
}
}