How to get fully qualified names of parameters of selected method [message #529564] |
Mon, 26 April 2010 08:19  |
Eclipse User |
|
|
|
As told by briandealwis on irc I tried using SearchEngine to find fq names of types of parameters of selected method as below but it throws more than one results :
SearchPattern pattern = null;
IJavaSearchScope scope = null;
CollectingSearchRequestor requestor =
new CollectingSearchRequestor();
SearchEngine engine = new SearchEngine();
String[] paramTypes =
Signature.getParameterTypes(
selectedMethod.getSignature());
List<Object> results = requestor.getResults();
for(String s : paramTypes) {
if(!isPrimitive(s)) {
String readebleTypeName =
Signature.toString(s);
pattern = SearchPattern.createPattern(
readebleTypeName,
IJavaSearchConstants.TYPE,
IJavaSearchConstants.DECLARATIONS,
SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
// IJavaElement clazz = selectedMethod.getParent();
scope = SearchEngine.createWorkspaceScope();
// (
// new IJavaElement[]
// {clazz}, false);
try {
engine.search(pattern,
new SearchParticipant[] {
SearchEngine.
getDefaultSearchParticipant()},
scope, requestor,
new NullProgressMonitor());
} catch (CoreException e) {
e.printStackTrace();
}
results = requestor.getResults();
for(ListIterator<Object> it = results.listIterator(); it.hasNext();) {
Object ob = it.next();
if(ob instanceof TypeDeclarationMatch) {
Object element = ((TypeDeclarationMatch)ob).getElement();
if(element instanceof ResolvedBinaryType) {
ResolvedBinaryType rbt = (ResolvedBinaryType)element;
System.out.println("Attempt : " + rbt.getKey() + "\tName : " + rbt.getElementName());
} else if(element instanceof SourceType) {
SourceType st = (SourceType) element;
System.out.println("Attempt : " + st.getKey() + "\tName : " + st.getElementName());
}
}
}
There is no way to resolve the exact type of the parameter used in the method definition as several types are returned. If a (java) scope is created by passing the class or just the method it rightfully returns no results as types of parameters used for testing were String, user-defined-type etc.
Also this code makes use of a lot of internal api.
Isn't there an easy way to find fq names of types of parameters of a selected method. Just as a mouse hover on a method name quickly displays in a javadoc yellow box?
Thanks much in advance.
|
|
|
Re: How to get fully qualified names of parameters of selected method [message #529606 is a reply to message #529564] |
Mon, 26 April 2010 10:15   |
Eclipse User |
|
|
|
Try with something like that:
String signature =
Signature.getParameterTypes(selectedMethod.getSignature())[0 ];
String[][] resolvedTypeName =
selectedMethod.getCompilationUnit().resolveType(Signature.ge tSignatureSimpleName(signature));
if (resolvedTypeName != null && resolvedTypeName.length == 1) {
String fqName = resolvedTypeName[0][0];
if (fqName != null && fqName.length() > 0) {
fqName = fqName + ".";
}
fqName = fqName + resolvedTypeName[0][1];
So you do not even need to use a search engine.
cheers
Andreas
kaprasi wrote:
> As told by briandealwis on irc I tried using SearchEngine to find fq
> names of types of parameters of selected method as below but it throws
> more than one results :
>
> SearchPattern pattern = null;
> IJavaSearchScope scope = null;
> CollectingSearchRequestor requestor =
> new CollectingSearchRequestor();
> SearchEngine engine = new SearchEngine();
>
> String[] paramTypes =
> Signature.getParameterTypes(
> selectedMethod.getSignature());
> List<Object> results =
> requestor.getResults();
> for(String s : paramTypes) {
> if(!isPrimitive(s)) {
> String readebleTypeName =
> Signature.toString(s);
>
> pattern =
> SearchPattern.createPattern(
> readebleTypeName,
> IJavaSearchConstants.TYPE,
>
> IJavaSearchConstants.DECLARATIONS,
>
> SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
> // IJavaElement clazz =
> selectedMethod.getParent();
> scope =
> SearchEngine.createWorkspaceScope();
> // (
> // new IJavaElement[]
> // {clazz},
> false);
> try {
> engine.search(pattern,
> new
> SearchParticipant[] {
> SearchEngine.
>
> getDefaultSearchParticipant()},
> scope, requestor,
> new
> NullProgressMonitor());
> } catch (CoreException e) {
> e.printStackTrace();
> }
> results = requestor.getResults();
> for(ListIterator<Object> it =
> results.listIterator(); it.hasNext();) {
> Object ob = it.next();
> if(ob instanceof
> TypeDeclarationMatch) {
> Object element =
> ((TypeDeclarationMatch)ob).getElement();
> if(element instanceof
> ResolvedBinaryType) {
> ResolvedBinaryType
> rbt = (ResolvedBinaryType)element;
>
> System.out.println("Attempt : " + rbt.getKey() + "\tName : " +
> rbt.getElementName());
> } else if(element
> instanceof SourceType) {
> SourceType st =
> (SourceType) element;
>
> System.out.println("Attempt : " + st.getKey() + "\tName : " +
> st.getElementName());
> }
> }
> }
>
>
> There is no way to resolve the exact type of the parameter used in the
> method definition as several types are returned. If a (java) scope is
> created by passing the class or just the method it rightfully returns no
> results as types of parameters used for testing were String,
> user-defined-type etc.
> Also this code makes use of a lot of internal api.
>
> Isn't there an easy way to find fq names of types of parameters of a
> selected method. Just as a mouse hover on a method name quickly displays
> in a javadoc yellow box?
>
> Thanks much in advance.
|
|
|
|
Re: How to get fully qualified names of parameters of selected method [message #529857 is a reply to message #529809] |
Tue, 27 April 2010 09:27  |
Eclipse User |
|
|
|
You are right, the resolveType method is located on IType and not on the
ICompilationUnit. That's how compilerless programming ends.
In your snippet looks ok for simple cases; you did not aware generic
types and array signatures.
A method handling these issues could look like: (ScoutSignature.java of
Scout Project)
------------------------------------------------------------ ----------------
private String getTypeReferenceImpl(String signature, IType type,
IImportValidator validator) throws JavaModelException {
StringBuilder sigBuilder = new StringBuilder();
int arrayCount = 0;
switch (Signature.getTypeSignatureKind(signature)) {
case Signature.WILDCARD_TYPE_SIGNATURE:
sigBuilder.append("? extends ");
sigBuilder.append(getTypeReferenceImpl(signature.substring(1 ),
type, validator));
break;
case Signature.ARRAY_TYPE_SIGNATURE:
arrayCount = Signature.getArrayCount(signature);
sigBuilder.append(getTypeReferenceImpl(signature.substring(a rrayCount),
type, validator));
break;
case Signature.BASE_TYPE_SIGNATURE:
sigBuilder.append(Signature.getSignatureSimpleName(signature ));
break;
default:
String[] typeArguments = Signature.getTypeArguments(signature);
signature = Signature.getTypeErasure(signature);
if (signature.startsWith("Q")) {
// unresolved
String[][] resolvedTypeName =
type.resolveType(Signature.getSignatureSimpleName(signature) );
if (resolvedTypeName != null && resolvedTypeName.length == 1) {
String fqName = resolvedTypeName[0][0];
if (fqName != null && fqName.length() > 0) {
fqName = fqName + ".";
}
fqName = fqName + resolvedTypeName[0][1];
sigBuilder.append(validator.getSimpleTypeRef(Signature.creat eTypeSignature(fqName,
true)));
}
}
else {
// resolved
sigBuilder.append(validator.getSimpleTypeRef(signature));
}
if (typeArguments != null && typeArguments.length > 0) {
sigBuilder.append("<");
for (int i = 0; i < typeArguments.length; i++) {
if (i > 0) {
sigBuilder.append(", ");
}
sigBuilder.append(getTypeReferenceImpl(typeArguments[i],
type, validator));
}
sigBuilder.append(">");
}
break;
}
for (int i = 0; i < arrayCount; i++) {
sigBuilder.append("[]");
}
return sigBuilder.toString();
}
------------------------------------------------------------ ----------------
In your case replace
'validator.getSimpleTypeRef(Signature.createTypeSignature(fq Name,
true))' by
'Signature.getSignatureQualifier(singleTypeSignature)+Signat ure.getSignatureSimpleName(singleTypeSignature)'
Hope it helps
Andreas
kaprasi wrote:
> Hi Andreas,
>
> Thanks for the reply.
> I couldn't locate resolveType() method for ICompilationUnit.
> But I used your code as a pointer and I think I have more or less nailed
> it :
>
>
> try {
> ICompilationUnit cu = method.getCompilationUnit();
> String[] paramTypes = Signature.getParameterTypes(
> method.getSignature());
> for(String s : paramTypes) {
> if(!isPrimitive(s)) {
> String readebleParamTypeName =
> Signature.getSignatureSimpleName(s);
> IType type =
> method.getDeclaringType();
> String[][] resolvedType =
> type.resolveType(readebleParamTypeName);
> if(resolvedType != null &&
> resolvedType.length > 0) {
> System.out.println("?? : " +
> resolvedType[0][0] + "." + resolvedType[0][1]);
> }
> } else {
> System.out.println("?? : " + Signature.toString(s));
> }
> }
> } catch (JavaModelException e) {
> e.printStackTrace();
> }
>
>
> Would be happy to know if this code can be further corrected.
> Thanks.
|
|
|
Powered by
FUDForum. Page generated in 0.03478 seconds