本文整理汇总了Java中com.google.auto.common.MoreElements类的典型用法代码示例。如果您正苦于以下问题:Java MoreElements类的具体用法?Java MoreElements怎么用?Java MoreElements使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MoreElements类属于com.google.auto.common包,在下文中一共展示了MoreElements类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getElementOwnerClassName
import com.google.auto.common.MoreElements; //导入依赖的package包/类
/**
* Get class name of special element
*/
protected String getElementOwnerClassName(Element element) {
String clazzName;
// todo: Get the Class which element's owner.
ElementKind elementKind = element.getKind();
if (ElementKind.FIELD == elementKind) {
clazzName = MoreElements.asVariable(element).getEnclosingElement().toString();
logger("[getDIClazzSafe]MoreElements.asVariable().getEnclosingElement(): " + MoreElements.asVariable(element).getEnclosingElement());
} else if (ElementKind.METHOD == elementKind) {
clazzName = MoreElements.asExecutable(element).getEnclosingElement().toString();
} else {
clazzName = typeUtils.erasure(element.asType()).toString();
}
return clazzName;
}
示例2: deepSame
import com.google.auto.common.MoreElements; //导入依赖的package包/类
/**
* 两组参数类型相同
*/
public static boolean deepSame(List<? extends VariableElement> _this, List<? extends VariableElement> _that) {
if (null == _this && null == _that) {
return true;
}
if (null == _this || null == _that) {
return false;
}
if (_this.size() != _that.size()) {
return false;
}
for (int i = 0, len = _this.size(); i < len; i++) {
VariableElement _thisEle = _this.get(i);
VariableElement _thatEle = _that.get(i);
if (!MoreElements.asType(_thisEle).getQualifiedName().toString()
.equals(MoreElements.asType(_thatEle).getQualifiedName().toString())) {
return false;
}
}
return true;
}
示例3: addQualifyExtension
import com.google.auto.common.MoreElements; //导入依赖的package包/类
public void addQualifyExtension(Metaqualifier context, Element annotatedElement,
AnnotationMirror annotationMirror, Qualify.Entry qualifyExtension) {
final Metaextension<?> metaextension = context.use(qualifyExtension);
final TypeMirror type = metaextension.type().get();
final TypeElement element = MoreElements.asType(types().asElement(type));
// Do not requires validation
if (type.toString().equals("java.lang.String")) return;
if (type.toString().equals("java.lang.Class")) return;
// Check extensions types has a valid valueOf(String) static method
final boolean valid = getExecutableElements(element).stream().anyMatch(method ->
method.getSimpleName().toString().equals("valueOf")
&& method.getModifiers().contains(Modifier.STATIC)
&& method.getParameters().size() == 1
&& isFirstParameterStringType(method));
if (!valid) {
getProcessingEnv().getMessager().printMessage(Diagnostic.Kind.ERROR, String.format(
"Qualifier extension type '%s' with key '%s' must have a 'valueOf(String)' static " +
"method", metaextension.type(), metaextension.extension()),
annotatedElement, annotationMirror);
}
}
示例4: process
import com.google.auto.common.MoreElements; //导入依赖的package包/类
@Override
public Set<Element> process(SetMultimap<Class<? extends Annotation>, Element> elementsByAnnotation) {
ImmutableSet.Builder<Element> elementsForLaterProcessing = ImmutableSet.builder();
for (Map.Entry<Class<? extends Annotation>, Element> entry : elementsByAnnotation.entries()) {
if (!entry.getValue().getKind().isInterface()) {
getProcessingEnv().getMessager().printMessage(Diagnostic.Kind.ERROR, entry.getKey().getName()
+ " annotation can be used only with interfaces", entry.getValue());
}
Element value = entry.getValue();
if (validate(value)) {
generate(MoreElements.asType(value));
} else {
elementsForLaterProcessing.add(value);
}
}
return elementsForLaterProcessing.build();
}
示例5: validateSingleUid
import com.google.auto.common.MoreElements; //导入依赖的package包/类
static void validateSingleUid(Element element, boolean uidFound,
String uidElemenetName, ProcessingEnvironment processingEnvironment) {
TypeElement typeElement = MoreElements.asType(element);
PackageElement packageElement = MoreElements.getPackage(element);
for (Element enclosed : packageElement.getEnclosedElements()) {
TypeElement enclosedType = MoreElements.asType(enclosed);
if (uidFound && Utils.hasUIDAnnotation(enclosedType)
&& !enclosedType.getQualifiedName().toString().equals(typeElement.getQualifiedName().toString())) {
processingEnvironment.getMessager().printMessage(Diagnostic.Kind.ERROR.ERROR,
"Cannot have two @UID annotated elements in one package: " +
uidElemenetName + " and " +
enclosedType.getQualifiedName().toString() + ".");
}
uidFound = uidFound || Utils.hasUIDAnnotation(enclosedType);
if (uidFound && uidElemenetName == null) {
uidElemenetName = enclosedType.getQualifiedName().toString();
}
}
TypeElement type = Utils.getTypeFromPackage(packageElement);
List<TypeElement> typeElements = Utils.getAnnotationMirrorValueArrayAsTypeElementList(Utils.getTypeAnnotation(type));
for (TypeElement elt : typeElements) {
if (Utils.hasTypeAnnotation(elt)) {
validateSingleUid(elt, uidFound, uidElemenetName, processingEnvironment);
}
}
}
示例6: validateThatCurrentPropertyIsReachableFromTheBaseType
import com.google.auto.common.MoreElements; //导入依赖的package包/类
private void validateThatCurrentPropertyIsReachableFromTheBaseType(TypeElement restElement, TypeElement currentProperty, TypeElement baseType,
Map<String, TypeTree> typeTrees,
ProcessingEnvironment processingEnvironment) {
List<TypeElement> superAndSub = Utils.getSuperAndSubTypes(typeTrees.get(baseType.getQualifiedName().toString()));
for (TypeElement typeElement : superAndSub) {
PackageElement packageElement = MoreElements.getPackage(typeElement);
for (Element element : packageElement.getEnclosedElements()) {
TypeElement asType = MoreElements.asType(element);
if (asType.getQualifiedName().toString().equals(currentProperty.getQualifiedName().toString())) {
return;
}
}
}
processingEnvironment.getMessager().printMessage(Diagnostic.Kind.ERROR, "Properties must be reachable from their base type. " +
"In the API endpoint "+restElement+ " for the method "+mOp+", the property "
+currentProperty.getQualifiedName().toString()+" is not reachable from the (possibly inferred) base type "+
baseType.getQualifiedName().toString()+". If you're pretty sure that the property you're using makes sense in "+
"the API call, then verify that $.class separators are correctly placed. Remember: "+
"$.class should be used to end an inner object in an API definition. If multiple objects end at the same place," +
" all levels must be closed with a $.class. Think of $.class like closing curly brackets.");
}
示例7: validateActionCreator
import com.google.auto.common.MoreElements; //导入依赖的package包/类
private static void validateActionCreator(ExecutableElement element,
String actionName,
TypeMirror actionCreator,
ArrayList<VariableElement> args,
Map<String, ActionCreatorElement> knownActionCreators,
Env env) throws ValidationException {
Element actionCreatorElement = MoreTypes.asElement(actionCreator);
if (!MoreElements.isAnnotationPresent(actionCreatorElement, ActionCreator.class)) {
throw new ValidationException(element, "Action creator %s should be annotated with @%s", actionCreator, ActionCreator.class.getSimpleName());
}
ActionCreatorElement creatorElement = knownActionCreators.get(env.getElements().getBinaryName((TypeElement) actionCreatorElement).toString());
if (creatorElement == null) {
throw new ElementNotReadyException();
}
if (!creatorElement.hasAction(actionName, args)) {
throw new ValidationException(element, "Cannot find action creator for action \"%s\" and args %s in interface %s", actionName, toString(args), creatorElement.getName(env));
}
}
示例8: parseConstructors
import com.google.auto.common.MoreElements; //导入依赖的package包/类
private static List<AutoReducerConstructor> parseConstructors(TypeElement typeElement) throws ValidationException {
List<AutoReducerConstructor> constructors = new ArrayList<>();
boolean hasNonDefaultConstructors = false;
for (Element element : typeElement.getEnclosedElements()) {
if (element.getKind() == ElementKind.CONSTRUCTOR) {
ExecutableElement executableElement = MoreElements.asExecutable(element);
if (executableElement.getModifiers().contains(Modifier.PRIVATE)) continue;
List<? extends VariableElement> parameters = executableElement.getParameters();
hasNonDefaultConstructors |= parameters.size() != 0;
constructors.add(new AutoReducerConstructor(executableElement));
}
}
//if we only have one default constructor, it can be omitted
if (!hasNonDefaultConstructors && constructors.size() == 1) return Collections.emptyList();
//this is the case when all constructors are private
if (constructors.size() == 0)
throw new ValidationException(typeElement, "No accessible constructors available for class %s", typeElement);
return constructors;
}
示例9: addAbstractMethods
import com.google.auto.common.MoreElements; //导入依赖的package包/类
private void addAbstractMethods(
TypeMirror typeMirror, List<ExecutableElement> abstractMethods) {
if (typeMirror.getKind() != TypeKind.DECLARED) {
return;
}
TypeElement typeElement = MoreElements.asType(typeMirror.accept(AS_ELEMENT_VISITOR, null));
addAbstractMethods(typeElement.getSuperclass(), abstractMethods);
for (TypeMirror interfaceMirror : typeElement.getInterfaces()) {
addAbstractMethods(interfaceMirror, abstractMethods);
}
for (ExecutableElement method : ElementFilter.methodsIn(typeElement.getEnclosedElements())) {
for (Iterator<ExecutableElement> it = abstractMethods.iterator(); it.hasNext(); ) {
ExecutableElement maybeOverridden = it.next();
if (elementUtils.overrides(method, maybeOverridden, typeElement)) {
it.remove();
}
}
if (method.getModifiers().contains(Modifier.ABSTRACT)) {
abstractMethods.add(method);
}
}
}
示例10: buildClassName
import com.google.auto.common.MoreElements; //导入依赖的package包/类
private ClassName buildClassName(Element element) {
String name = element.getSimpleName().toString();
// try to remove Presenter at the end of the name
String newName = removeEndingName(name, "Presenter");
if (newName == null) {
errors.addInvalid("Class name " + newName);
}
String pkg = MoreElements.getPackage(element).getQualifiedName().toString();
if (StringUtils.isBlank(pkg)) {
errors.addInvalid("Package name " + pkg);
}
pkg = pkg + ".stackable";
return ClassName.get(pkg, newName + "Stackable");
}
示例11: findScope
import com.google.auto.common.MoreElements; //导入依赖的package包/类
/**
* Find annotation that is itself annoted with @Scope
* If there is one, it will be later applied on the generated component
* Otherwise the component will be unscoped
* Throw error if more than one scope annotation found
*/
private AnnotationMirror findScope() {
AnnotationMirror annotationTypeMirror = null;
for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
Element annotationElement = annotationMirror.getAnnotationType().asElement();
if (MoreElements.isAnnotationPresent(annotationElement, Scope.class)) {
// already found one scope
if (annotationTypeMirror != null) {
errors.addInvalid("Several dagger scopes on same element are not allowed");
continue;
}
annotationTypeMirror = annotationMirror;
}
}
return annotationTypeMirror;
}
示例12: buildClassName
import com.google.auto.common.MoreElements; //导入依赖的package包/类
private ClassName buildClassName() {
String name = extractor.getElement().getSimpleName().toString();
// try to remove NavigationScope at the end of the name
String newName = removeEndingName(removeEndingName(name, "Scope"), "Navigation");
if (newName == null) {
errors.addInvalid("Class name " + newName);
}
String pkg = MoreElements.getPackage(extractor.getElement()).getQualifiedName().toString();
if (StringUtils.isBlank(pkg)) {
errors.addInvalid("Package name " + pkg);
}
pkg = pkg + ".path";
return ClassName.get(pkg, newName + "Path");
}
示例13: extract
import com.google.auto.common.MoreElements; //导入依赖的package包/类
@Override
public void extract() {
if (element.getKind() != ElementKind.CLASS) {
errors.addInvalid("Annotation can be applied only on a class");
return;
}
viewTypeMirror = ExtractorUtils.getValueFromAnnotation(element, AutoPath.class, ANNOTATION_VIEW);
if (viewTypeMirror == null) {
errors.addMissing("withView() value");
}
constructorElements = new ArrayList<>();
for (Element e : element.getEnclosedElements()) {
if (e.getKind() == ElementKind.CONSTRUCTOR) {
constructorElements.add(MoreElements.asExecutable(e));
}
}
}
示例14: buildClassName
import com.google.auto.common.MoreElements; //导入依赖的package包/类
private ClassName buildClassName(Element element) {
String name = element.getSimpleName().toString();
// try to remove Presenter at the end of the name
String newName = removeEndingName(name, "Presenter");
if (newName == null) {
errors.addInvalid("Class name " + newName);
}
String pkg = MoreElements.getPackage(element).getQualifiedName().toString();
if (StringUtils.isBlank(pkg)) {
errors.addInvalid("Package name " + pkg);
}
pkg = pkg + ".screen";
return ClassName.get(pkg, newName + "Screen");
}
示例15: findWriteToParcel
import com.google.auto.common.MoreElements; //导入依赖的package包/类
@Nullable private ExecutableElement findWriteToParcel(TypeElement subject) {
TypeMirror parcel = elements.getTypeElement("android.os.Parcel").asType();
@SuppressWarnings("deprecation") // Support for kapt2
ImmutableSet<ExecutableElement> methods =
MoreElements.getLocalAndInheritedMethods(subject, elements);
for (ExecutableElement element : methods) {
if (element.getSimpleName().contentEquals("writeToParcel")
&& MoreTypes.isTypeOf(void.class, element.getReturnType())
&& !element.getModifiers().contains(Modifier.ABSTRACT)) {
List<? extends VariableElement> parameters = element.getParameters();
if (parameters.size() == 2
&& types.isSameType(parcel, parameters.get(0).asType())
&& MoreTypes.isTypeOf(int.class, parameters.get(1).asType())) {
return element;
}
}
}
return null;
}