|
From: <caw...@us...> - 2007-01-23 01:00:34
|
Revision: 1853
http://svn.sourceforge.net/rubyeclipse/?rev=1853&view=rev
Author: cawilliams
Date: 2007-01-22 17:00:32 -0800 (Mon, 22 Jan 2007)
Log Message:
-----------
start moving towards searching loadpaths for stuff, not using that hack of linking core stubs inside a project
Modified Paths:
--------------
trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/codeassist/RubyElementRequestor.java
trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/RubyElement.java
trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/RubyProject.java
trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultTypeInferrer.java
Removed Paths:
-------------
trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/ExternalPackageFragmentRoot.java
Modified: trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/codeassist/RubyElementRequestor.java
===================================================================
--- trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/codeassist/RubyElementRequestor.java 2007-01-22 23:58:42 UTC (rev 1852)
+++ trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/codeassist/RubyElementRequestor.java 2007-01-23 01:00:32 UTC (rev 1853)
@@ -1,16 +1,7 @@
package org.rubypeople.rdt.internal.codeassist;
-import java.io.File;
-
-import org.eclipse.core.resources.IFolder;
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.resources.ResourceAttributes;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.Path;
import org.rubypeople.rdt.core.IRubyProject;
import org.rubypeople.rdt.core.IType;
-import org.rubypeople.rdt.core.RubyCore;
import org.rubypeople.rdt.core.RubyModelException;
public class RubyElementRequestor {
@@ -19,43 +10,10 @@
public RubyElementRequestor(IRubyProject[] projects) {
this.projects = projects;
- // Get path of folder containing ruby core stubs
- String rootDirName = RubyCore.getOSDirectory(RubyCore.getPlugin());
- String dirName = rootDirName
- + "ruby/lib";
+ }
- File rubyfolder = new File(dirName);
- IPath projectPath = new Path(rubyfolder.getAbsolutePath());
- // XXX This is still a big hack. We're adding the ruby core library on demand. We need to add it when the project is created (and/ore interpreter is installed)
- IFolder folder = projects[0].getProject().getFolder("ruby_core");
- if (!folder.exists()) {
- try {
- folder.createLink(projectPath, IResource.ALLOW_MISSING_LOCAL, null);
- } catch (CoreException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- // Hide ruby_core resource folder
- try {
- ResourceAttributes ra = folder.getResourceAttributes();
- if ( ra != null ) {
-
- //TODO: Doesn't hide & make readonly for some reason?
- ra.setHidden(true);
- ra.setReadOnly(true);
-
- folder.setResourceAttributes(ra);
-
- // Mark ruby_core as derived to keep out of source control
- folder.setDerived(true);
- }
- } catch (CoreException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
+ public RubyElementRequestor(IRubyProject rubyProject) {
+ this(new IRubyProject[] {rubyProject});
}
public IType findType(String typeName) {
Deleted: trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/ExternalPackageFragmentRoot.java
===================================================================
--- trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/ExternalPackageFragmentRoot.java 2007-01-22 23:58:42 UTC (rev 1852)
+++ trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/ExternalPackageFragmentRoot.java 2007-01-23 01:00:32 UTC (rev 1853)
@@ -1,18 +0,0 @@
-package org.rubypeople.rdt.internal.core;
-
-import org.eclipse.core.runtime.IPath;
-import org.rubypeople.rdt.core.ISourceFolderRoot;
-
-public class ExternalPackageFragmentRoot extends SourceFolderRoot implements
- ISourceFolderRoot {
-
- protected ExternalPackageFragmentRoot(IPath resource,
- RubyProject project) {
- super(null, project);
- }
-
- @Override
- public boolean isExternal() {
- return true;
- }
-}
Modified: trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/RubyElement.java
===================================================================
--- trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/RubyElement.java 2007-01-22 23:58:42 UTC (rev 1852)
+++ trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/RubyElement.java 2007-01-23 01:00:32 UTC (rev 1853)
@@ -26,6 +26,7 @@
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
@@ -211,10 +212,10 @@
*
* @param type - one of the type constants defined by RubyElement
*/
- public ArrayList getChildrenOfType(int type) throws RubyModelException {
+ public ArrayList<IRubyElement> getChildrenOfType(int type) throws RubyModelException {
IRubyElement[] children = getChildren();
int size = children.length;
- ArrayList list = new ArrayList(size);
+ ArrayList<IRubyElement> list = new ArrayList<IRubyElement>(size);
for (int i = 0; i < size; ++i) {
RubyElement elt = (RubyElement)children[i];
if (elt.getElementType() == type) {
Modified: trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/RubyProject.java
===================================================================
--- trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/RubyProject.java 2007-01-22 23:58:42 UTC (rev 1852)
+++ trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/RubyProject.java 2007-01-23 01:00:32 UTC (rev 1853)
@@ -70,7 +70,6 @@
public class RubyProject extends Openable implements IProjectNature, IRubyElement, IRubyProject {
protected IProject project;
- protected List loadPathEntries;
protected boolean scratched;
/**
@@ -379,17 +378,36 @@
*/
public IType findType(String fullyQualifiedName) {
int index = fullyQualifiedName.lastIndexOf("::");
- String className = null, packageName = null;
+ String className = null;
if (index == -1) {
- packageName = "";
className = fullyQualifiedName;
} else {
- packageName = fullyQualifiedName.substring(0, index);
className = fullyQualifiedName.substring(index + 2);
}
- // FIXME Handle the namespaces properly. we ignore them so far!
- return searchChildren(this, className);
+ // XXX Use the imports to search the path properly, then do an exhaustive search if that fails
+ IType child = searchChildren(this, className);
+ if (child != null) return child;
+ try {
+ ILoadpathEntry[] loadpaths = getResolvedLoadpath(true);
+ for (int i = 0; i < loadpaths.length; i++) {
+ ILoadpathEntry entry = loadpaths[i];
+ IPath path = entry.getPath();
+ SourceFolderRoot root = new ExternalSourceFolderRoot(path, this);
+ List<IRubyElement> childen = root.getChildrenOfType(IRubyElement.TYPE);
+ for (IRubyElement element : childen) {
+ if (element.isType(IRubyElement.TYPE)) {
+ IType aType = (IType) element;
+ if (aType.getElementName().equals(className)) {
+ return aType;
+ }
+ }
+ }
+ }
+ } catch (RubyModelException e) {
+ e.printStackTrace();
+ }
+ return null;
}
/**
@@ -578,7 +596,7 @@
} else {
// external target
if (RubyModel.isFile(target)) {
- root = new ExternalPackageFragmentRoot(entryPath, this);
+ root = new ExternalSourceFolderRoot(entryPath, this);
}
}
} else {
@@ -2045,7 +2063,7 @@
}
private ISourceFolderRoot getPackageFragmentRoot0(IPath path) {
- return new ExternalPackageFragmentRoot(path, this);
+ return new ExternalSourceFolderRoot(path, this);
}
/*
Modified: trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultTypeInferrer.java
===================================================================
--- trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultTypeInferrer.java 2007-01-22 23:58:42 UTC (rev 1852)
+++ trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultTypeInferrer.java 2007-01-23 01:00:32 UTC (rev 1853)
@@ -26,284 +26,321 @@
import org.rubypeople.rdt.internal.ti.util.OffsetNodeLocator;
public class DefaultTypeInferrer implements ITypeInferrer {
-
+
private RootNode rootNode;
/**
* Infers type inside the source at given offset.
+ *
* @return List of ITypeGuess objects.
*/
public List<ITypeGuess> infer(String source, int offset) {
RubyParser parser = new RubyParser();
rootNode = (RootNode) parser.parse(source);
Node node = OffsetNodeLocator.Instance().getNodeAtOffset(rootNode.getBodyNode(), offset);
-
- if ( node == null )
- {
+
+ if (node == null) {
return null;
}
-
-// System.out.println("offset: " + offset + ": " + node.getClass().getName());
+ // System.out.println("offset: " + offset + ": " +
+ // node.getClass().getName());
+
return infer(node);
}
-
+
/**
* Infers the type of the specified node.
- * @param node Node to infer type of.
+ *
+ * @param node
+ * Node to infer type of.
* @return List of ITypeGuess objects.
*/
- private List<ITypeGuess> infer(Node node)
- {
+ private List<ITypeGuess> infer(Node node) {
List<ITypeGuess> guesses = new LinkedList<ITypeGuess>();
tryConstantNode(node, guesses);
tryAsgnNode(node, guesses);
-
- //todo: refactor these 3 by common features into 1 (or 1+3) method(s)
+
+ // todo: refactor these 3 by common features into 1 (or 1+3) method(s)
tryLocalVarNode(node, guesses);
tryInstVarNode(node, guesses);
tryGlobalVarNode(node, guesses);
-
+
tryWellKnownMethodCalls(node, guesses);
-
+
return guesses;
}
-
+
/**
* Infers type if node is a constant node; i.e. 5, 'foo', [1,2,3]
- * @param node Node to infer type of.
- * @param guesses List of ITypeGuess objects to insert guesses into.
+ *
+ * @param node
+ * Node to infer type of.
+ * @param guesses
+ * List of ITypeGuess objects to insert guesses into.
*/
- private void tryConstantNode(Node node, List<ITypeGuess>guesses)
- {
+ private void tryConstantNode(Node node, List<ITypeGuess> guesses) {
// Try seeing if the rvalue is a constant (5, "foo", [1,2,3], etc.)
- String concreteGuess = ConstNodeTypeNames.get(node.getClass().getSimpleName());
- if ( concreteGuess != null )
- {
- guesses.add( new BasicTypeGuess( concreteGuess, 100 ) );
+ String concreteGuess = ConstNodeTypeNames.get(node.getClass().getSimpleName());
+ if (concreteGuess != null) {
+ guesses.add(new BasicTypeGuess(concreteGuess, 100));
}
}
-
+
/**
- * Infers type if node is an assignment node; i.e. x = 5, @y = 'foo', $z = [1,2,3]
- * @param node Node to infer type of.
- * @param guesses List of ITypeGuess objects to insert guesses into.
+ * Infers type if node is an assignment node; i.e. x = 5,
+ *
+ * @y = 'foo', $z = [1,2,3]
+ * @param node
+ * Node to infer type of.
+ * @param guesses
+ * List of ITypeGuess objects to insert guesses into.
*/
- private void tryAsgnNode(Node node, List<ITypeGuess>guesses)
- {
+ private void tryAsgnNode(Node node, List<ITypeGuess> guesses) {
Node valueNode = null;
-
- if ( node instanceof LocalAsgnNode )
- {
- valueNode = ((LocalAsgnNode)node).getValueNode();
+
+ if (node instanceof LocalAsgnNode) {
+ valueNode = ((LocalAsgnNode) node).getValueNode();
}
- if ( node instanceof InstAsgnNode)
- {
- valueNode = ((InstAsgnNode)node).getValueNode();
+ if (node instanceof InstAsgnNode) {
+ valueNode = ((InstAsgnNode) node).getValueNode();
}
- if ( node instanceof GlobalAsgnNode)
- {
- valueNode = ((GlobalAsgnNode)node).getValueNode();
+ if (node instanceof GlobalAsgnNode) {
+ valueNode = ((GlobalAsgnNode) node).getValueNode();
}
- if ( valueNode != null )
- {
+ if (valueNode != null) {
guesses.addAll(infer(valueNode));
}
}
-
- private void tryInstVarNode(Node node, List<ITypeGuess> guesses)
- {
- if ( node instanceof InstVarNode )
- {
- final InstVarNode instVarNode = (InstVarNode)node;
+
+ private void tryInstVarNode(Node node, List<ITypeGuess> guesses) {
+ if (node instanceof InstVarNode) {
+ final InstVarNode instVarNode = (InstVarNode) node;
int nodeStart = node.getPosition().getStartOffset();
-
- //todo: see if there is attr_reader/attr_writer, maybe?
- //todo: find calls to the reader/writers
- //todo: for STI on InstVar, find references within this ClassNode to this InstVar... record 'em
-
- // Find first assignment to this var name that occurs before the reference
- //todo: This will find assignments in other local scopes that precede this reference but have the same variable name.
- // To mitigate, ensure that the closest spanning ScopeNode for both this LocalVarNode and the AsgnNode are the name ScopeNode.
- // Or scopingNode. Still not sure whether IterNodes count or not... silly block-local-var ambiguity ;)
- Node initialAssignmentNode = FirstPrecursorNodeLocator.Instance().findFirstPrecursor( rootNode, nodeStart, new INodeAcceptor(){
+
+ // todo: see if there is attr_reader/attr_writer, maybe?
+ // todo: find calls to the reader/writers
+ // todo: for STI on InstVar, find references within this ClassNode
+ // to this InstVar... record 'em
+
+ // Find first assignment to this var name that occurs before the
+ // reference
+ // todo: This will find assignments in other local scopes that
+ // precede this reference but have the same variable name.
+ // To mitigate, ensure that the closest spanning ScopeNode for both
+ // this LocalVarNode and the AsgnNode are the name ScopeNode.
+ // Or scopingNode. Still not sure whether IterNodes count or not...
+ // silly block-local-var ambiguity ;)
+ Node initialAssignmentNode = FirstPrecursorNodeLocator.Instance().findFirstPrecursor(rootNode, nodeStart, new INodeAcceptor() {
public boolean doesAccept(Node node) {
String name = null;
- if ( node instanceof LocalAsgnNode ) name = ((LocalAsgnNode)node).getName();
- if ( node instanceof InstAsgnNode ) name = ((InstAsgnNode)node).getName();
- if ( node instanceof GlobalAsgnNode ) name = ((GlobalAsgnNode)node).getName();
- return ( name != null && name.equals(instVarNode.getName())); /** refactor to common INodeAcceptor for instVarName,localVarName,globalVarName*/
+ if (node instanceof LocalAsgnNode)
+ name = ((LocalAsgnNode) node).getName();
+ if (node instanceof InstAsgnNode)
+ name = ((InstAsgnNode) node).getName();
+ if (node instanceof GlobalAsgnNode)
+ name = ((GlobalAsgnNode) node).getName();
+ return (name != null && name.equals(instVarNode.getName()));
+ /**
+ * refactor to common INodeAcceptor for
+ * instVarName,localVarName,globalVarName
+ */
}
});
- if ( initialAssignmentNode != null )
- {
+ if (initialAssignmentNode != null) {
tryAsgnNode(initialAssignmentNode, guesses);
}
}
}
-
- private void tryGlobalVarNode(Node node, List<ITypeGuess> guesses)
- {
- if ( node instanceof GlobalVarNode )
- {
- final GlobalVarNode globalVarNode = (GlobalVarNode)node;
+
+ private void tryGlobalVarNode(Node node, List<ITypeGuess> guesses) {
+ if (node instanceof GlobalVarNode) {
+ final GlobalVarNode globalVarNode = (GlobalVarNode) node;
int nodeStart = node.getPosition().getStartOffset();
-
- //todo: for STI on GlobalVar, find references within this ClassNode to this GlobalVar... record 'em
- //todo: p.s. globals are low-priority.
-
- // Find first assignment to this var name that occurs before the reference
- //todo: This will find assignments in other local scopes that precede this reference but have the same variable name.
- // To mitigate, ensure that the closest spanning ScopeNode for both this LocalVarNode and the AsgnNode are the name ScopeNode.
- // Or scopingNode. Still not sure whether IterNodes count or not... silly block-local-var ambiguity ;)
- Node initialAssignmentNode = FirstPrecursorNodeLocator.Instance().findFirstPrecursor( rootNode, nodeStart, new INodeAcceptor(){
+
+ // todo: for STI on GlobalVar, find references within this ClassNode
+ // to this GlobalVar... record 'em
+ // todo: p.s. globals are low-priority.
+
+ // Find first assignment to this var name that occurs before the
+ // reference
+ // todo: This will find assignments in other local scopes that
+ // precede this reference but have the same variable name.
+ // To mitigate, ensure that the closest spanning ScopeNode for both
+ // this LocalVarNode and the AsgnNode are the name ScopeNode.
+ // Or scopingNode. Still not sure whether IterNodes count or not...
+ // silly block-local-var ambiguity ;)
+ Node initialAssignmentNode = FirstPrecursorNodeLocator.Instance().findFirstPrecursor(rootNode, nodeStart, new INodeAcceptor() {
public boolean doesAccept(Node node) {
String name = null;
- if ( node instanceof LocalAsgnNode ) name = ((LocalAsgnNode)node).getName();
- if ( node instanceof InstAsgnNode ) name = ((InstAsgnNode)node).getName();
- if ( node instanceof GlobalAsgnNode ) name = ((GlobalAsgnNode)node).getName();
- return ( name != null && name.equals(globalVarNode.getName())); /** refactor to common INodeAcceptor for instVarName,localVarName,globalVarName*/
+ if (node instanceof LocalAsgnNode)
+ name = ((LocalAsgnNode) node).getName();
+ if (node instanceof InstAsgnNode)
+ name = ((InstAsgnNode) node).getName();
+ if (node instanceof GlobalAsgnNode)
+ name = ((GlobalAsgnNode) node).getName();
+ return (name != null && name.equals(globalVarNode.getName()));
+ /**
+ * refactor to common INodeAcceptor for
+ * instVarName,localVarName,globalVarName
+ */
}
});
- if ( initialAssignmentNode != null )
- {
+ if (initialAssignmentNode != null) {
tryAsgnNode(initialAssignmentNode, guesses);
}
}
}
-
- private void tryLocalVarNode(Node node, List<ITypeGuess> guesses)
- {
- //System.out.println(node.getClass().getName());
- if ( node instanceof LocalVarNode )
- {
- LocalVarNode localVarNode = (LocalVarNode)node;
+
+ private void tryLocalVarNode(Node node, List<ITypeGuess> guesses) {
+ // System.out.println(node.getClass().getName());
+ if (node instanceof LocalVarNode) {
+ LocalVarNode localVarNode = (LocalVarNode) node;
int nodeStart = node.getPosition().getStartOffset();
final String localVarName = TypeInferenceHelper.Instance().getVarName(localVarNode);
- // See if it has been assigned to, earlier [todo: in this local scope].
- // Find first assignment to this var name that occurs before the reference
- //todo: This will find assignments in other local scopes that precede this reference but have the same variable name.
- // To mitigate, ensure that the closest spanning ScopeNode for both this LocalVarNode and the AsgnNode are the name ScopeNode.
- // Or scopingNode. Still not sure whether IterNodes count or not... silly block-local-var ambiguity ;)
- Node initialAssignmentNode = FirstPrecursorNodeLocator.Instance().findFirstPrecursor( rootNode, nodeStart, new INodeAcceptor(){
+ // See if it has been assigned to, earlier [todo: in this local
+ // scope].
+ // Find first assignment to this var name that occurs before the
+ // reference
+ // todo: This will find assignments in other local scopes that
+ // precede this reference but have the same variable name.
+ // To mitigate, ensure that the closest spanning ScopeNode for both
+ // this LocalVarNode and the AsgnNode are the name ScopeNode.
+ // Or scopingNode. Still not sure whether IterNodes count or not...
+ // silly block-local-var ambiguity ;)
+ Node initialAssignmentNode = FirstPrecursorNodeLocator.Instance().findFirstPrecursor(rootNode, nodeStart, new INodeAcceptor() {
public boolean doesAccept(Node node) {
String name = null;
- if ( node instanceof LocalAsgnNode ) name = ((LocalAsgnNode)node).getName();
- if ( node instanceof InstAsgnNode ) name = ((InstAsgnNode)node).getName();
- if ( node instanceof GlobalAsgnNode ) name = ((GlobalAsgnNode)node).getName();
- return ( name != null && name.equals(localVarName));
+ if (node instanceof LocalAsgnNode)
+ name = ((LocalAsgnNode) node).getName();
+ if (node instanceof InstAsgnNode)
+ name = ((InstAsgnNode) node).getName();
+ if (node instanceof GlobalAsgnNode)
+ name = ((GlobalAsgnNode) node).getName();
+ return (name != null && name.equals(localVarName));
}
});
- if ( initialAssignmentNode != null )
- {
+ if (initialAssignmentNode != null) {
tryAsgnNode(initialAssignmentNode, guesses);
}
// See if it is a param into this scope
- ArgsNode argsNode = (ArgsNode)FirstPrecursorNodeLocator.Instance().findFirstPrecursor(rootNode, nodeStart, new INodeAcceptor(){
+ ArgsNode argsNode = (ArgsNode) FirstPrecursorNodeLocator.Instance().findFirstPrecursor(rootNode, nodeStart, new INodeAcceptor() {
public boolean doesAccept(Node node) {
- return ( (node instanceof ArgsNode) && (doesArgsNodeContainsVariable((ArgsNode)node, localVarName)));
+ return ((node instanceof ArgsNode) && (doesArgsNodeContainsVariable((ArgsNode) node, localVarName)));
}
});
// If so, find its enclosing method
- if ( argsNode != null )
- {
- int argNumber = getArgumentIndex(argsNode,localVarName);
- //System.out.println("Variable " + localVarName + " is the " + argNumber + "th argument to the enclosing method ");
-
+ if (argsNode != null) {
+ int argNumber = getArgumentIndex(argsNode, localVarName);
+ // System.out.println("Variable " + localVarName + " is the " +
+ // argNumber + "th argument to the enclosing method ");
+
// Find enclosing method
- Node defNode = FirstPrecursorNodeLocator.Instance().findFirstPrecursor(rootNode, nodeStart, new INodeAcceptor(){
+ Node defNode = FirstPrecursorNodeLocator.Instance().findFirstPrecursor(rootNode, nodeStart, new INodeAcceptor() {
public boolean doesAccept(Node node) {
- //System.out.println("Looking for enclosing method, checking: " + node.getClass().getName() + "[" + node.getPosition().getStartOffset() + ".." + node.getPosition().getEndOffset() + "]" );
+ // System.out.println("Looking for enclosing method,
+ // checking: " + node.getClass().getName() + "[" +
+ // node.getPosition().getStartOffset() + ".." +
+ // node.getPosition().getEndOffset() + "]" );
ArgsNode argsNode = null;
- if ( node instanceof DefnNode ) argsNode = ((DefnNode)node).getArgsNode();
- if ( node instanceof DefsNode ) argsNode = ((DefsNode)node).getArgsNode();
- return ( (argsNode != null) && (doesArgsNodeContainsVariable(argsNode, localVarName)));
+ if (node instanceof DefnNode)
+ argsNode = ((DefnNode) node).getArgsNode();
+ if (node instanceof DefsNode)
+ argsNode = ((DefsNode) node).getArgsNode();
+ return ((argsNode != null) && (doesArgsNodeContainsVariable(argsNode, localVarName)));
}
});
- if ( defNode != null )
- {
+ if (defNode != null) {
String methodName = null;
- if ( defNode instanceof DefnNode ) methodName = ((DefnNode)defNode).getName();
- if ( defNode instanceof DefsNode ) methodName = ((DefsNode)defNode).getName();
+ if (defNode instanceof DefnNode)
+ methodName = ((DefnNode) defNode).getName();
+ if (defNode instanceof DefsNode)
+ methodName = ((DefsNode) defNode).getName();
- //System.out.println("Variable " + localVarName + " is the " + argNumber + "th argument to method " + methodName );
-
+ // System.out.println("Variable " + localVarName + " is the
+ // " + argNumber + "th argument to method " + methodName );
+
// Find all invocations of the surrounding method.
- //todo: from easiest to hardest:
- // It may be a global function, where simply a CallNode where method name must be matched.
- // It may be a DefsNode static class method, where a CallNode whose receiverNode is a ConstNode whose name is the surrounding class
- // It may be an DefnNode method defined in a class, where a CallNode whose receiverNode must be type-matched to the surrounding class
-
+ // todo: from easiest to hardest:
+ // It may be a global function, where simply a CallNode
+ // where method name must be matched.
+ // It may be a DefsNode static class method, where a
+ // CallNode whose receiverNode is a ConstNode whose name is
+ // the surrounding class
+ // It may be an DefnNode method defined in a class, where a
+ // CallNode whose receiverNode must be type-matched to the
+ // surrounding class
+
}
}
}
}
-
- private void tryWellKnownMethodCalls(Node node, List<ITypeGuess> guesses)
- {
- if ( node instanceof CallNode )
- {
- CallNode callNode = (CallNode)node;
-
+
+ private void tryWellKnownMethodCalls(Node node, List<ITypeGuess> guesses) {
+ if (node instanceof CallNode) {
+ CallNode callNode = (CallNode) node;
+
String method = callNode.getName();
- if ( method.equals("new") && callNode.getReceiverNode() instanceof ConstNode)
- {
- guesses.add( new BasicTypeGuess( ((ConstNode)callNode.getReceiverNode()).getName() , 100 ) );
- }
- else
- {
-//todo: this NEEDS to be done with a multimap and various confidences for each. i.e. X.slice, X is 50/50 Array or String
+ if (method.equals("new") && callNode.getReceiverNode() instanceof ConstNode) {
+ guesses.add(new BasicTypeGuess(((ConstNode) callNode.getReceiverNode()).getName(), 100));
+ } else {
+ // todo: this NEEDS to be done with a multimap and various
+ // confidences for each. i.e. X.slice, X is 50/50 Array or
+ // String
String methodReturnTypeGuess = TypicalMethodReturnNames.get(method);
- if ( methodReturnTypeGuess != null )
- {
- guesses.add( new BasicTypeGuess( methodReturnTypeGuess, 100 ) );
+ if (methodReturnTypeGuess != null) {
+ guesses.add(new BasicTypeGuess(methodReturnTypeGuess, 100));
}
}
}
}
-
/**
* Determine whether an ArgsNode contains a particular named argument
- * @param argsNode ArgsNode to search
- * @param argName Name of argument to find
+ *
+ * @param argsNode
+ * ArgsNode to search
+ * @param argName
+ * Name of argument to find
* @return
*/
- private boolean doesArgsNodeContainsVariable(ArgsNode argsNode, String argName)
- {
+ private boolean doesArgsNodeContainsVariable(ArgsNode argsNode, String argName) {
return getArgumentIndex(argsNode, argName) >= 0;
}
-
+
/**
- * Finds the index of an argument in an ArgsNode by name, -1 if it is not contained.
- * @param argsNode ArgsNode to search
- * @param argName Name of argument to find
+ * Finds the index of an argument in an ArgsNode by name, -1 if it is not
+ * contained.
+ *
+ * @param argsNode
+ * ArgsNode to search
+ * @param argName
+ * Name of argument to find
* @return Index of argName in argsNode or -1 if it is not there.
*/
- private int getArgumentIndex(ArgsNode argsNode, String argName)
- {
+ private int getArgumentIndex(ArgsNode argsNode, String argName) {
int argNumber = 0;
- for ( Iterator iter = argsNode.getArgs().iterator(); iter.hasNext();) {
- if (((ArgumentNode)iter.next()).getName().equals(argName)) { break; }
+ for (Iterator iter = argsNode.getArgs().iterator(); iter.hasNext();) {
+ if (((ArgumentNode) iter.next()).getName().equals(argName)) {
+ break;
+ }
argNumber++;
}
- if ( argNumber == argsNode.getArgsCount() )
- {
+ if (argNumber == argsNode.getArgsCount()) {
return -1;
}
return argNumber;
}
-
-
+
public static void main(String[] args) {
ITypeInferrer dti = new DefaultTypeInferrer();
- List<ITypeGuess> guesses = dti.infer("'string'",3);
-
+ List<ITypeGuess> guesses = dti.infer("'string'", 3);
+
for (ITypeGuess guess : guesses) {
- System.out.println("Type guess: " + guess.getType() + ", " + guess.getConfidence() + "%" );
+ System.out.println("Type guess: " + guess.getType() + ", " + guess.getConfidence() + "%");
}
-
+
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|