use of io.micronaut.inject.ast.ParameterElement in project micronaut-core by micronaut-projects.
the class ClientTypesRule method validate.
@Override
public RouteValidationResult validate(List<UriMatchTemplate> templates, ParameterElement[] parameters, MethodElement method) {
String[] errors = new String[] {};
if (method.hasAnnotation(Client.class)) {
final Stream.Builder<ClassElement> builder = Stream.<ClassElement>builder().add(method.getReturnType());
for (ParameterElement param : method.getParameters()) {
builder.add(param.getType());
}
errors = builder.build().filter(type -> {
for (Class<?> clazz : SERVER_TYPES) {
if (type.isAssignable(clazz)) {
return true;
}
}
return false;
}).map(type -> "The type [" + type + "] must not be used in declarative client methods. The type is specific to server based usages.").toArray(String[]::new);
}
return new RouteValidationResult(errors);
}
use of io.micronaut.inject.ast.ParameterElement in project micronaut-core by micronaut-projects.
the class BeanIntrospectionWriter method invokeBeanConstructor.
private void invokeBeanConstructor(GeneratorAdapter writer, MethodElement constructor, BiConsumer<GeneratorAdapter, MethodElement> argumentsPusher) {
boolean isConstructor = constructor instanceof ConstructorElement;
boolean isCompanion = constructor != defaultConstructor && constructor.getDeclaringType().getSimpleName().endsWith("$Companion");
List<ParameterElement> constructorArguments = Arrays.asList(constructor.getParameters());
Collection<Type> argumentTypes = constructorArguments.stream().map(pe -> JavaModelUtils.getTypeReference(pe.getType())).collect(Collectors.toList());
if (isConstructor) {
writer.newInstance(beanType);
writer.dup();
} else if (isCompanion) {
writer.getStatic(beanType, "Companion", JavaModelUtils.getTypeReference(constructor.getDeclaringType()));
}
argumentsPusher.accept(writer, constructor);
if (isConstructor) {
final String constructorDescriptor = getConstructorDescriptor(constructorArguments);
writer.invokeConstructor(beanType, new Method("<init>", constructorDescriptor));
} else if (constructor.isStatic()) {
final String methodDescriptor = getMethodDescriptor(beanType, argumentTypes);
Method method = new Method(constructor.getName(), methodDescriptor);
if (classElement.isInterface()) {
writer.visitMethodInsn(Opcodes.INVOKESTATIC, beanType.getInternalName(), method.getName(), method.getDescriptor(), true);
} else {
writer.invokeStatic(beanType, method);
}
} else if (isCompanion) {
writer.invokeVirtual(JavaModelUtils.getTypeReference(constructor.getDeclaringType()), new Method(constructor.getName(), getMethodDescriptor(beanType, argumentTypes)));
}
}
use of io.micronaut.inject.ast.ParameterElement in project micronaut-core by micronaut-projects.
the class BeanDefinitionWriter method pushNewMethodReference.
private void pushNewMethodReference(GeneratorAdapter staticInit, Type beanType, MethodElement methodElement, boolean requiresReflection, boolean isPostConstructMethod, boolean isPreDestroyMethod) {
for (ParameterElement value : methodElement.getParameters()) {
DefaultAnnotationMetadata.contributeDefaults(this.annotationMetadata, value.getAnnotationMetadata());
DefaultAnnotationMetadata.contributeRepeatable(this.annotationMetadata, value.getGenericType());
}
staticInit.newInstance(Type.getType(AbstractInitializableBeanDefinition.MethodReference.class));
staticInit.dup();
// 1: declaringType
staticInit.push(beanType);
// 2: methodName
staticInit.push(methodElement.getName());
// 3: arguments
if (!methodElement.hasParameters()) {
staticInit.visitInsn(ACONST_NULL);
} else {
pushBuildArgumentsForMethod(beanFullClassName, beanDefinitionType, classWriter, staticInit, Arrays.asList(methodElement.getParameters()), defaultsStorage, loadTypeMethods);
}
// 4: annotationMetadata
pushAnnotationMetadata(staticInit, methodElement.getAnnotationMetadata());
// 5: requiresReflection
staticInit.push(requiresReflection);
if (isPreDestroyMethod || isPostConstructMethod) {
// 6: isPostConstructMethod
staticInit.push(isPostConstructMethod);
// 7: isPreDestroyMethod
staticInit.push(isPreDestroyMethod);
staticInit.invokeConstructor(Type.getType(AbstractInitializableBeanDefinition.MethodReference.class), METHOD_REFERENCE_CONSTRUCTOR_POST_PRE);
} else {
staticInit.invokeConstructor(Type.getType(AbstractInitializableBeanDefinition.MethodReference.class), METHOD_REFERENCE_CONSTRUCTOR);
}
}
use of io.micronaut.inject.ast.ParameterElement in project micronaut-core by micronaut-projects.
the class BeanDefinitionWriter method visitMethodInjectionPointInternal.
private void visitMethodInjectionPointInternal(MethodVisitData methodVisitData, GeneratorAdapter injectMethodVisitor, int injectInstanceIndex) {
MethodElement methodElement = methodVisitData.getMethodElement();
final AnnotationMetadata annotationMetadata = methodElement.getAnnotationMetadata();
final List<ParameterElement> argumentTypes = Arrays.asList(methodElement.getParameters());
applyDefaultNamedToParameters(argumentTypes);
final TypedElement declaringType = methodVisitData.beanType;
final String methodName = methodElement.getName();
final boolean requiresReflection = methodVisitData.requiresReflection;
final ClassElement returnType = methodElement.getReturnType();
DefaultAnnotationMetadata.contributeDefaults(this.annotationMetadata, annotationMetadata);
DefaultAnnotationMetadata.contributeRepeatable(this.annotationMetadata, returnType);
boolean hasArguments = methodElement.hasParameters();
int argCount = hasArguments ? argumentTypes.size() : 0;
Type declaringTypeRef = JavaModelUtils.getTypeReference(declaringType);
boolean hasInjectScope = false;
for (ParameterElement value : argumentTypes) {
DefaultAnnotationMetadata.contributeDefaults(this.annotationMetadata, value.getAnnotationMetadata());
DefaultAnnotationMetadata.contributeRepeatable(this.annotationMetadata, value.getGenericType());
if (value.hasDeclaredAnnotation(InjectScope.class)) {
hasInjectScope = true;
}
}
if (!requiresReflection) {
// if the method doesn't require reflection then invoke it directly
// invoke the method on this injected instance
injectMethodVisitor.loadLocal(injectInstanceIndex, beanType);
String methodDescriptor;
if (hasArguments) {
methodDescriptor = getMethodDescriptor(returnType, argumentTypes);
Iterator<ParameterElement> argIterator = argumentTypes.iterator();
for (int i = 0; i < argCount; i++) {
ParameterElement entry = argIterator.next();
pushMethodParameterValue(injectMethodVisitor, i, entry);
}
} else {
methodDescriptor = getMethodDescriptor(returnType, Collections.emptyList());
}
injectMethodVisitor.visitMethodInsn(isInterface ? INVOKEINTERFACE : INVOKEVIRTUAL, declaringTypeRef.getInternalName(), methodName, methodDescriptor, isInterface);
if (isConfigurationProperties && returnType != PrimitiveElement.VOID) {
injectMethodVisitor.pop();
}
} else {
injectMethodVisitor.loadThis();
injectMethodVisitor.loadArg(0);
injectMethodVisitor.loadArg(1);
injectMethodVisitor.push(currentMethodIndex);
injectMethodVisitor.loadLocal(injectInstanceLocalVarIndex, beanType);
if (hasArguments) {
pushNewArray(injectMethodVisitor, Object.class, argumentTypes.size());
Iterator<ParameterElement> argIterator = argumentTypes.iterator();
for (int i = 0; i < argCount; i++) {
int finalI = i;
pushStoreInArray(injectMethodVisitor, i, argumentTypes.size(), () -> {
ParameterElement entry = argIterator.next();
pushMethodParameterValue(injectMethodVisitor, finalI, entry);
pushBoxPrimitiveIfNecessary(entry.getType(), injectMethodVisitor);
});
}
} else {
pushNewArray(injectMethodVisitor, Object.class, 0);
}
injectMethodVisitor.invokeVirtual(superType, INVOKE_WITH_REFLECTION_METHOD);
}
destroyInjectScopeBeansIfNecessary(injectMethodVisitor, hasInjectScope);
}
use of io.micronaut.inject.ast.ParameterElement in project micronaut-core by micronaut-projects.
the class BeanDefinitionWriter method visitBeanDefinitionConstructorInternal.
private void visitBeanDefinitionConstructorInternal(GeneratorAdapter staticInit, Object constructor, boolean requiresReflection) {
if (constructor instanceof MethodElement) {
MethodElement methodElement = (MethodElement) constructor;
AnnotationMetadata constructorMetadata = methodElement.getAnnotationMetadata();
DefaultAnnotationMetadata.contributeDefaults(this.annotationMetadata, constructorMetadata);
DefaultAnnotationMetadata.contributeRepeatable(this.annotationMetadata, methodElement.getGenericReturnType());
ParameterElement[] parameters = methodElement.getParameters();
List<ParameterElement> parameterList = Arrays.asList(parameters);
applyDefaultNamedToParameters(parameterList);
pushNewMethodReference(staticInit, JavaModelUtils.getTypeReference(methodElement.getDeclaringType()), methodElement, requiresReflection, false, false);
} else if (constructor instanceof FieldElement) {
FieldElement fieldConstructor = (FieldElement) constructor;
pushNewFieldReference(staticInit, JavaModelUtils.getTypeReference(fieldConstructor.getDeclaringType()), fieldConstructor, constructorRequiresReflection);
} else {
throw new IllegalArgumentException("Unexpected constructor: " + constructor);
}
staticInit.putStatic(beanDefinitionType, FIELD_CONSTRUCTOR, Type.getType(AbstractInitializableBeanDefinition.MethodOrFieldReference.class));
GeneratorAdapter publicConstructor = new GeneratorAdapter(classWriter.visitMethod(ACC_PUBLIC, CONSTRUCTOR_NAME, DESCRIPTOR_DEFAULT_CONSTRUCTOR, null, null), ACC_PUBLIC, CONSTRUCTOR_NAME, DESCRIPTOR_DEFAULT_CONSTRUCTOR);
publicConstructor.loadThis();
publicConstructor.push(beanType);
publicConstructor.getStatic(beanDefinitionType, FIELD_CONSTRUCTOR, Type.getType(AbstractInitializableBeanDefinition.MethodOrFieldReference.class));
publicConstructor.invokeConstructor(superBeanDefinition ? superType : beanDefinitionType, PROTECTED_ABSTRACT_BEAN_DEFINITION_CONSTRUCTOR);
publicConstructor.visitInsn(RETURN);
publicConstructor.visitMaxs(5, 1);
publicConstructor.visitEnd();
if (!superBeanDefinition) {
// create protected constructor for subclasses of AbstractBeanDefinition
GeneratorAdapter protectedConstructor = new GeneratorAdapter(classWriter.visitMethod(ACC_PROTECTED, PROTECTED_ABSTRACT_BEAN_DEFINITION_CONSTRUCTOR.getName(), PROTECTED_ABSTRACT_BEAN_DEFINITION_CONSTRUCTOR.getDescriptor(), null, null), ACC_PROTECTED, PROTECTED_ABSTRACT_BEAN_DEFINITION_CONSTRUCTOR.getName(), PROTECTED_ABSTRACT_BEAN_DEFINITION_CONSTRUCTOR.getDescriptor());
AnnotationMetadata annotationMetadata = this.annotationMetadata != null ? this.annotationMetadata : AnnotationMetadata.EMPTY_METADATA;
protectedConstructor.loadThis();
// 1: beanType
protectedConstructor.loadArg(0);
// 2: `AbstractBeanDefinition2.MethodOrFieldReference.class` constructor
protectedConstructor.loadArg(1);
// 3: annotationMetadata
if (this.annotationMetadata == null) {
protectedConstructor.push((String) null);
} else {
protectedConstructor.getStatic(getTypeReferenceForName(getBeanDefinitionReferenceClassName()), AbstractAnnotationMetadataWriter.FIELD_ANNOTATION_METADATA, Type.getType(AnnotationMetadata.class));
}
// 4: `AbstractBeanDefinition2.MethodReference[].class` methodInjection
if (allMethodVisits.isEmpty()) {
protectedConstructor.push((String) null);
} else {
protectedConstructor.getStatic(beanDefinitionType, FIELD_INJECTION_METHODS, Type.getType(AbstractInitializableBeanDefinition.MethodReference[].class));
}
// 5: `AbstractBeanDefinition2.FieldReference[].class` fieldInjection
if (fieldInjectionPoints.isEmpty()) {
protectedConstructor.push((String) null);
} else {
protectedConstructor.getStatic(beanDefinitionType, FIELD_INJECTION_FIELDS, Type.getType(AbstractInitializableBeanDefinition.FieldReference[].class));
}
// 6: `ExecutableMethod[]` executableMethods
if (executableMethodsDefinitionWriter == null) {
protectedConstructor.push((String) null);
} else {
protectedConstructor.newInstance(executableMethodsDefinitionWriter.getClassType());
protectedConstructor.dup();
protectedConstructor.invokeConstructor(executableMethodsDefinitionWriter.getClassType(), METHOD_DEFAULT_CONSTRUCTOR);
}
// 7: `Map<String, Argument<?>[]>` typeArgumentsMap
if (!hasTypeArguments()) {
protectedConstructor.push((String) null);
} else {
protectedConstructor.getStatic(beanDefinitionType, FIELD_TYPE_ARGUMENTS, Type.getType(Map.class));
}
// 8: `Optional` scope
String scope = annotationMetadata.getAnnotationNameByStereotype(AnnotationUtil.SCOPE).orElse(null);
if (scope != null) {
protectedConstructor.push(scope);
protectedConstructor.invokeStatic(TYPE_OPTIONAL, METHOD_OPTIONAL_OF);
} else {
protectedConstructor.invokeStatic(TYPE_OPTIONAL, METHOD_OPTIONAL_EMPTY);
}
// 9: `boolean` isAbstract
protectedConstructor.push(isAbstract);
// 10: `boolean` isProvided
protectedConstructor.push(annotationMetadata.hasDeclaredStereotype(Provided.class));
// 11: `boolean` isIterable
protectedConstructor.push(isIterable(annotationMetadata));
// 12: `boolean` isSingleton
protectedConstructor.push(isSingleton(scope));
// 13: `boolean` isPrimary
protectedConstructor.push(annotationMetadata.hasDeclaredStereotype(Primary.class));
// 14: `boolean` isConfigurationProperties
protectedConstructor.push(isConfigurationProperties);
// 15: isContainerType
protectedConstructor.push(isContainerType());
// 16: requiresMethodProcessing
protectedConstructor.push(preprocessMethods);
protectedConstructor.invokeConstructor(isSuperFactory ? TYPE_ABSTRACT_BEAN_DEFINITION : superType, BEAN_DEFINITION_CLASS_CONSTRUCTOR);
protectedConstructor.visitInsn(RETURN);
protectedConstructor.visitMaxs(20, 1);
protectedConstructor.visitEnd();
}
}
Aggregations