use of io.micronaut.inject.ast.ClassElement in project micronaut-core by micronaut-projects.
the class JavaVisitorContext method getClassElements.
@Override
@NonNull
public ClassElement[] getClassElements(@NonNull String aPackage, @NonNull String... stereotypes) {
ArgumentUtils.requireNonNull("aPackage", aPackage);
ArgumentUtils.requireNonNull("stereotypes", stereotypes);
final PackageElement packageElement = elements.getPackageElement(aPackage);
if (packageElement != null) {
List<ClassElement> classElements = new ArrayList<>();
populateClassElements(stereotypes, packageElement, classElements);
return classElements.toArray(new ClassElement[0]);
}
return new ClassElement[0];
}
use of io.micronaut.inject.ast.ClassElement in project micronaut-core by micronaut-projects.
the class ModelUtils method findNonPrivateStaticCreators.
private List<ExecutableElement> findNonPrivateStaticCreators(TypeElement classElement, AnnotationUtils annotationUtils) {
List<? extends Element> enclosedElements = classElement.getEnclosedElements();
List<ExecutableElement> staticCreators = ElementFilter.methodsIn(enclosedElements).stream().filter(method -> method.getModifiers().contains(STATIC)).filter(method -> !method.getModifiers().contains(PRIVATE)).filter(method -> typeUtils.isAssignable(typeUtils.erasure(method.getReturnType()), classElement.asType())).filter(method -> {
final AnnotationMetadata annotationMetadata = annotationUtils.getAnnotationMetadata(method);
return annotationMetadata.hasStereotype(Creator.class);
}).collect(Collectors.toList());
if (staticCreators.isEmpty()) {
TypeElement companionClass = ElementFilter.typesIn(enclosedElements).stream().filter(type -> type.getSimpleName().toString().equals("Companion")).filter(type -> type.getModifiers().contains(STATIC)).findFirst().orElse(null);
if (companionClass != null) {
staticCreators = ElementFilter.methodsIn(companionClass.getEnclosedElements()).stream().filter(method -> !method.getModifiers().contains(PRIVATE)).filter(method -> method.getReturnType().equals(classElement.asType())).filter(method -> {
final AnnotationMetadata annotationMetadata = annotationUtils.getAnnotationMetadata(method);
return annotationMetadata.hasStereotype(Creator.class);
}).collect(Collectors.toList());
} else if (classElement.getKind() == ElementKind.ENUM) {
staticCreators = ElementFilter.methodsIn(classElement.getEnclosedElements()).stream().filter(method -> method.getModifiers().contains(STATIC)).filter(method -> !method.getModifiers().contains(PRIVATE)).filter(method -> method.getReturnType().equals(classElement.asType())).filter(method -> method.getSimpleName().toString().equals("valueOf")).collect(Collectors.toList());
}
}
return staticCreators;
}
use of io.micronaut.inject.ast.ClassElement in project micronaut-core by micronaut-projects.
the class JavaClassElement method getTypeArguments.
@Override
@NonNull
public Map<String, ClassElement> getTypeArguments() {
List<? extends TypeParameterElement> typeParameters = classElement.getTypeParameters();
Iterator<? extends TypeParameterElement> tpi = typeParameters.iterator();
Map<String, ClassElement> map = new LinkedHashMap<>();
while (tpi.hasNext()) {
TypeParameterElement tpe = tpi.next();
ClassElement classElement = mirrorToClassElement(tpe.asType(), visitorContext, this.genericTypeInfo, visitorContext.getConfiguration().includeTypeLevelAnnotationsInGenericArguments());
map.put(tpe.toString(), classElement);
}
return Collections.unmodifiableMap(map);
}
use of io.micronaut.inject.ast.ClassElement in project micronaut-core by micronaut-projects.
the class JavaClassElement method getTypeArguments.
@NonNull
@Override
public Map<String, ClassElement> getTypeArguments(@NonNull String type) {
if (StringUtils.isNotEmpty(type)) {
Map<String, Map<String, TypeMirror>> data = visitorContext.getGenericUtils().buildGenericTypeArgumentElementInfo(classElement, null, getBoundTypeMirrors());
Map<String, TypeMirror> forType = data.get(type);
if (forType != null) {
Map<String, ClassElement> typeArgs = new LinkedHashMap<>(forType.size());
for (Map.Entry<String, TypeMirror> entry : forType.entrySet()) {
TypeMirror v = entry.getValue();
ClassElement ce = v != null ? mirrorToClassElement(v, visitorContext, Collections.emptyMap(), visitorContext.getConfiguration().includeTypeLevelAnnotationsInGenericArguments()) : null;
if (ce == null) {
return Collections.emptyMap();
} else {
typeArgs.put(entry.getKey(), ce);
}
}
return Collections.unmodifiableMap(typeArgs);
}
}
return Collections.emptyMap();
}
use of io.micronaut.inject.ast.ClassElement in project micronaut-core by micronaut-projects.
the class JavaBeanDefinitionBuilder method createChildBean.
@Override
protected AbstractBeanDefinitionBuilder createChildBean(FieldElement producerField) {
final ClassElement parentType = getBeanType();
return new JavaBeanDefinitionBuilder(JavaBeanDefinitionBuilder.this.getOriginatingElement(), producerField.getGenericField().getType(), JavaBeanDefinitionBuilder.this.metadataBuilder, (JavaVisitorContext) JavaBeanDefinitionBuilder.this.visitorContext) {
@Override
public Element getProducingElement() {
return producerField;
}
@Override
public ClassElement getDeclaringElement() {
return producerField.getDeclaringType();
}
@Override
protected BeanDefinitionWriter createBeanDefinitionWriter() {
final BeanDefinitionWriter writer = super.createBeanDefinitionWriter();
final JavaElementFactory elementFactory = ((JavaVisitorContext) visitorContext).getElementFactory();
final VariableElement variableElement = (VariableElement) producerField.getNativeType();
writer.visitBeanFactoryField(parentType, elementFactory.newFieldElement(parentType, variableElement, new AnnotationMetadataHierarchy(parentType.getDeclaredMetadata(), producerField.getDeclaredMetadata())));
return writer;
}
};
}
Aggregations