Skip to content

Commit

Permalink
Merge pull request #63 from treblereel/@Lazy_initial
Browse files Browse the repository at this point in the history
added @lazy annotation and initial processing
  • Loading branch information
treblereel authored Mar 16, 2021
2 parents 86cc81e + 4b348e5 commit b76c318
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
29 changes: 29 additions & 0 deletions annotations/src/main/java/io/crysknife/annotation/Lazy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright © 2021 Treblereel
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package io.crysknife.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author Dmitrii Tikhomirov Created by treblereel 3/16/21
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
public @interface Lazy {

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Qualifier;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
Expand All @@ -39,6 +40,7 @@
import com.github.javaparser.ast.expr.ObjectCreationExpr;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.google.auto.common.MoreElements;
import com.google.auto.common.MoreTypes;
import io.crysknife.exception.GenerationException;
import io.crysknife.generator.BeanIOCGenerator;
import io.crysknife.generator.IOCGenerator;
Expand Down Expand Up @@ -184,10 +186,17 @@ private FieldPoint parseField(Element type, IOCContext context) {
.ifPresent(elm -> getDependsOn().add(context.getBeanDefinitionOrCreateAndReturn(elm)));
bean = context.getBeanDefinitionOrCreateAndReturn(field.getType());
} else if (context.getQualifiers().containsKey(field.getType())) {
// TODO what if type has several qualifiers ???
for (AnnotationMirror mirror : context.getGenerationContext().getElements()
.getAllAnnotationMirrors(type)) {
bean =
context.getQualifiers().get(field.getType()).get(mirror.getAnnotationType().toString());
DeclaredType annotationType = mirror.getAnnotationType();
Qualifier qualifier = annotationType.asElement().getAnnotation(Qualifier.class);
// exclude all annotations, except Qualifiers
if (qualifier != null) {
bean = context.getQualifiers().get(field.getType())
.get(mirror.getAnnotationType().toString());
break;
}
}
} else {
bean = context.getBeanDefinitionOrCreateAndReturn(field.getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.google.auto.common.MoreElements;
import com.google.auto.common.MoreTypes;
import io.crysknife.annotation.Lazy;

/**
* @author Dmitrii Tikhomirov Created by treblereel 3/3/19
Expand All @@ -30,9 +31,12 @@ public class FieldPoint extends Point {

private VariableElement field;

private boolean isLazy;

private FieldPoint(String name, TypeElement injection, VariableElement field) {
super(injection, name);
this.field = field;
this.isLazy = field.getAnnotation(Lazy.class) != null;
}

public static FieldPoint of(VariableElement injection) {
Expand Down Expand Up @@ -79,11 +83,19 @@ public boolean equals(Object o) {

@Override
public String toString() {
return "FieldPoint{" + "injection=" + type + " name=" + name + (isNamed() ? getNamed() : "")
+ '}';
return "FieldPoint{" + "field=" + field + ", name='" + name + '\'' + ", type=" + type
+ ", isLazy=" + isLazy + '}';
}

public boolean isNamed() {
return field.getAnnotation(Named.class) != null;
}

public boolean isLazy() {
return isLazy;
}

public void setLazy(boolean lazy) {
isLazy = lazy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import javax.inject.Inject;
import javax.inject.Singleton;

import io.crysknife.annotation.Lazy;

/**
* @author Dmitrii Tikhomirov Created by treblereel 4/14/19
*/
Expand All @@ -28,10 +30,11 @@ public class QualifierConstructorInjection {
public QualifierBean qualifierBeanTwo;

@Inject
@Lazy
public QualifierBean qualifier;

@Inject
public QualifierConstructorInjection(@QualifierTwo QualifierBean qualifierBeanTwo,
public QualifierConstructorInjection(@QualifierTwo @Lazy QualifierBean qualifierBeanTwo,
@QualifierOne QualifierBean qualifierBeanOne) {
this.qualifierBeanOne = qualifierBeanOne;
this.qualifierBeanTwo = qualifierBeanTwo;
Expand Down

0 comments on commit b76c318

Please sign in to comment.