本文整理匯總了Java中javax.lang.model.element.Element類的典型用法代碼示例。如果您正苦於以下問題:Java Element類的具體用法?Java Element怎麽用?Java Element使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Element類屬於javax.lang.model.element包,在下文中一共展示了Element類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: computeReturn
import javax.lang.model.element.Element; //導入依賴的package包/類
private static List<? extends TypeMirror> computeReturn(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
ReturnTree rt = (ReturnTree) parent.getLeaf();
if (rt.getExpression() == error) {
TreePath method = findMethod(parent);
if (method == null) {
return null;
}
Element el = info.getTrees().getElement(method);
if (el == null || el.getKind() != ElementKind.METHOD) {
return null;
}
types.add(ElementKind.PARAMETER);
types.add(ElementKind.LOCAL_VARIABLE);
types.add(ElementKind.FIELD);
return Collections.singletonList(((ExecutableElement) el).getReturnType());
}
return null;
}
示例2: getAllSelectedElements
import javax.lang.model.element.Element; //導入依賴的package包/類
Set<Element> getAllSelectedElements(DocletEnvironment docenv) {
Set<Element> result = new TreeSet<Element>((Element e1, Element e2) -> {
// some grouping by kind preferred
int rc = e1.getKind().compareTo(e2.getKind());
if (rc != 0) return rc;
rc = e1.toString().compareTo(e2.toString());
if (rc != 0) return rc;
return Integer.compare(e1.hashCode(), e2.hashCode());
});
Set<? extends Element> elements = docenv.getIncludedElements();
for (ModuleElement me : ElementFilter.modulesIn(elements)) {
addEnclosedElements(docenv, result, me);
}
for (PackageElement pe : ElementFilter.packagesIn(elements)) {
ModuleElement mdle = docenv.getElementUtils().getModuleOf(pe);
if (mdle != null)
addEnclosedElements(docenv, result, mdle);
addEnclosedElements(docenv, result, pe);
}
for (TypeElement te : ElementFilter.typesIn(elements)) {
addEnclosedElements(docenv, result, te);
}
return result;
}
示例3: checkElementVisibility
import javax.lang.model.element.Element; //導入依賴的package包/類
/**
* Checks that {@code target} is visible from {@code fromPkg}.
* If {@code fromPkg} is {@code null}, we take that to mean that {@code target} should be visible everywhere.
* Throws an {@link EasyPluginException} with a proper error message if the target element does not match
* the visibility constraint.
* @param eles Elements
* @param target The target element to check for visibility
* @param fromPkg The package to check for visibility from.
* Null indicates it needs to be globally visible.
* @throws EasyPluginException if it's not visible
*/
static void checkElementVisibility(Elements eles, Element target, String fromPkg) {
// I would have used a switch, but that messed up compilation somehow.
// I guess it generated another class file?
// Anyways, this works.
if (target.getKind().isClass() || target.getKind().isInterface()) {
checkClassVisibility(eles, (TypeElement) target, fromPkg);
} else if (target.getKind().isField()
|| target.getKind() == ElementKind.METHOD
|| target.getKind() == ElementKind.CONSTRUCTOR) {
checkMemberVisibility(eles, target, fromPkg);
} else if (target.getKind() == ElementKind.ENUM_CONSTANT) {
checkClassVisibility(eles, (TypeElement) target.getEnclosingElement(), fromPkg);
} else {
// This isn't an EasyPluginException because our code shouldn't be dumb
// enough to check the visibility of any other kind of element.
throw new IllegalArgumentException("Bad kind for element visibility check: " + target.getKind());
}
}
示例4: visitIdentifier
import javax.lang.model.element.Element; //導入依賴的package包/類
@Override
public State visitIdentifier(IdentifierTree node, Void p) {
super.visitIdentifier(node, p);
Element e = info.getTrees().getElement(getCurrentPath());
if (e == null || !isVariableElement(e)) {
return State.POSSIBLE_NULL;
}
if (e.getKind() == ElementKind.ENUM_CONSTANT) {
// enum constants are never null
return State.NOT_NULL;
}
State s = variable2State.get((VariableElement) e);
if (s != null) {
return s;
}
return getStateFromAnnotations(info, e);
}
示例5: deleteDeclIfMatch
import javax.lang.model.element.Element; //導入依賴的package包/類
private void deleteDeclIfMatch(Tree tree, Element elementToFind) {
if (JavaPluginUtils.isSyntheticPath(workingCopy, getCurrentPath())) {
return ;
}
Element el = workingCopy.getTrees().getElement(getCurrentPath());
if (isMatch(el, elementToFind)) {
Tree parent = getCurrentPath().getParentPath().getLeaf();
Tree newOne = null;
if (TreeUtilities.CLASS_TREE_KINDS.contains(parent.getKind())) {
newOne = make.removeClassMember((ClassTree) parent, tree);
} else if (parent.getKind() == Tree.Kind.COMPILATION_UNIT) {
newOne = make.removeCompUnitTypeDecl((CompilationUnitTree) parent, tree);
} else if (tree.getKind() == Tree.Kind.VARIABLE) {
if (parent.getKind() == Tree.Kind.METHOD) {
newOne = make.removeMethodParameter((MethodTree)parent, (VariableTree) tree);
} else {
newOne = make.removeBlockStatement((BlockTree)parent, (VariableTree) tree);
}
}
if (newOne!=null) {
rewrite(parent,newOne);
}
}
}
示例6: visitClass
import javax.lang.model.element.Element; //導入依賴的package包/類
@Override
public Void visitClass(ClassTree tree, Void d) {
Element el = info.getTrees().getElement(getCurrentPath());
if (toFind.equals(el)) {
try {
int[] span = treeUtils.findNameSpan(tree);
if(span != null) {
MutablePositionRegion region = createRegion(doc, span[0], span[1]);
usages.add(region);
}
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);
}
}
handleJavadoc(getCurrentPath());
return super.visitClass(tree, d);
}
示例7: buildOnChangeBinding
import javax.lang.model.element.Element; //導入依賴的package包/類
private void buildOnChangeBinding(MethodSpec.Builder constructorBuilder, Element element) {
OnPrefChange onPrefChange = element.getAnnotation(OnPrefChange.class);
if (onPrefChange != null) {
int[] resourceIds = onPrefChange.value();
for (int resourceId : resourceIds) {
constructorBuilder.addStatement("target.findPreference(target.getString($L))"
+ ".setOnPreferenceChangeListener(new $T(){\n"
+ "@Override public boolean onPreferenceChange($T preference, Object newValue) {\n"
+ "\t\ttarget.$L(preference, newValue);\n"
+ "\t\treturn true;\n"
+ "\t}\n"
+ "})", resourceId, CHANGE_LISTENER, PREFERENCE, element.getSimpleName());
}
}
}
示例8: printDocComment
import javax.lang.model.element.Element; //導入依賴的package包/類
private void printDocComment(Element e) {
String docComment = elementUtils.getDocComment(e);
if (docComment != null) {
// Break comment into lines
StringTokenizer st = new StringTokenizer(docComment,
"\n\r");
indent();
writer.println("/**");
while(st.hasMoreTokens()) {
indent();
writer.print(" *");
writer.println(st.nextToken());
}
indent();
writer.println(" */");
}
}
示例9: getMethodParams
import javax.lang.model.element.Element; //導入依賴的package包/類
private List<ParamInfo> getMethodParams(Element classElement) {
List<ParamInfo> methodParams = new LinkedList<ParamInfo>();
ExecutableElement methodElement = (ExecutableElement) classElement;
for (VariableElement methodParam : methodElement.getParameters()) {
try {
String name = getParamName(methodParam);
ParamInfo.Type type = getParamType(methodParam);
String clsName = getParamClsName(methodParam);
methodParams.add(new ParamInfo(name, type, clsName));
} catch (IllegalArgumentException ex) {
// Do nothing, thrown when we are missing some info.
}
}
return methodParams;
}
示例10: overridableKind
import javax.lang.model.element.Element; //導入依賴的package包/類
private static int overridableKind(Element el) {
if ( el.getModifiers().contains(Modifier.FINAL)
|| el.getModifiers().contains(Modifier.PRIVATE)) {
return -1;
}
if (el.getKind().isClass() || el.getKind().isInterface()) {
return el.getModifiers().contains(Modifier.ABSTRACT) ? 0 : 1;
}
if ( el.getKind() == ElementKind.METHOD
&& !el.getModifiers().contains(Modifier.STATIC)
&& !el.getEnclosingElement().getModifiers().contains(Modifier.FINAL)) {
return el.getModifiers().contains(Modifier.ABSTRACT) ? 2 : 3;
}
return -1;
}
示例11: handleFlowResults
import javax.lang.model.element.Element; //導入依賴的package包/類
private void handleFlowResults(Queue<Env<AttrContext>> queue, ListBuffer<Element> elems) {
for (Env<AttrContext> env: queue) {
switch (env.tree.getTag()) {
case JCTree.CLASSDEF:
JCClassDecl cdef = (JCClassDecl) env.tree;
if (cdef.sym != null)
elems.append(cdef.sym);
break;
case JCTree.TOPLEVEL:
JCCompilationUnit unit = (JCCompilationUnit) env.tree;
if (unit.packge != null)
elems.append(unit.packge);
break;
}
}
genList.addAll(queue);
}
示例12: isElementAvail
import javax.lang.model.element.Element; //導入依賴的package包/類
/**
* Checks if the element is still available. Tests if it is still valid.
* (Was not deleted by matching mechanism.)
* If element is available, returns null, otherwise it creates problem.
* (Helper method for refactoring implementation as this problem is
* general for all refactorings.)
*
* @param e element to check
* @param info
* @return problem message or null if the element is valid
*/
protected static Problem isElementAvail(TreePathHandle e, CompilationInfo info) {
if (e==null) {
//element is null or is not valid.
return new Problem(true, NbBundle.getMessage(FindVisitor.class, "DSC_ElNotAvail")); // NOI18N
} else {
Element el = e.resolveElement(info);
String elName = el != null ? el.getSimpleName().toString() : null;
if (el == null || el.asType().getKind() == TypeKind.ERROR || "<error>".equals(elName)) { // NOI18N
return new Problem(true, NbBundle.getMessage(FindVisitor.class, "DSC_ElementNotResolved"));
}
if ("this".equals(elName) || "super".equals(elName)) { // NOI18N
return new Problem(true, NbBundle.getMessage(FindVisitor.class, "ERR_CannotRefactorThis", el.getSimpleName()));
}
// element is still available
return null;
}
}
示例13: getNodeForElement
import javax.lang.model.element.Element; //導入依賴的package包/類
public ElementNode getNodeForElement(ElementHandle<Element> eh) {
if (getDescritption().elementHandle != null
&& eh.signatureEquals(getDescritption().elementHandle)) {
return this;
}
Children ch = getChildren();
if (ch instanceof ElementChilren) {
for (Node sub : ch.getNodes()) {
ElementNode result = ((ElementNode) sub).getNodeForElement(eh);
if (result != null) {
return result;
}
}
}
return null;
}
示例14: testGoToTypeVariable
import javax.lang.model.element.Element; //導入依賴的package包/類
public void testGoToTypeVariable() throws Exception {
final boolean[] wasCalled = new boolean[1];
performTest("package test; public class Test<TTT> {public void test() {TTT t;}}", 60, new OrigUiUtilsCaller() {
public void open(FileObject fo, int pos) {
assertTrue(source == fo);
assertEquals(32, pos);
wasCalled[0] = true;
}
public void beep() {
fail("Should not be called.");
}
public void open(ClasspathInfo info, Element el) {
fail("Should not be called.");
}
}, false);
assertTrue(wasCalled[0]);
}
示例15: handleFlowResults
import javax.lang.model.element.Element; //導入依賴的package包/類
private void handleFlowResults(Queue<Env<AttrContext>> queue, ListBuffer<Element> elems) {
for (Env<AttrContext> env: queue) {
switch (env.tree.getTag()) {
case CLASSDEF:
JCClassDecl cdef = (JCClassDecl) env.tree;
if (cdef.sym != null)
elems.append(cdef.sym);
break;
case MODULEDEF:
JCModuleDecl mod = (JCModuleDecl) env.tree;
if (mod.sym != null)
elems.append(mod.sym);
break;
case PACKAGEDEF:
JCCompilationUnit unit = env.toplevel;
if (unit.packge != null)
elems.append(unit.packge);
break;
}
}
genList.addAll(queue);
}