|
|
|
|
|
Re: Suggesting Java classes [message #1696354 is a reply to message #1696196] |
Tue, 26 May 2015 04:04 |
Luis De Bello Messages: 95 Registered: January 2015 |
Member |
|
|
Hi Christian,
Thanks for pointing out to check the common types I was doing some debugging and I learnt that text ends up using the SearchEngine from eclipse adding some filters and hierarchy. As I didn't want to include Xbase to cover this simple case I have extracted some part of the code and then I use the result to generate some proposals. I left the code here in case someone need it
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.core.search.SearchPattern;
import org.eclipse.jdt.core.search.TypeNameRequestor;
public final class JDTResolver {
public JDTResolver() {
}
public List<JDTType> resolve(EObject model, String currentContent) {
List<JDTType> jdtTypes = new ArrayList<>();
String platformString = model.eResource().getURI().toPlatformString(true);
IFile resource = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformString));
IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(new IJavaElement[] { JavaCore.create(resource.getProject()) });
char[] typeName = null;
char[] packageName = null;
final int index = currentContent.lastIndexOf('.');
if (index == -1) {
// There is no package qualification perform the search only on the type name
typeName = currentContent.toCharArray();
} else if (index + 1 == currentContent.length()) {
// There is a package qualification and the last character is a dot perform the search for all types under the given package pattern for all types
typeName = "".toCharArray();
// Package name without the trailing dot
packageName = currentContent.substring(0, index).toCharArray();
} else {
// There is a package qualification, followed by a dot, and a type fragment type name without the package qualification
typeName = currentContent.substring(index + 1).toCharArray();
// Package name without the trailing dot
packageName = currentContent.substring(0, index).toCharArray();
}
JDTTypeNameRequestor requestor = new JDTTypeNameRequestor(jdtTypes);
try {
// Important: Do not use the search() method, its performance is bad compared to the searchAllTypeNames() method
new SearchEngine().searchAllTypeNames(packageName, SearchPattern.R_EXACT_MATCH, typeName, SearchPattern.R_PREFIX_MATCH, IJavaSearchConstants.CLASS_AND_INTERFACE,
searchScope, requestor, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
} catch (JavaModelException e) {
// Log error here
}
return jdtTypes;
}
public static class JDTType {
private String simpleTypeName;
private String packageName;
private int modifiers;
public JDTType(String simpleTypeName, String packageName, int modifiers) {
super();
this.simpleTypeName = simpleTypeName;
this.packageName = packageName;
this.modifiers = modifiers;
}
public String getSimpleTypeName() {
return simpleTypeName;
}
public String getPackageName() {
return packageName;
}
public int getModifiers() {
return modifiers;
}
}
private static class JDTTypeNameRequestor extends TypeNameRequestor {
private List<JDTType> jdtTypes;
public JDTTypeNameRequestor(List<JDTType> jdtTypes) {
this.jdtTypes = jdtTypes;
}
@Override
public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path) {
jdtTypes.add(new JDTType(String.valueOf(simpleTypeName), String.valueOf(packageName), modifiers));
}
}
}
[Updated on: Tue, 26 May 2015 04:06] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.04051 seconds