本文整理匯總了Java中javax.lang.model.element.Element.getEnclosingElement方法的典型用法代碼示例。如果您正苦於以下問題:Java Element.getEnclosingElement方法的具體用法?Java Element.getEnclosingElement怎麽用?Java Element.getEnclosingElement使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.lang.model.element.Element
的用法示例。
在下文中一共展示了Element.getEnclosingElement方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testSynteticDefaultConstructor
import javax.lang.model.element.Element; //導入方法依賴的package包/類
public void testSynteticDefaultConstructor() throws Exception {
performTest("SynteticDefaultConstructor");
source.runModificationTask(new Task<WorkingCopy>() {
public void run(WorkingCopy copy) throws IOException {
copy.toPhase(Phase.RESOLVED);
ClassTree topLevel = findTopLevelClass(copy);
SourceUtilsTestUtil2.run(copy, new AddSimpleField(), topLevel);
}
}).commit();
JavaSourceAccessor.getINSTANCE().revalidate(source);
CompilationInfo check = SourceUtilsTestUtil.getCompilationInfo(source, Phase.RESOLVED);
CompilationUnitTree cu = check.getCompilationUnit();
assertEquals(check.getDiagnostics().toString(), 0, check.getDiagnostics().size());
ClassTree newTopLevel = findTopLevelClass(check);
Element clazz = check.getTrees().getElement(TreePath.getPath(cu, newTopLevel));
Element pack = clazz.getEnclosingElement();
assertEquals(ElementKind.PACKAGE, pack.getKind());
assertEquals("test", ((PackageElement) pack).getQualifiedName().toString());
assertEquals(clazz.getEnclosedElements().toString(), 2 + 1/*syntetic default constructor*/, clazz.getEnclosedElements().size());
}
示例2: testEmptyClass
import javax.lang.model.element.Element; //導入方法依賴的package包/類
public void testEmptyClass() throws Exception {
performTest("EmptyClass");
source.runModificationTask(new Task<WorkingCopy>() {
public void run(WorkingCopy copy) throws IOException {
copy.toPhase(Phase.RESOLVED);
ClassTree topLevel = findTopLevelClass(copy);
SourceUtilsTestUtil2.run(copy, new AddSimpleField(), topLevel);
}
}).commit();
JavaSourceAccessor.getINSTANCE().revalidate(source);
CompilationInfo check = SourceUtilsTestUtil.getCompilationInfo(source, Phase.RESOLVED);
CompilationUnitTree cu = check.getCompilationUnit();
assertEquals(check.getDiagnostics().toString(), 0, check.getDiagnostics().size());
ClassTree newTopLevel = findTopLevelClass(check);
Element clazz = check.getTrees().getElement(TreePath.getPath(cu, newTopLevel));
Element pack = clazz.getEnclosingElement();
assertEquals(ElementKind.PACKAGE, pack.getKind());
assertEquals("test", ((PackageElement) pack).getQualifiedName().toString());
assertEquals(clazz.getEnclosedElements().toString(), 1 + 1/*syntetic default constructor*/, clazz.getEnclosedElements().size());
}
示例3: categories
import javax.lang.model.element.Element; //導入方法依賴的package包/類
/**
* Categories field, find his papa.
*
* @param elements Field need autowired
*/
private void categories(Set<? extends Element> elements) throws IllegalAccessException {
if (CollectionUtils.isNotEmpty(elements)) {
for (Element element : elements) {
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
if (element.getModifiers().contains(Modifier.PRIVATE)) {
throw new IllegalAccessException("The autowired fields CAN NOT BE 'private'!!! please check field ["
+ element.getSimpleName() + "] in class [" + enclosingElement.getQualifiedName() + "]");
}
if (parentAndChild.containsKey(enclosingElement)) { // Has categries
parentAndChild.get(enclosingElement).add(element);
} else {
List<Element> childs = new ArrayList<>();
childs.add(element);
parentAndChild.put(enclosingElement, childs);
}
}
logger.info("categories finished.");
}
}
示例4: findActivityInfo
import javax.lang.model.element.Element; //導入方法依賴的package包/類
public ActivityInfo findActivityInfo(Element element) {
TypeElement parent = (TypeElement) element.getEnclosingElement();
boolean isInActivity = isSubtype(parent, SUPER_ACTIVITY_FULL_NAME);
ActivityInfo activityInfo = null;
if (isInActivity) {
String activityName = parent.getQualifiedName().toString();
activityInfo = findActivityByName(activityName);
if (activityInfo == null) {
printError(element, "activity without com.bboylin.Annotation.Router annotation error for %s", activityName);
throw new IllegalStateException();
}
} else {
printError(element, "the param annotation should only be used in activity");
}
return activityInfo;
}
示例5: isEnclosingType
import javax.lang.model.element.Element; //導入方法依賴的package包/類
protected boolean isEnclosingType(TypeElement el) {
if (el == null) {
return false;
}
Element e = enclosingElement;
while (e != null && e != el) {
e = e.getEnclosingElement();
}
return e != null;
}
示例6: getEnclosingPackageOrClass
import javax.lang.model.element.Element; //導入方法依賴的package包/類
private Element getEnclosingPackageOrClass(Element e) {
while (e != null) {
switch (e.getKind()) {
case CLASS:
case ENUM:
case INTERFACE:
case PACKAGE:
return e;
default:
e = e.getEnclosingElement();
}
}
return e;
}
示例7: getEnclosingElements
import javax.lang.model.element.Element; //導入方法依賴的package包/類
private List<Element> getEnclosingElements() {
List<Element> elements = new ArrayList<>();
Element enclosing = type.getEnclosingElement();
while (enclosing != null && !(enclosing instanceof PackageElement)) {
elements.add(enclosing);
enclosing = enclosing.getEnclosingElement();
}
return elements;
}
示例8: run
import javax.lang.model.element.Element; //導入方法依賴的package包/類
@Override
public List<Fix> run(CompilationInfo info, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
Element el = info.getTrees().getElement(treePath);
if (el == null || el.asType().getKind() == TypeKind.ERROR) return null;
if (el.getEnclosingElement() == null || !(el.getEnclosingElement().getKind().isClass() || el.getEnclosingElement().getKind().isInterface())) return null;
TypeElement targetEnclosing = (TypeElement) el.getEnclosingElement();
if (!Utilities.isTargetWritable(targetEnclosing, info)) return null;
TreePath outtermostClassPath = null;
TreePath up = treePath;
while (up != null) {
if (TreeUtilities.CLASS_TREE_KINDS.contains(up.getLeaf().getKind())) {
outtermostClassPath = up;
}
up = up.getParentPath();
}
Element outtermostClassEl = outtermostClassPath != null ? info.getTrees().getElement(outtermostClassPath) : null;
if (outtermostClassEl == null || !(outtermostClassEl.getKind().isClass() || outtermostClassEl.getKind().isInterface())) return null;
TypeElement outtermostClass = (TypeElement) outtermostClassEl;
Visibility newVisibility = Utilities.getAccessModifiers(info, outtermostClass, targetEnclosing);
if (newVisibility.ordinal() <= Visibility.forElement(el).ordinal() || newVisibility == Visibility.PRIVATE) return null; //should not happen?
return Collections.<Fix>singletonList(new FixImpl(info, el, newVisibility).toEditorFix());
}
示例9: computeKey
import javax.lang.model.element.Element; //導入方法依賴的package包/類
String computeKey(Element e) {
StringBuilder buf = new StringBuilder();
while (e != null) {
buf.append(e.toString());
e = e.getEnclosingElement();
}
buf.append(jfo.getName());
return buf.toString();
}
示例10: findPackageJackCompiler
import javax.lang.model.element.Element; //導入方法依賴的package包/類
private String findPackageJackCompiler(Element element) {
Element enclosingElement = element.getEnclosingElement();
if (enclosingElement != null && enclosingElement.getKind() == ElementKind.PACKAGE && element.getKind() == ElementKind.CLASS) {
String className = element.asType().toString();
int index = className.lastIndexOf('.');
if (index <= 0) {
mMessager.printMessage(Diagnostic.Kind.WARNING, "Couldn't find package name with Jack compiler");
return null;
}
return className.substring(0, index);
}
return enclosingElement != null ? findPackageJackCompiler(enclosingElement) : null;
}
示例11: packageNameOf
import javax.lang.model.element.Element; //導入方法依賴的package包/類
/**
* Returns the name of the package that the given type is in. If the type is in the default
* (unnamed) package then the name is the empty string.
*/
static String packageNameOf(Element type) {
while (true) {
Element enclosing = type.getEnclosingElement();
if (enclosing == null) {
throw new RuntimeException("null value from type.getEnclosingElement()");
}
if (enclosing instanceof PackageElement) {
return ((PackageElement) enclosing).getQualifiedName().toString();
}
type = enclosing;
}
}
示例12: parseResourceString
import javax.lang.model.element.Element; //導入方法依賴的package包/類
private void parseResourceString(Element element,
Map<TypeElement, BindingSet.Builder> builderMap, Set<TypeElement> erasedTargetNames) {
boolean hasError = false;
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
// Verify that the target type is String.
if (!STRING_TYPE.equals(element.asType().toString())) {
error(element, "@%s field type must be 'String'. (%s.%s)",
BindString.class.getSimpleName(), enclosingElement.getQualifiedName(),
element.getSimpleName());
hasError = true;
}
// Verify common generated code restrictions.
hasError |= isInaccessibleViaGeneratedCode(BindString.class, "fields", element);
hasError |= isBindingInWrongPackage(BindString.class, element);
if (hasError) {
return;
}
// Assemble information on the field.
String name = element.getSimpleName().toString();
int id = element.getAnnotation(BindString.class).value();
QualifiedId qualifiedId = elementToQualifiedId(element, id);
BindingSet.Builder builder = getOrCreateBindingBuilder(builderMap, enclosingElement);
builder.addResource(
new FieldResourceBinding(getId(qualifiedId), name, FieldResourceBinding.Type.STRING));
erasedTargetNames.add(enclosingElement);
}
示例13: parseResourceArray
import javax.lang.model.element.Element; //導入方法依賴的package包/類
private void parseResourceArray(Element element,
Map<TypeElement, BindingSet.Builder> builderMap, Set<TypeElement> erasedTargetNames) {
boolean hasError = false;
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
// Verify that the target type is supported.
FieldResourceBinding.Type type = getArrayResourceMethodName(element);
if (type == null) {
error(element,
"@%s field type must be one of: String[], int[], CharSequence[], %s. (%s.%s)",
BindArray.class.getSimpleName(), TYPED_ARRAY_TYPE, enclosingElement.getQualifiedName(),
element.getSimpleName());
hasError = true;
}
// Verify common generated code restrictions.
hasError |= isInaccessibleViaGeneratedCode(BindArray.class, "fields", element);
hasError |= isBindingInWrongPackage(BindArray.class, element);
if (hasError) {
return;
}
// Assemble information on the field.
String name = element.getSimpleName().toString();
int id = element.getAnnotation(BindArray.class).value();
QualifiedId qualifiedId = elementToQualifiedId(element, id);
BindingSet.Builder builder = getOrCreateBindingBuilder(builderMap, enclosingElement);
builder.addResource(new FieldResourceBinding(getId(qualifiedId), name, type));
erasedTargetNames.add(enclosingElement);
}
示例14: createPluginFactory
import javax.lang.model.element.Element; //導入方法依賴的package包/類
private static void createPluginFactory(ProcessingEnvironment env, Element topLevelClass, List<GeneratedPlugin> plugins) {
PackageElement pkg = (PackageElement) topLevelClass.getEnclosingElement();
String genClassName = "PluginFactory_" + topLevelClass.getSimpleName();
try {
JavaFileObject factory = env.getFiler().createSourceFile(pkg.getQualifiedName() + "." + genClassName, topLevelClass);
try (PrintWriter out = new PrintWriter(factory.openWriter())) {
out.printf("// CheckStyle: stop header check\n");
out.printf("// CheckStyle: stop line length check\n");
out.printf("// GENERATED CONTENT - DO NOT EDIT\n");
out.printf("// GENERATORS: %s, %s\n", VerifierAnnotationProcessor.class.getName(), PluginGenerator.class.getName());
out.printf("package %s;\n", pkg.getQualifiedName());
out.printf("\n");
createImports(out, plugins);
out.printf("\n");
out.printf("@ServiceProvider(NodeIntrinsicPluginFactory.class)\n");
out.printf("public class %s implements NodeIntrinsicPluginFactory {\n", genClassName);
for (GeneratedPlugin plugin : plugins) {
out.printf("\n");
plugin.generate(env, out);
}
out.printf("\n");
createPluginFactoryMethod(out, plugins);
out.printf("}\n");
}
} catch (IOException e) {
env.getMessager().printMessage(Diagnostic.Kind.ERROR, e.getMessage());
}
}
示例15: parseNewThreadAnnotation
import javax.lang.model.element.Element; //導入方法依賴的package包/類
private void parseNewThreadAnnotation(Element element, Map<TypeElement, Builder> builderMap) {
if (!(element instanceof ExecutableElement) || element.getKind() != METHOD) {
throw new IllegalStateException(
String.format("@%s annotation must be on a method.",
CallOnNewThread.class.getSimpleName()));
}
ExecutableElement executableElement = (ExecutableElement) element;
if (!executableElement.getModifiers().contains(Modifier.PUBLIC)) {
throw new IllegalStateException(
String.format("@%s annotation must be on a public method.",
CallOnNewThread.class.getSimpleName()));
}
TypeElement typeElement = (TypeElement) element.getEnclosingElement();
String name = executableElement.getSimpleName().toString();
List<? extends VariableElement> methodParameters = executableElement.getParameters();
TypeMirror returnTypeMirror = executableElement.getReturnType();
MethodParameter[] parameters = MethodParameter.NONE;
if (!methodParameters.isEmpty()) {
parameters = new MethodParameter[methodParameters.size()];
for (int i = 0; i < methodParameters.size(); i++) {
VariableElement variableElement = methodParameters.get(i);
TypeMirror methodParameterType = variableElement.asType();
if (methodParameterType instanceof TypeVariable) {
TypeVariable typeVariable = (TypeVariable) methodParameterType;
methodParameterType = typeVariable.getUpperBound();
}
parameters[i] = new MethodParameter(i, TypeName.get(methodParameterType),
variableElement.getSimpleName().toString());
}
}
MethodThreadStitching methodThreadStitching = new MethodThreadStitching(
name,
TypeName.get(returnTypeMirror),
Arrays.asList(parameters),
Constants.TYPE_NEW_THREAD);
StitchGenerator.Builder builder = getOrCreateStitchingBuilder(builderMap, typeElement);
builder.addMethod(methodThreadStitching);
}