blob: 59c0a9e189fda4de3bc2194720ea48f6063e12f3 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
--><mediawiki xmlns="http://www.mediawiki.org/xml/export-0.3/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="0.3" xml:lang="en" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.3/ http://www.mediawiki.org/xml/export-0.3.xsd">
<siteinfo>
<sitename>NetBeans Wiki</sitename>
<base>http://wiki.netbeans.org/Main_Page</base>
<generator>MediaWiki 1.15.1</generator>
<case>first-letter</case>
<namespaces>
<namespace key="-2">Media</namespace>
<namespace key="-1">Special</namespace>
<namespace key="0"/>
<namespace key="1">Talk</namespace>
<namespace key="2">User</namespace>
<namespace key="3">User talk</namespace>
<namespace key="4">NetBeans Wiki</namespace>
<namespace key="5">NetBeans Wiki talk</namespace>
<namespace key="6">File</namespace>
<namespace key="7">File talk</namespace>
<namespace key="8">MediaWiki</namespace>
<namespace key="9">MediaWiki talk</namespace>
<namespace key="10">Template</namespace>
<namespace key="11">Template talk</namespace>
<namespace key="12">Help</namespace>
<namespace key="13">Help talk</namespace>
<namespace key="14">Category</namespace>
<namespace key="15">Category talk</namespace>
</namespaces>
</siteinfo>
<page>
<title>JavaHT GetAllMembers</title>
<id>9412</id>
<revision>
<id>55764</id>
<timestamp>2012-07-10T14:37:39Z</timestamp>
<contributor>
<username>Javydreamercsw</username>
<id>2603</id>
</contributor>
<minor/>
<comment>/* How do I Get All Methods/Fields/Constructors of a Class? */</comment>
<text xml:space="preserve">__NOTOC__
====How do I Get All Methods/Fields/Constructors of a Class?====
* First, you need to be in a Javac context, see [[JavaHT_GetIntoJavacContext | previous section]] for more information.
* Then, you need to find &lt;tt&gt;javax.lang.model.element.TypeElement&lt;/tt&gt; you want to analyze. See &lt;tt&gt;com.sun.source.tree.Trees.getElement(TreePath)&lt;/tt&gt; and &lt;tt&gt;javax.lang.model.util.Elements.getTypeElement(String)&lt;/tt&gt;. You can get &lt;tt&gt;Trees&lt;/tt&gt; and &lt;tt&gt;Elements&lt;/tt&gt; from &lt;tt&gt;org.netbeans.api.java.source.CompilationInfo&lt;/tt&gt;.
* Finally, use &lt;tt&gt;Element.getEnclosedElements()&lt;/tt&gt; to find out the elements enclosed by the class - for classes, this returns all members (methods, fields and inner classes) of the class. You can then use &lt;tt&gt;ElementFilter&lt;/tt&gt; to filter out specific kind of member: methods, constructors, fields and inner classes.
Example:
&lt;pre&gt;
protected void performAction(Node[] activatedNodes) {
DataObject dataObject = (DataObject) activatedNodes[0].getLookup().lookup(DataObject.class);
JavaSource js = JavaSource.forFileObject(dataObject.getPrimaryFile());
try {
js.runUserActionTask(new Task&lt;CompilationController&gt;() {
public void run(CompilationController parameter) throws IOException {
parameter.toPhase(Phase.ELEMENTS_RESOLVED);
new MemberVisitor(parameter).scan(parameter.getCompilationUnit(), null);
}
}, true);
}
catch (IOException e) {
Logger.getLogger("global").log(Level.SEVERE, e.getMessage(), e);
}
}
private static class MemberVisitor extends TreePathScanner&lt;Void, Void&gt; {
private CompilationInfo info;
public MemberVisitor(CompilationInfo info) {
this.info = info;
}
@Override
public Void visitClass(ClassTree t, Void v) {
Element el = info.getTrees().getElement(getCurrentPath());
if (el == null) {
System.err.println("Cannot resolve class!");
}
else {
TypeElement te = (TypeElement) el;
System.err.println("Resolved class: " + te.getQualifiedName().toString());
//XXX: only as an example, uses toString on element, which should be used only for debugging
System.err.println("enclosed methods: " + ElementFilter.methodsIn(te.getEnclosedElements()));
System.err.println("enclosed types: " + ElementFilter.typesIn(te.getEnclosedElements()));
}
return null;
}
}
&lt;/pre&gt;</text>
</revision>
</page>
</mediawiki>