本文整理汇总了Java中javax.lang.model.element.Element.getModifiers方法的典型用法代码示例。如果您正苦于以下问题:Java Element.getModifiers方法的具体用法?Java Element.getModifiers怎么用?Java Element.getModifiers使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.lang.model.element.Element
的用法示例。
在下文中一共展示了Element.getModifiers方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkAnnotation
import javax.lang.model.element.Element; //导入方法依赖的package包/类
@SuppressWarnings("StringConcatenationInsideStringBufferAppend")
@Override
public void checkAnnotation(Element annotatedField) {
checkEnvironment(annotatedField);
if (annotatedField.getKind() != mValidKind) {
mErrorBuilder.append("Field " + annotatedField + " of " + annotatedField.getEnclosingElement().getSimpleName() + " should be " + mValidKind.name() + ", or not mark it as @" + InjectPresenter.class.getSimpleName()).append("\n");
}
for (Modifier modifier : annotatedField.getModifiers()) {
if (!mValidModifiers.contains(modifier)) {
mErrorBuilder.append("Field " + annotatedField + " of " + annotatedField.getEnclosingElement().getSimpleName() + " can't be a " + modifier).append(". Use ").append(validModifiersToString()).append("\n");
}
}
Element enclosingElement = annotatedField.getEnclosingElement();
while (enclosingElement.getKind() == ElementKind.CLASS) {
if (!enclosingElement.getModifiers().contains(Modifier.PUBLIC)) {
mErrorBuilder.append(enclosingElement.getSimpleName() + " should be PUBLIC ");
break;
}
enclosingElement = enclosingElement.getEnclosingElement();
}
}
示例2: addAllNonPrivateAccessors
import javax.lang.model.element.Element; //导入方法依赖的package包/类
private void addAllNonPrivateAccessors(Element element, Elements elements, Types types, JsonObjectHolder objectHolder) {
List<? extends Element> enclosedElements = element.getEnclosedElements();
for (Element enclosedElement : enclosedElements) {
ElementKind enclosedElementKind = enclosedElement.getKind();
if (enclosedElementKind == ElementKind.FIELD) {
Set<Modifier> modifiers = enclosedElement.getModifiers();
if (modifiers.contains(Modifier.PRIVATE) && !modifiers.contains(Modifier.TRANSIENT) && !modifiers.contains(Modifier.STATIC)) {
String getter = JsonFieldHolder.getGetter(enclosedElement, elements);
String setter = JsonFieldHolder.getSetter(enclosedElement, elements);
if (!TextUtils.isEmpty(getter) && !TextUtils.isEmpty(setter)) {
createOrUpdateFieldHolder(enclosedElement, elements, types, objectHolder);
}
}
}
}
}
示例3: isDeclaredAsJavaBean
import javax.lang.model.element.Element; //导入方法依赖的package包/类
public static String isDeclaredAsJavaBean(TypeElement clazz) {
if (ElementKind.CLASS != clazz.getKind()) {
return PaletteUtils.getBundleString("MSG_notAClass"); // NOI18N
}
Set<javax.lang.model.element.Modifier> mods = clazz.getModifiers();
if (mods.contains(javax.lang.model.element.Modifier.ABSTRACT)) {
return PaletteUtils.getBundleString("MSG_abstractClass"); // NOI18N
}
if (!mods.contains(javax.lang.model.element.Modifier.PUBLIC)) {
return PaletteUtils.getBundleString("MSG_notPublic"); // NOI18N
}
for (Element member : clazz.getEnclosedElements()) {
mods = member.getModifiers();
if (ElementKind.CONSTRUCTOR == member.getKind() &&
mods.contains(javax.lang.model.element.Modifier.PUBLIC) &&
((ExecutableElement) member).getParameters().isEmpty()) {
return null;
}
}
return PaletteUtils.getBundleString("MSG_noPublicConstructor"); // NOI18N
}
示例4: process
import javax.lang.model.element.Element; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Set<? extends Element> root = roundEnv.getElementsAnnotatedWith(APILevel.class);
System.out.println("Processing ..");
for (Element element : root) {
for (Modifier modifier : element.getModifiers()) {
if (modifier.equals(Modifier.ABSTRACT)
|| modifier.equals(Modifier.PRIVATE)
|| modifier.equals(Modifier.PUBLIC)
|| modifier.equals(Modifier.PROTECTED)) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, ERROR_MESSAGE, element);
return false;
}
}
}
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, ERROR_MESSAGE, null);
return true;
}
示例5: addModifiers
import javax.lang.model.element.Element; //导入方法依赖的package包/类
/**
* Add the modifier for the member. The modifiers are ordered as specified
* by <em>The Java Language Specification</em>.
*
* @param member the member for which teh modifier will be added.
* @param htmltree the content tree to which the modifier information will be added.
*/
protected void addModifiers(Element member, Content htmltree) {
Set<Modifier> set = new TreeSet<>(member.getModifiers());
// remove the ones we really don't need
set.remove(NATIVE);
set.remove(SYNCHRONIZED);
set.remove(STRICTFP);
// According to JLS, we should not be showing public modifier for
// interface methods.
if ((utils.isField(member) || utils.isMethod(member))
&& ((writer instanceof ClassWriterImpl
&& utils.isInterface(((ClassWriterImpl) writer).getTypeElement()) ||
writer instanceof AnnotationTypeWriterImpl) )) {
// Remove the implicit abstract and public modifiers
if (utils.isMethod(member) &&
(utils.isInterface(member.getEnclosingElement()) ||
utils.isAnnotationType(member.getEnclosingElement()))) {
set.remove(ABSTRACT);
set.remove(PUBLIC);
}
if (!utils.isMethod(member)) {
set.remove(PUBLIC);
}
}
if (!set.isEmpty()) {
String mods = set.stream().map(Modifier::toString).collect(Collectors.joining(" "));
htmltree.addContent(mods);
htmltree.addContent(Contents.SPACE);
}
}
示例6: checkFieldModifier
import javax.lang.model.element.Element; //导入方法依赖的package包/类
private void checkFieldModifier(Element element) {
Set<Modifier> modifiers = element.getModifiers();
if (modifiers.contains(PRIVATE) || modifiers.contains(STATIC)) {
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
ProcessorUtils.printError(messager, element, "@%s %s must not be private or static. (%s.%s)",
Autowired.class.getSimpleName(), "fields", enclosingElement.getQualifiedName(),
element.getSimpleName());
}
}
示例7: addAllNonPrivateFields
import javax.lang.model.element.Element; //导入方法依赖的package包/类
private void addAllNonPrivateFields(Element element, Elements elements, Types types, JsonObjectHolder objectHolder) {
List<? extends Element> enclosedElements = element.getEnclosedElements();
for (Element enclosedElement : enclosedElements) {
ElementKind enclosedElementKind = enclosedElement.getKind();
if (enclosedElementKind == ElementKind.FIELD) {
Set<Modifier> modifiers = enclosedElement.getModifiers();
if (!modifiers.contains(Modifier.PRIVATE) && !modifiers.contains(Modifier.PROTECTED) && !modifiers.contains(Modifier.TRANSIENT) && !modifiers.contains(Modifier.STATIC)) {
createOrUpdateFieldHolder(enclosedElement, elements, types, objectHolder);
}
}
}
}
示例8: getExtendedModifiers
import javax.lang.model.element.Element; //导入方法依赖的package包/类
private static String getExtendedModifiers(Elements elements, Element el) {
StringBuilder sb = new StringBuilder();
for (Modifier m : el.getModifiers())
sb.append(m.name());
if (elements.isDeprecated(el))
sb.append(DEPRECATED);
if (el.getKind() == ElementKind.FIELD) {
Object v = ((VariableElement) el).getConstantValue();
if (v != null) {
sb.append(v.getClass().getName());
sb.append(String.valueOf(v));
}
}
return sb.toString();
}
示例9: hasStaticImportSimpleNameClash
import javax.lang.model.element.Element; //导入方法依赖的package包/类
/**
* @param info
* @param simpleName of static method.
* @return true if a static import exists with the same simple name.
* Caveat, expect false positives on protected and default visibility methods from wildcard static imports.
*/
private static boolean hasStaticImportSimpleNameClash(CompilationInfo info, String simpleName) {
for (ImportTree i : info.getCompilationUnit().getImports()) {
if (!i.isStatic()) {
continue;
}
String q = i.getQualifiedIdentifier().toString();
if (q.endsWith(".*")) { //NOI18N
TypeElement ie = info.getElements().getTypeElement(q.substring(0, q.length() - 2));
if (ie == null) {
continue;
}
for (Element enclosed : ie.getEnclosedElements()) {
Set<Modifier> modifiers = enclosed.getModifiers();
if (enclosed.getKind() != ElementKind.METHOD || !modifiers.contains(Modifier.STATIC) || modifiers.contains(Modifier.PRIVATE)) {
continue;
}
String sn1 = enclosed.getSimpleName().toString();
if (simpleName.equals(sn1)) {
return true;
}
}
} else {
int endIndex = q.lastIndexOf("."); //NOI18N
if (endIndex == -1 || endIndex >= q.length() - 1) {
continue;
}
if (q.substring(endIndex).equals(simpleName)) {
return true;
}
}
}
return false;
}
示例10: checkModifier
import javax.lang.model.element.Element; //导入方法依赖的package包/类
/**
* Returns true if access is allowed.
*
* @param e the element in question
* @return whether the modifiers pass this filter
*/
public boolean checkModifier(Element e) {
Set<Modifier> modifiers = e.getModifiers();
AccessKind fflag = AccessKind.PACKAGE;
if (modifiers.contains(Modifier.PUBLIC)) {
fflag = AccessKind.PUBLIC;
} else if (modifiers.contains(Modifier.PROTECTED)) {
fflag = AccessKind.PROTECTED;
} else if (modifiers.contains(Modifier.PRIVATE)) {
fflag = AccessKind.PRIVATE;
}
EnumSet<AccessKind> filterSet = filterMap.get(getAllowedKind(e.getKind()));
return filterSet.contains(fflag);
}
示例11: ElementDescription
import javax.lang.model.element.Element; //导入方法依赖的package包/类
public ElementDescription(CompilationInfo info, Element element, boolean overriddenFlag) {
this.originalCPInfo = info.getClasspathInfo();
this.handle = ElementHandle.create(element);
if (METHOD.equals(element.getKind()) && null != element.getEnclosingElement()) {
//when showing the implementors/overriders of a method, show the type icon (not the method icon)
this.imageKind = element.getEnclosingElement().getKind();
} else {
this.imageKind = this.handle.getKind();
}
this.outtermostElement = ElementHandle.create(SourceUtils.getOutermostEnclosingTypeElement(element));
this.modifiers = element.getModifiers();
this.displayName = overriddenFlag ? computeDisplayNameIsOverridden(element) : computeDisplayNameOverrides(element);
this.overriddenFlag = overriddenFlag;
}
示例12: create
import javax.lang.model.element.Element; //导入方法依赖的package包/类
public static Description create(CompilationInfo info, Element element, List<Description> subs, boolean isSelectable, boolean isSelected ) {
boolean deprecated = info.getElements().isDeprecated(element);
String htmlHeader = null;
switch (element.getKind()) {
case ANNOTATION_TYPE:
case CLASS:
case ENUM:
case INTERFACE:
htmlHeader = createHtmlHeader(deprecated, (TypeElement)element);
break;
case ENUM_CONSTANT:
case FIELD:
htmlHeader = createHtmlHeader(deprecated, (VariableElement)element);
break;
case CONSTRUCTOR:
case METHOD:
htmlHeader = createHtmlHeader(deprecated, (ExecutableElement)element);
break;
}
return new Description(element.getSimpleName().toString(),
ElementHandle.create(element),
element.getModifiers(),
subs,
htmlHeader,
isSelectable,
isSelected);
}
示例13: getParamModels
import javax.lang.model.element.Element; //导入方法依赖的package包/类
private List<ActivityIntentModel.ParamModel> getParamModels(TypeElement typeElement) {
List<ActivityIntentModel.ParamModel> paramModels = new ArrayList<>();
List<? extends Element> list = typeElement.getEnclosedElements();
if (list == null) {
return paramModels;
}
for (Element paramElement : list) {
IntentParam intentParam = paramElement.getAnnotation(IntentParam.class); //AnnotationMirror mirror = e.getAnnotationMirrors().get(0);
if (intentParam != null) {
if (paramElement.getKind() != ElementKind.FIELD || paramElement.getModifiers() == null || !paramElement.getModifiers().contains(Modifier.STATIC)) {
messager.printMessage(Diagnostic.Kind.ERROR, "IntentParam can only be applied to STATIC FIELD");
continue;
}
messager.printMessage(Diagnostic.Kind.NOTE, " IntentParam annotated element:" + paramElement.getSimpleName());
ActivityIntentModel.ParamModel paramModel = new ActivityIntentModel.ParamModel();
paramModel.fieldName = paramElement.getSimpleName().toString();
//TypeMirror fieldType = e.asType();
//paramModel.type = fieldType;
paramModel.type = intentParam.type();
paramModel.generatedPropName = intentParam.name();
paramModel.qualifiedClassName = typeElement.getQualifiedName().toString();
paramModels.add(paramModel);
}
}
return paramModels;
}
示例14: isValid
import javax.lang.model.element.Element; //导入方法依赖的package包/类
/**
* 注解是否有效
* @author leibing
* @createTime 2017/3/2
* @lastModify 2017/3/2
* @param annotationClass
* @param targetThing
* @param element 元素
* @return
*/
private boolean isValid(Class<? extends Annotation> annotationClass, String targetThing, Element element) {
boolean isVaild = true;
// 获取变量的所在的父元素,肯能是类、接口、枚举
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
// 父元素的全限定名
String qualifiedName = enclosingElement.getQualifiedName().toString();
// 所在的类不能是private或static修饰
Set<Modifier> modifiers = element.getModifiers();
if (modifiers.contains(PRIVATE) || modifiers.contains(STATIC)) {
error(element, "@%s %s must not be private or static. (%s.%s)",
annotationClass.getSimpleName(), targetThing, enclosingElement.getQualifiedName(),
element.getSimpleName());
isVaild = false;
}
// 父元素必须是类,而不能是接口或枚举
if (enclosingElement.getKind() != ElementKind.CLASS) {
error(enclosingElement, "@%s %s may only be contained in classes. (%s.%s)",
annotationClass.getSimpleName(), targetThing, enclosingElement.getQualifiedName(),
element.getSimpleName());
isVaild = false;
}
// 不能在Android框架层注解
if (qualifiedName.startsWith("android.")) {
error(element, "@%s-annotated class incorrectly in Android framework package. (%s)",
annotationClass.getSimpleName(), qualifiedName);
return false;
}
// 不能在java框架层注解
if (qualifiedName.startsWith("java.")) {
error(element, "@%s-annotated class incorrectly in Java framework package. (%s)",
annotationClass.getSimpleName(), qualifiedName);
return false;
}
return isVaild;
}
示例15: process
import javax.lang.model.element.Element; //导入方法依赖的package包/类
@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment environment) {
for(final Element element : environment.getElementsAnnotatedWith(PutMany.class)) {
final PutMany annotation = element.getAnnotation(PutMany.class);
if(ElementKind.METHOD != element.getKind()) {
throw new DataSinkDefinitionException(
"Must use @PutMany with methods only! Tried to use with " + element.getSimpleName()
+ ", which is not a method.");
}
final Set<Modifier> modifiers = element.getModifiers();
if(!modifiers.contains(Modifier.PUBLIC)) {
throw new DataSinkDefinitionException(
"Must use @PutMany with public methods only! Tried to use with " + element.getSimpleName()
+ ", which is not public.");
}
if(modifiers.contains(Modifier.STATIC)) {
throw new DataSinkDefinitionException(
"Must use @PutMany with non-static methods only! Tried to use with " + element.getSimpleName()
+ ", which is static.");
}
final ExecutableType method = (ExecutableType)element.asType();
final Types types = processingEnv.getTypeUtils();
final Elements elements = processingEnv.getElementUtils();
// annotation.value() will throw the exception because it doesn't have compiled class information yet, and is of type Class<?>.
TypeMirror annotatedType = null;
try {
annotation.value();
} catch(final MirroredTypeException e) {
annotatedType = e.getTypeMirror();
}
if(TypeKind.VOID != method.getReturnType().getKind()) {
throw new DataSinkDefinitionException("@PutMany method must have void return! Tried to use with " + element.getSimpleName() + ", which returns "
+ method.getReturnType() + ".");
}
if(method.getParameterTypes().size() != 2) {
throw new DataSinkDefinitionException("@PutMany methods must take 2 arguments: Iterable<T> items, PipelineContext context. Tried to use with "
+ element.getSimpleName() + ", which has a different signature.");
}
final TypeElement iterableType = elements.getTypeElement(Iterable.class.getName());
final TypeMirror[] genericType = new TypeMirror[] {annotatedType};
final DeclaredType itemsType = types.getDeclaredType(iterableType, genericType);
final TypeMirror contextType = elements.getTypeElement(PipelineContext.class.getName()).asType();
if(!types.isAssignable(itemsType, method.getParameterTypes().get(0))) {
throw new DataSinkDefinitionException(
"@PutMany method annotated value must be assignable from the generic type of the method's first argument . Tried to use with "
+ element.getSimpleName() + ", which takes " + method.getParameterTypes().get(0) + ".");
}
if(!types.isAssignable(method.getParameterTypes().get(1), contextType)) {
throw new DataSinkDefinitionException("@PutMany method second argument must be assignable from " + contextType + ". Tried to use with "
+ element.getSimpleName() + ", which takes " + method.getParameterTypes().get(1) + ".");
}
}
return true;
}