lombok.experimental.Wither Java Examples

The following examples show how to use lombok.experimental.Wither. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HandleWither.java    From EasyMPermission with MIT License 6 votes vote down vote up
@Override public void handle(AnnotationValues<Wither> annotation, Annotation ast, EclipseNode annotationNode) {
	handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.WITHER_FLAG_USAGE, "@Wither");
	
	EclipseNode node = annotationNode.up();
	AccessLevel level = annotation.getInstance().value();
	if (level == AccessLevel.NONE || node == null) return;
	
	List<Annotation> onMethod = unboxAndRemoveAnnotationParameter(ast, "onMethod", "@Wither(onMethod=", annotationNode);
	List<Annotation> onParam = unboxAndRemoveAnnotationParameter(ast, "onParam", "@Wither(onParam=", annotationNode);
	
	switch (node.getKind()) {
	case FIELD:
		createWitherForFields(level, annotationNode.upFromAnnotationToFields(), annotationNode, true, onMethod, onParam);
		break;
	case TYPE:
		if (!onMethod.isEmpty()) {
			annotationNode.addError("'onMethod' is not supported for @Wither on a type.");
		}
		if (!onParam.isEmpty()) {
			annotationNode.addError("'onParam' is not supported for @Wither on a type.");
		}
		generateWitherForType(node, annotationNode, level, false);
		break;
	}
}
 
Example #2
Source File: HandleWither.java    From EasyMPermission with MIT License 6 votes vote down vote up
@Override public void handle(AnnotationValues<Wither> annotation, JCAnnotation ast, JavacNode annotationNode) {
	handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.WITHER_FLAG_USAGE, "@Wither");
	
	Collection<JavacNode> fields = annotationNode.upFromAnnotationToFields();
	deleteAnnotationIfNeccessary(annotationNode, Wither.class);
	deleteImportFromCompilationUnit(annotationNode, "lombok.AccessLevel");
	JavacNode node = annotationNode.up();
	AccessLevel level = annotation.getInstance().value();
	
	if (level == AccessLevel.NONE || node == null) return;
	
	List<JCAnnotation> onMethod = unboxAndRemoveAnnotationParameter(ast, "onMethod", "@Wither(onMethod=", annotationNode);
	List<JCAnnotation> onParam = unboxAndRemoveAnnotationParameter(ast, "onParam", "@Wither(onParam=", annotationNode);
	
	switch (node.getKind()) {
	case FIELD:
		createWitherForFields(level, fields, annotationNode, true, onMethod, onParam);
		break;
	case TYPE:
		if (!onMethod.isEmpty()) annotationNode.addError("'onMethod' is not supported for @Wither on a type.");
		if (!onParam.isEmpty()) annotationNode.addError("'onParam' is not supported for @Wither on a type.");
		generateWitherForType(node, annotationNode, level, false);
		break;
	}
}
 
Example #3
Source File: WitherProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@NotNull
private Collection<PsiField> getWitherFields(@NotNull PsiClass psiClass) {
  Collection<PsiField> witherFields = new ArrayList<>();
  for (PsiField psiField : psiClass.getFields()) {
    boolean createWither = true;
    PsiModifierList modifierList = psiField.getModifierList();
    if (null != modifierList) {
      // Skip static fields.
      createWither = !modifierList.hasModifierProperty(PsiModifier.STATIC);
      // Skip final fields that are initialized and not annotated with @Builder.Default
      createWither &= !(modifierList.hasModifierProperty(PsiModifier.FINAL) && psiField.hasInitializer() &&
        PsiAnnotationSearchUtil.findAnnotation(psiField, BUILDER_DEFAULT_ANNOTATION) == null);
      // Skip fields that start with $
      createWither &= !psiField.getName().startsWith(LombokUtils.LOMBOK_INTERN_FIELD_MARKER);
      // Skip fields having Wither annotation already
      createWither &= !PsiAnnotationSearchUtil.isAnnotatedWith(psiField, Wither.class, With.class);
    }
    if (createWither) {
      witherFields.add(psiField);
    }
  }
  return witherFields;
}
 
Example #4
Source File: HandleWither.java    From EasyMPermission with MIT License 5 votes vote down vote up
/**
 * Generates a wither on the stated field.
 * 
 * Used by {@link HandleValue}.
 * 
 * The difference between this call and the handle method is as follows:
 * 
 * If there is a {@code lombok.experimental.Wither} annotation on the field, it is used and the
 * same rules apply (e.g. warning if the method already exists, stated access level applies).
 * If not, the wither is still generated if it isn't already there, though there will not
 * be a warning if its already there. The default access level is used.
 */
public void generateWitherForField(EclipseNode fieldNode, EclipseNode sourceNode, AccessLevel level) {
	for (EclipseNode child : fieldNode.down()) {
		if (child.getKind() == Kind.ANNOTATION) {
			if (annotationTypeMatches(Wither.class, child)) {
				//The annotation will make it happen, so we can skip it.
				return;
			}
		}
	}
	
	List<Annotation> empty = Collections.emptyList();
	createWitherForField(level, fieldNode, sourceNode, false, empty, empty);
}
 
Example #5
Source File: HandleWither.java    From EasyMPermission with MIT License 5 votes vote down vote up
public void generateWitherForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean checkForTypeLevelWither) {
	if (checkForTypeLevelWither) {
		if (hasAnnotation(Wither.class, typeNode)) {
			//The annotation will make it happen, so we can skip it.
			return;
		}
	}
	
	JCClassDecl typeDecl = null;
	if (typeNode.get() instanceof JCClassDecl) typeDecl = (JCClassDecl) typeNode.get();
	long modifiers = typeDecl == null ? 0 : typeDecl.mods.flags;
	boolean notAClass = (modifiers & (Flags.INTERFACE | Flags.ANNOTATION | Flags.ENUM)) != 0;
	
	if (typeDecl == null || notAClass) {
		errorNode.addError("@Wither is only supported on a class or a field.");
		return;
	}
	
	for (JavacNode field : typeNode.down()) {
		if (field.getKind() != Kind.FIELD) continue;
		JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
		//Skip fields that start with $
		if (fieldDecl.name.toString().startsWith("$")) continue;
		//Skip static fields.
		if ((fieldDecl.mods.flags & Flags.STATIC) != 0) continue;
		//Skip final initialized fields.
		if ((fieldDecl.mods.flags & Flags.FINAL) != 0 && fieldDecl.init != null) continue;
		
		generateWitherForField(field, errorNode.get(), level);
	}
}
 
Example #6
Source File: WitherFieldProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
public PsiMethod createWitherMethod(@NotNull PsiField psiField, @NotNull String methodModifier, @NotNull AccessorsInfo accessorsInfo) {
  LombokLightMethodBuilder methodBuilder = null;
  final PsiClass psiFieldContainingClass = psiField.getContainingClass();
  if (psiFieldContainingClass != null) {
    final PsiType returnType = PsiClassUtil.getTypeWithGenerics(psiFieldContainingClass);
    final String psiFieldName = psiField.getName();
    final PsiType psiFieldType = psiField.getType();

    methodBuilder = new LombokLightMethodBuilder(psiField.getManager(), getWitherName(accessorsInfo, psiFieldName, psiFieldType))
      .withMethodReturnType(returnType)
      .withContainingClass(psiFieldContainingClass)
      .withNavigationElement(psiField)
      .withModifier(methodModifier);

    PsiAnnotation witherAnnotation = PsiAnnotationSearchUtil.findAnnotation(psiField, Wither.class, With.class);
    addOnXAnnotations(witherAnnotation, methodBuilder.getModifierList(), "onMethod");

    final LombokLightParameter methodParameter = new LombokLightParameter(psiFieldName, psiFieldType, methodBuilder, JavaLanguage.INSTANCE);
    PsiModifierList methodParameterModifierList = methodParameter.getModifierList();
    copyAnnotations(psiField, methodParameterModifierList,
      LombokUtils.NON_NULL_PATTERN, LombokUtils.NULLABLE_PATTERN, LombokUtils.DEPRECATED_PATTERN);
    addOnXAnnotations(witherAnnotation, methodParameterModifierList, "onParam");
    methodBuilder.withParameter(methodParameter);

    if (psiFieldContainingClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
      methodBuilder.withModifier(PsiModifier.ABSTRACT);
    } else {
      final String paramString = getConstructorCall(psiField, psiFieldContainingClass);
      final String blockText = String.format("return this.%s == %s ? this : new %s(%s);", psiFieldName, psiFieldName, returnType.getCanonicalText(), paramString);
      methodBuilder.withBody(PsiMethodUtil.createCodeBlockFromText(blockText, methodBuilder));
    }
  }
  return methodBuilder;
}
 
Example #7
Source File: WitherProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public WitherProcessor() {
  super(PsiMethod.class, Wither.class, With.class);
}
 
Example #8
Source File: WitherFieldProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public WitherFieldProcessor() {
  super(PsiMethod.class, Wither.class, With.class);
}
 
Example #9
Source File: HandleWither.java    From EasyMPermission with MIT License 3 votes vote down vote up
/**
 * Generates a wither on the stated field.
 * 
 * Used by {@link HandleValue}.
 * 
 * The difference between this call and the handle method is as follows:
 * 
 * If there is a {@code lombok.experimental.Wither} annotation on the field, it is used and the
 * same rules apply (e.g. warning if the method already exists, stated access level applies).
 * If not, the wither is still generated if it isn't already there, though there will not
 * be a warning if its already there. The default access level is used.
 * 
 * @param fieldNode The node representing the field you want a wither for.
 * @param pos The node responsible for generating the wither (the {@code @Value} or {@code @Wither} annotation).
 */
public void generateWitherForField(JavacNode fieldNode, DiagnosticPosition pos, AccessLevel level) {
	if (hasAnnotation(Wither.class, fieldNode)) {
		//The annotation will make it happen, so we can skip it.
		return;
	}
	
	createWitherForField(level, fieldNode, fieldNode, false, List.<JCAnnotation>nil(), List.<JCAnnotation>nil());
}