lombok.experimental.Accessors Java Examples

The following examples show how to use lombok.experimental.Accessors. 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: HandlerUtil.java    From EasyMPermission with MIT License 6 votes vote down vote up
public static boolean shouldReturnThis0(AnnotationValues<Accessors> accessors, AST<?, ?, ?> ast) {
	boolean chainForced = accessors.isExplicit("chain");
	boolean fluentForced = accessors.isExplicit("fluent");
	Accessors instance = accessors.getInstance();
	
	boolean chain = instance.chain();
	boolean fluent = instance.fluent();
	
	if (chainForced) return chain;
	
	if (!chainForced) {
		Boolean chainConfig = ast.readConfiguration(ConfigurationKeys.ACCESSORS_CHAIN);
		if (chainConfig != null) return chainConfig;
	}
	
	if (!fluentForced) {
		Boolean fluentConfig = ast.readConfiguration(ConfigurationKeys.ACCESSORS_FLUENT);
		if (fluentConfig != null) fluent = fluentConfig;
	}
	
	return chain || fluent;
}
 
Example #2
Source File: HandleAccessors.java    From EasyMPermission with MIT License 5 votes vote down vote up
@Override public void handle(AnnotationValues<Accessors> annotation, JCAnnotation ast, JavacNode annotationNode) {
	// Accessors itself is handled by HandleGetter/Setter; this is just to ensure that the annotation is removed
	// from the AST when delomboking.
	
	handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.ACCESSORS_FLAG_USAGE, "@Accessors");
	
	deleteAnnotationIfNeccessary(annotationNode, Accessors.class);
}
 
Example #3
Source File: JavacHandlerUtil.java    From EasyMPermission with MIT License 5 votes vote down vote up
/**
 * When generating a setter, the setter either returns void (beanspec) or Self (fluent).
 * This method scans for the {@code Accessors} annotation to figure that out.
 */
public static boolean shouldReturnThis(JavacNode field) {
	if ((((JCVariableDecl) field.get()).mods.flags & Flags.STATIC) != 0) return false;
	
	AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field);
	
	return HandlerUtil.shouldReturnThis0(accessors, field.getAst());
}
 
Example #4
Source File: HandlerUtil.java    From EasyMPermission with MIT License 5 votes vote down vote up
private static String toAccessorName(AST<?, ?, ?> ast, AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean,
		String booleanPrefix, String normalPrefix, boolean adhereToFluent) {
	
	fieldName = fieldName.toString();
	if (fieldName.length() == 0) return null;
	
	if (Boolean.TRUE.equals(ast.readConfiguration(ConfigurationKeys.GETTER_CONSEQUENT_BOOLEAN))) isBoolean = false;
	boolean explicitPrefix = accessors != null && accessors.isExplicit("prefix");
	boolean explicitFluent = accessors != null && accessors.isExplicit("fluent");
	
	Accessors ac = (explicitPrefix || explicitFluent) ? accessors.getInstance() : null;
	
	List<String> prefix = explicitPrefix ? Arrays.asList(ac.prefix()) : ast.readConfiguration(ConfigurationKeys.ACCESSORS_PREFIX);
	boolean fluent = explicitFluent ? ac.fluent() : Boolean.TRUE.equals(ast.readConfiguration(ConfigurationKeys.ACCESSORS_FLUENT));
	
	fieldName = removePrefix(fieldName, prefix);
	if (fieldName == null) return null;
	
	String fName = fieldName.toString();
	if (adhereToFluent && fluent) return fName;
	
	if (isBoolean && fName.startsWith("is") && fieldName.length() > 2 && !Character.isLowerCase(fieldName.charAt(2))) {
		// The field is for example named 'isRunning'.
		return booleanPrefix + fName.substring(2);
	}
	
	return buildAccessorName(isBoolean ? booleanPrefix : normalPrefix, fName);
}
 
Example #5
Source File: HandlerUtil.java    From EasyMPermission with MIT License 5 votes vote down vote up
private static List<String> toAllAccessorNames(AST<?, ?, ?> ast, AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean,
		String booleanPrefix, String normalPrefix, boolean adhereToFluent) {
	
	if (Boolean.TRUE.equals(ast.readConfiguration(ConfigurationKeys.GETTER_CONSEQUENT_BOOLEAN))) isBoolean = false;
	if (!isBoolean) {
		String accessorName = toAccessorName(ast, accessors, fieldName, false, booleanPrefix, normalPrefix, adhereToFluent);
		return (accessorName == null) ? Collections.<String>emptyList() : Collections.singletonList(accessorName);
	}
	
	boolean explicitPrefix = accessors != null && accessors.isExplicit("prefix");
	boolean explicitFluent = accessors != null && accessors.isExplicit("fluent");
	
	Accessors ac = (explicitPrefix || explicitFluent) ? accessors.getInstance() : null;
	
	List<String> prefix = explicitPrefix ? Arrays.asList(ac.prefix()) : ast.readConfiguration(ConfigurationKeys.ACCESSORS_PREFIX);
	boolean fluent = explicitFluent ? ac.fluent() : Boolean.TRUE.equals(ast.readConfiguration(ConfigurationKeys.ACCESSORS_FLUENT));
	
	fieldName = removePrefix(fieldName, prefix);
	if (fieldName == null) return Collections.emptyList();
	
	List<String> baseNames = toBaseNames(fieldName, isBoolean, fluent);
	
	Set<String> names = new HashSet<String>();
	for (String baseName : baseNames) {
		if (adhereToFluent && fluent) {
			names.add(baseName);
		} else {
			names.add(buildAccessorName(normalPrefix, baseName));
			if (!normalPrefix.equals(booleanPrefix)) names.add(buildAccessorName(booleanPrefix, baseName));
		}
	}
	
	return new ArrayList<String>(names);
	
}
 
Example #6
Source File: AccessorsInfo.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@NotNull
public static AccessorsInfo build(@NotNull PsiVariable psiVariable, @Nullable PsiClass containingClass) {
  final PsiAnnotation accessorsFieldAnnotation = PsiAnnotationSearchUtil.findAnnotation(psiVariable, Accessors.class);
  if (null != accessorsFieldAnnotation) {
    return buildFromAnnotation(accessorsFieldAnnotation, containingClass);
  } else {
    return build(containingClass);
  }
}
 
Example #7
Source File: AccessorsInfo.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@NotNull
public static AccessorsInfo build(@NotNull PsiField psiField, @NotNull AccessorsInfo classAccessorsInfo) {
  final PsiAnnotation accessorsFieldAnnotation = PsiAnnotationSearchUtil.findAnnotation(psiField, Accessors.class);
  if (null != accessorsFieldAnnotation) {
    return buildFromAnnotation(accessorsFieldAnnotation, psiField.getContainingClass());
  } else {
    return classAccessorsInfo;
  }
}
 
Example #8
Source File: AccessorsInfo.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@NotNull
public static AccessorsInfo build(@Nullable PsiClass psiClass) {
  PsiClass containingClass = psiClass;
  while (null != containingClass) {
    final PsiAnnotation accessorsClassAnnotation = PsiAnnotationSearchUtil.findAnnotation(containingClass, Accessors.class);
    if (null != accessorsClassAnnotation) {
      return buildFromAnnotation(accessorsClassAnnotation, containingClass);
    }
    containingClass = containingClass.getContainingClass();
  }

  return buildAccessorsInfo(psiClass, null, null, Collections.emptySet());
}
 
Example #9
Source File: HandleAccessors.java    From EasyMPermission with MIT License 4 votes vote down vote up
@Override public void handle(AnnotationValues<Accessors> annotation, Annotation ast, EclipseNode annotationNode) {
	// Accessors itself is handled by HandleGetter/Setter; this is just to ensure that usages are flagged if requested.
	
	handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.ACCESSORS_FLAG_USAGE, "@Accessors");
}
 
Example #10
Source File: EclipseHandlerUtil.java    From EasyMPermission with MIT License 4 votes vote down vote up
/**
 * When generating a setter, the setter either returns void (beanspec) or Self (fluent).
 * This method scans for the {@code Accessors} annotation and associated config properties to figure that out.
 */
public static boolean shouldReturnThis(EclipseNode field) {
	if ((((FieldDeclaration) field.get()).modifiers & ClassFileConstants.AccStatic) != 0) return false;
	AnnotationValues<Accessors> accessors = EclipseHandlerUtil.getAccessorsForField(field);
	return shouldReturnThis0(accessors, field.getAst());
}
 
Example #11
Source File: HandlerUtil.java    From EasyMPermission with MIT License 2 votes vote down vote up
/**
 * Generates a getter name from a given field name.
 * 
 * Strategy:
 * <ul>
 * <li>Reduce the field's name to its base name by stripping off any prefix (from {@code Accessors}). If the field name does not fit
 * the prefix list, this method immediately returns {@code null}.</li>
 * <li>If {@code Accessors} has {@code fluent=true}, then return the basename.</li>
 * <li>Pick a prefix. 'get' normally, but 'is' if {@code isBoolean} is true.</li>
 * <li>Only if {@code isBoolean} is true: Check if the field starts with {@code is} followed by a non-lowercase character. If so, return the field name verbatim.</li> 
 * <li>Check if the first character of the field is lowercase. If so, check if the second character
 * exists and is title or upper case. If so, uppercase the first character. If not, titlecase the first character.</li>
 * <li>Return the prefix plus the possibly title/uppercased first character, and the rest of the field name.</li>
 * </ul>
 * 
 * @param accessors Accessors configuration.
 * @param fieldName the name of the field.
 * @param isBoolean if the field is of type 'boolean'. For fields of type {@code java.lang.Boolean}, you should provide {@code false}.
 * @return The getter name for this field, or {@code null} if this field does not fit expected patterns and therefore cannot be turned into a getter name.
 */
public static String toGetterName(AST<?, ?, ?> ast, AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) {
	return toAccessorName(ast, accessors, fieldName, isBoolean, "is", "get", true);
}
 
Example #12
Source File: HandlerUtil.java    From EasyMPermission with MIT License 2 votes vote down vote up
/**
 * Generates a setter name from a given field name.
 * 
 * Strategy:
 * <ul>
 * <li>Reduce the field's name to its base name by stripping off any prefix (from {@code Accessors}). If the field name does not fit
 * the prefix list, this method immediately returns {@code null}.</li>
 * <li>If {@code Accessors} has {@code fluent=true}, then return the basename.</li>
 * <li>Only if {@code isBoolean} is true: Check if the field starts with {@code is} followed by a non-lowercase character.
 * If so, replace {@code is} with {@code set} and return that.</li> 
 * <li>Check if the first character of the field is lowercase. If so, check if the second character
 * exists and is title or upper case. If so, uppercase the first character. If not, titlecase the first character.</li>
 * <li>Return {@code "set"} plus the possibly title/uppercased first character, and the rest of the field name.</li>
 * </ul>
 * 
 * @param accessors Accessors configuration.
 * @param fieldName the name of the field.
 * @param isBoolean if the field is of type 'boolean'. For fields of type {@code java.lang.Boolean}, you should provide {@code false}.
 * @return The setter name for this field, or {@code null} if this field does not fit expected patterns and therefore cannot be turned into a getter name.
 */
public static String toSetterName(AST<?, ?, ?> ast, AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) {
	return toAccessorName(ast, accessors, fieldName, isBoolean, "set", "set", true);
}
 
Example #13
Source File: HandlerUtil.java    From EasyMPermission with MIT License 2 votes vote down vote up
/**
 * Generates a wither name from a given field name.
 * 
 * Strategy:
 * <ul>
 * <li>Reduce the field's name to its base name by stripping off any prefix (from {@code Accessors}). If the field name does not fit
 * the prefix list, this method immediately returns {@code null}.</li>
 * <li>Only if {@code isBoolean} is true: Check if the field starts with {@code is} followed by a non-lowercase character.
 * If so, replace {@code is} with {@code with} and return that.</li> 
 * <li>Check if the first character of the field is lowercase. If so, check if the second character
 * exists and is title or upper case. If so, uppercase the first character. If not, titlecase the first character.</li>
 * <li>Return {@code "with"} plus the possibly title/uppercased first character, and the rest of the field name.</li>
 * </ul>
 * 
 * @param accessors Accessors configuration.
 * @param fieldName the name of the field.
 * @param isBoolean if the field is of type 'boolean'. For fields of type {@code java.lang.Boolean}, you should provide {@code false}.
 * @return The wither name for this field, or {@code null} if this field does not fit expected patterns and therefore cannot be turned into a getter name.
 */
public static String toWitherName(AST<?, ?, ?> ast, AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) {
	return toAccessorName(ast, accessors, fieldName, isBoolean, "with", "with", false);
}
 
Example #14
Source File: HandlerUtil.java    From EasyMPermission with MIT License 2 votes vote down vote up
/**
 * Returns all names of methods that would represent the getter for a field with the provided name.
 * 
 * For example if {@code isBoolean} is true, then a field named {@code isRunning} would produce:<br />
 * {@code [isRunning, getRunning, isIsRunning, getIsRunning]}
 * 
 * @param accessors Accessors configuration.
 * @param fieldName the name of the field.
 * @param isBoolean if the field is of type 'boolean'. For fields of type 'java.lang.Boolean', you should provide {@code false}.
 */
public static List<String> toAllGetterNames(AST<?, ?, ?> ast, AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) {
	return toAllAccessorNames(ast, accessors, fieldName, isBoolean, "is", "get", true);
}
 
Example #15
Source File: HandlerUtil.java    From EasyMPermission with MIT License 2 votes vote down vote up
/**
 * Returns all names of methods that would represent the setter for a field with the provided name.
 * 
 * For example if {@code isBoolean} is true, then a field named {@code isRunning} would produce:<br />
 * {@code [setRunning, setIsRunning]}
 * 
 * @param accessors Accessors configuration.
 * @param fieldName the name of the field.
 * @param isBoolean if the field is of type 'boolean'. For fields of type 'java.lang.Boolean', you should provide {@code false}.
 */
public static List<String> toAllSetterNames(AST<?, ?, ?> ast, AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) {
	return toAllAccessorNames(ast, accessors, fieldName, isBoolean, "set", "set", true);
}
 
Example #16
Source File: HandlerUtil.java    From EasyMPermission with MIT License 2 votes vote down vote up
/**
 * Returns all names of methods that would represent the wither for a field with the provided name.
 * 
 * For example if {@code isBoolean} is true, then a field named {@code isRunning} would produce:<br />
 * {@code [withRunning, withIsRunning]}
 * 
 * @param accessors Accessors configuration.
 * @param fieldName the name of the field.
 * @param isBoolean if the field is of type 'boolean'. For fields of type 'java.lang.Boolean', you should provide {@code false}.
 */
public static List<String> toAllWitherNames(AST<?, ?, ?> ast, AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) {
	return toAllAccessorNames(ast, accessors, fieldName, isBoolean, "with", "with", false);
}