Java Code Examples for lombok.javac.JavacNode#down()

The following examples show how to use lombok.javac.JavacNode#down() . 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: HandleConstructor.java    From EasyMPermission with MIT License 6 votes vote down vote up
public static List<JavacNode> findRequiredFields(JavacNode typeNode) {
	ListBuffer<JavacNode> fields = new ListBuffer<JavacNode>();
	for (JavacNode child : typeNode.down()) {
		if (child.getKind() != Kind.FIELD) continue;
		JCVariableDecl fieldDecl = (JCVariableDecl) child.get();
		//Skip fields that start with $
		if (fieldDecl.name.toString().startsWith("$")) continue;
		long fieldFlags = fieldDecl.mods.flags;
		//Skip static fields.
		if ((fieldFlags & Flags.STATIC) != 0) continue;
		boolean isFinal = (fieldFlags & Flags.FINAL) != 0;
		boolean isNonNull = !findAnnotations(child, NON_NULL_PATTERN).isEmpty();
		if ((isFinal || isNonNull) && fieldDecl.init == null) fields.append(child);
	}
	return fields.toList();
}
 
Example 2
Source File: HandleConstructor.java    From EasyMPermission with MIT License 6 votes vote down vote up
public static List<JavacNode> findAllFields(JavacNode typeNode) {
	ListBuffer<JavacNode> fields = new ListBuffer<JavacNode>();
	for (JavacNode child : typeNode.down()) {
		if (child.getKind() != Kind.FIELD) continue;
		JCVariableDecl fieldDecl = (JCVariableDecl) child.get();
		//Skip fields that start with $
		if (fieldDecl.name.toString().startsWith("$")) continue;
		long fieldFlags = fieldDecl.mods.flags;
		//Skip static fields.
		if ((fieldFlags & Flags.STATIC) != 0) continue;
		//Skip initialized final fields
		boolean isFinal = (fieldFlags & Flags.FINAL) != 0;
		if (!isFinal || fieldDecl.init == null) fields.append(child);
	}
	return fields.toList();
}
 
Example 3
Source File: HandleGetter.java    From EasyMPermission with MIT License 6 votes vote down vote up
public void generateGetterForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean checkForTypeLevelGetter) {
	if (checkForTypeLevelGetter) {
		if (hasAnnotation(Getter.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)) != 0;
	
	if (typeDecl == null || notAClass) {
		errorNode.addError("@Getter is only supported on a class, an enum, or a field.");
		return;
	}
	
	for (JavacNode field : typeNode.down()) {
		if (fieldQualifiesForGetterGeneration(field)) generateGetterForField(field, errorNode.get(), level, false);
	}
}
 
Example 4
Source File: JavacHandlerUtil.java    From EasyMPermission with MIT License 6 votes vote down vote up
public static void sanityCheckForMethodGeneratingAnnotationsOnBuilderClass(JavacNode typeNode, JavacNode errorNode) {
	List<String> disallowed = List.nil();
	for (JavacNode child : typeNode.down()) {
		for (Class<? extends java.lang.annotation.Annotation> annType : INVALID_ON_BUILDERS) {
			if (annotationTypeMatches(annType, child)) {
				disallowed = disallowed.append(annType.getSimpleName());
			}
		}
	}
	
	int size = disallowed.size();
	if (size == 0) return;
	if (size == 1) {
		errorNode.addError("@" + disallowed.head + " is not allowed on builder classes.");
		return;
	}
	StringBuilder out = new StringBuilder();
	for (String a : disallowed) out.append("@").append(a).append(", ");
	out.setLength(out.length() - 2);
	errorNode.addError(out.append(" are not allowed on builder classes.").toString());
}
 
Example 5
Source File: JavacHandlerUtil.java    From EasyMPermission with MIT License 6 votes vote down vote up
private static boolean hasAnnotation(Class<? extends Annotation> type, JavacNode node, boolean delete) {
	if (node == null) return false;
	if (type == null) return false;
	switch (node.getKind()) {
	case ARGUMENT:
	case FIELD:
	case LOCAL:
	case TYPE:
	case METHOD:
		for (JavacNode child : node.down()) {
			if (annotationTypeMatches(type, child)) {
				if (delete) deleteAnnotationIfNeccessary(child, type);
				return true;
			}
		}
		// intentional fallthrough
	default:
		return false;
	}
}
 
Example 6
Source File: JavacHandlerUtil.java    From EasyMPermission with MIT License 6 votes vote down vote up
/**
 * Given a list of field names and a node referring to a type, finds each name in the list that does not match a field within the type.
 */
public static List<Integer> createListOfNonExistentFields(List<String> list, JavacNode type, boolean excludeStandard, boolean excludeTransient) {
	boolean[] matched = new boolean[list.size()];
	
	for (JavacNode child : type.down()) {
		if (list.isEmpty()) break;
		if (child.getKind() != Kind.FIELD) continue;
		JCVariableDecl field = (JCVariableDecl)child.get();
		if (excludeStandard) {
			if ((field.mods.flags & Flags.STATIC) != 0) continue;
			if (field.name.toString().startsWith("$")) continue;
		}
		if (excludeTransient && (field.mods.flags & Flags.TRANSIENT) != 0) continue;
		
		int idx = list.indexOf(child.getName());
		if (idx > -1) matched[idx] = true;
	}
	
	ListBuffer<Integer> problematic = new ListBuffer<Integer>();
	for (int i = 0 ; i < list.size() ; i++) {
		if (!matched[i]) problematic.append(i);
	}
	
	return problematic.toList();
}
 
Example 7
Source File: JavacHandlerUtil.java    From EasyMPermission with MIT License 6 votes vote down vote up
/**
 * Searches the given field node for annotations and returns each one that matches the provided regular expression pattern.
 * 
 * Only the simple name is checked - the package and any containing class are ignored.
 */
public static List<JCAnnotation> findAnnotations(JavacNode fieldNode, Pattern namePattern) {
	ListBuffer<JCAnnotation> result = new ListBuffer<JCAnnotation>();
	for (JavacNode child : fieldNode.down()) {
		if (child.getKind() == Kind.ANNOTATION) {
			JCAnnotation annotation = (JCAnnotation) child.get();
			String name = annotation.annotationType.toString();
			int idx = name.lastIndexOf(".");
			String suspect = idx == -1 ? name : name.substring(idx + 1);
			if (namePattern.matcher(suspect).matches()) {
				result.append(annotation);
			}
		}
	}	
	return result.toList();
}
 
Example 8
Source File: HandleBuilder.java    From EasyMPermission with MIT License 5 votes vote down vote up
public JavacNode findInnerClass(JavacNode parent, String name) {
	for (JavacNode child : parent.down()) {
		if (child.getKind() != Kind.TYPE) continue;
		JCClassDecl td = (JCClassDecl) child.get();
		if (td.name.contentEquals(name)) return child;
	}
	return null;
}
 
Example 9
Source File: HandleTable.java    From sqlitemagic with Apache License 2.0 5 votes vote down vote up
private static Set<String> findAllStaticMethodNames(JavacNode typeNode) {
  Set<String> methodNames = new LinkedHashSet<>();
  for (JavacNode child : typeNode.down()) {
    if (child.getKind() != AST.Kind.METHOD) continue;
    JCTree.JCMethodDecl methodDecl = (JCTree.JCMethodDecl) child.get();
    long methodFlags = methodDecl.mods.flags;
    //Take static methods
    if ((methodFlags & Flags.STATIC) != 0) {
      methodNames.add(child.getName());
    }
  }
  return methodNames;
}
 
Example 10
Source File: HandleBuilder.java    From EasyMPermission with MIT License 5 votes vote down vote up
private void makeSimpleSetterMethodForBuilder(JavacNode builderType, JavacNode fieldNode, JavacNode source, boolean fluent, boolean chain) {
	Name fieldName = ((JCVariableDecl) fieldNode.get()).name;
	
	for (JavacNode child : builderType.down()) {
		if (child.getKind() != Kind.METHOD) continue;
		Name existingName = ((JCMethodDecl) child.get()).name;
		if (existingName.equals(fieldName)) return;
	}
	
	String setterName = fluent ? fieldNode.getName() : HandlerUtil.buildAccessorName("set", fieldNode.getName());
	
	JavacTreeMaker maker = fieldNode.getTreeMaker();
	JCMethodDecl newMethod = HandleSetter.createSetter(Flags.PUBLIC, fieldNode, maker, setterName, chain, source, List.<JCAnnotation>nil(), List.<JCAnnotation>nil());
	injectMethod(builderType, newMethod);
}
 
Example 11
Source File: HandleBuilder.java    From EasyMPermission with MIT License 5 votes vote down vote up
public void generateBuilderFields(JavacNode builderType, java.util.List<BuilderFieldData> builderFields, JCTree source) {
	int len = builderFields.size();
	java.util.List<JavacNode> existing = new ArrayList<JavacNode>();
	for (JavacNode child : builderType.down()) {
		if (child.getKind() == Kind.FIELD) existing.add(child);
	}
	
	top:
	for (int i = len - 1; i >= 0; i--) {
		BuilderFieldData bfd = builderFields.get(i);
		if (bfd.singularData != null && bfd.singularData.getSingularizer() != null) {
			bfd.createdFields.addAll(bfd.singularData.getSingularizer().generateFields(bfd.singularData, builderType, source));
		} else {
			for (JavacNode exists : existing) {
				Name n = ((JCVariableDecl) exists.get()).name;
				if (n.equals(bfd.name)) {
					bfd.createdFields.add(exists);
					continue top;
				}
			}
			JavacTreeMaker maker = builderType.getTreeMaker();
			JCModifiers mods = maker.Modifiers(Flags.PRIVATE);
			JCVariableDecl newField = maker.VarDef(mods, bfd.name, cloneType(maker, bfd.type, source, builderType.getContext()), null);
			bfd.createdFields.add(injectFieldAndMarkGenerated(builderType, newField));
		}
	}
}
 
Example 12
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 13
Source File: JavacHandlerUtil.java    From EasyMPermission with MIT License 5 votes vote down vote up
static boolean lookForGetter(JavacNode field, FieldAccess fieldAccess) {
	if (fieldAccess == FieldAccess.GETTER) return true;
	if (fieldAccess == FieldAccess.ALWAYS_FIELD) return false;
	
	// If @Getter(lazy = true) is used, then using it is mandatory.
	for (JavacNode child : field.down()) {
		if (child.getKind() != Kind.ANNOTATION) continue;
		if (annotationTypeMatches(Getter.class, child)) {
			AnnotationValues<Getter> ann = createAnnotation(Getter.class, child);
			if (ann.getInstance().lazy()) return true;
		}
	}
	return false;
}
 
Example 14
Source File: JavacHandlerUtil.java    From EasyMPermission with MIT License 5 votes vote down vote up
/**
 * Returns if a field is marked deprecated, either by {@code @Deprecated} or in javadoc
 * @param field the field to check
 * @return {@code true} if a field is marked deprecated, either by {@code @Deprecated} or in javadoc, otherwise {@code false}
 */
public static boolean isFieldDeprecated(JavacNode field) {
	JCVariableDecl fieldNode = (JCVariableDecl) field.get();
	if ((fieldNode.mods.flags & Flags.DEPRECATED) != 0) {
		return true;
	}
	for (JavacNode child : field.down()) {
		if (annotationTypeMatches(Deprecated.class, child)) {
			return true;
		}
	}
	return false;
}
 
Example 15
Source File: HandleFieldDefaults.java    From EasyMPermission with MIT License 5 votes vote down vote up
public boolean generateFieldDefaultsForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean makeFinal, boolean checkForTypeLevelFieldDefaults) {
	if (checkForTypeLevelFieldDefaults) {
		if (hasAnnotation(FieldDefaults.class, typeNode)) {
			//The annotation will make it happen, so we can skip it.
			return true;
		}
	}
	
	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)) != 0;
	
	if (typeDecl == null || notAClass) {
		errorNode.addError("@FieldDefaults is only supported on a class or an enum.");
		return false;
	}
	
	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;
		
		setFieldDefaultsForField(field, errorNode.get(), level, makeFinal);
	}
	
	return true;
}
 
Example 16
Source File: HandleSetter.java    From EasyMPermission with MIT License 5 votes vote down vote up
public void generateSetterForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean checkForTypeLevelSetter) {
	if (checkForTypeLevelSetter) {
		if (hasAnnotation(Setter.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("@Setter 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 fields.
		if ((fieldDecl.mods.flags & Flags.FINAL) != 0) continue;
		
		generateSetterForField(field, errorNode, level);
	}
}
 
Example 17
Source File: HandleUtilityClass.java    From EasyMPermission with MIT License 5 votes vote down vote up
private void changeModifiersAndGenerateConstructor(JavacNode typeNode, JavacNode errorNode) {
	JCClassDecl classDecl = (JCClassDecl) typeNode.get();
	
	boolean makeConstructor = true;
	
	classDecl.mods.flags |= Flags.FINAL;
	
	boolean markStatic = true;
	
	if (typeNode.up().getKind() == Kind.COMPILATION_UNIT) markStatic = false;
	if (markStatic && typeNode.up().getKind() == Kind.TYPE) {
		JCClassDecl typeDecl = (JCClassDecl) typeNode.up().get();
		if ((typeDecl.mods.flags & Flags.INTERFACE) != 0) markStatic = false;
	}
	
	if (markStatic) classDecl.mods.flags |= Flags.STATIC;
	
	for (JavacNode element : typeNode.down()) {
		if (element.getKind() == Kind.FIELD) {
			JCVariableDecl fieldDecl = (JCVariableDecl) element.get();
			fieldDecl.mods.flags |= Flags.STATIC;
		} else if (element.getKind() == Kind.METHOD) {
			JCMethodDecl methodDecl = (JCMethodDecl) element.get();
			if (methodDecl.name.contentEquals("<init>")) {
				if (getGeneratedBy(methodDecl) == null && (methodDecl.mods.flags & Flags.GENERATEDCONSTR) == 0) {
					element.addError("@UtilityClasses cannot have declared constructors.");
					makeConstructor = false;
					continue;
				}
			}
			
			methodDecl.mods.flags |= Flags.STATIC;
		} else if (element.getKind() == Kind.TYPE) {
			JCClassDecl innerClassDecl = (JCClassDecl) element.get();
			innerClassDecl.mods.flags |= Flags.STATIC;
		}
	}
	
	if (makeConstructor) createPrivateDefaultConstructor(typeNode);
}
 
Example 18
Source File: HandleConstructor.java    From EasyMPermission with MIT License 5 votes vote down vote up
public void generateConstructor(JavacNode typeNode, AccessLevel level, List<JCAnnotation> onConstructor, List<JavacNode> fields, String staticName, SkipIfConstructorExists skipIfConstructorExists, Boolean suppressConstructorProperties, JavacNode source) {
	boolean staticConstrRequired = staticName != null && !staticName.equals("");
	
	if (skipIfConstructorExists != SkipIfConstructorExists.NO && constructorExists(typeNode) != MemberExistsResult.NOT_EXISTS) return;
	if (skipIfConstructorExists != SkipIfConstructorExists.NO) {
		for (JavacNode child : typeNode.down()) {
			if (child.getKind() == Kind.ANNOTATION) {
				boolean skipGeneration = annotationTypeMatches(NoArgsConstructor.class, child) ||
						annotationTypeMatches(AllArgsConstructor.class, child) ||
						annotationTypeMatches(RequiredArgsConstructor.class, child);
				
				if (!skipGeneration && skipIfConstructorExists == SkipIfConstructorExists.YES) {
					skipGeneration = annotationTypeMatches(Builder.class, child);
				}
				
				if (skipGeneration) {
					if (staticConstrRequired) {
						// @Data has asked us to generate a constructor, but we're going to skip this instruction, as an explicit 'make a constructor' annotation
						// will take care of it. However, @Data also wants a specific static name; this will be ignored; the appropriate way to do this is to use
						// the 'staticName' parameter of the @XArgsConstructor you've stuck on your type.
						// We should warn that we're ignoring @Data's 'staticConstructor' param.
						source.addWarning("Ignoring static constructor name: explicit @XxxArgsConstructor annotation present; its `staticName` parameter will be used.");
					}
					return;
				}
			}
		}
	}
	
	JCMethodDecl constr = createConstructor(staticConstrRequired ? AccessLevel.PRIVATE : level, onConstructor, typeNode, fields, suppressConstructorProperties, source);
	injectMethod(typeNode, constr);
	if (staticConstrRequired) {
		JCMethodDecl staticConstr = createStaticConstructor(staticName, level, typeNode, fields, source.get());
		injectMethod(typeNode, staticConstr);
	}
}
 
Example 19
Source File: HandleTable.java    From sqlitemagic with Apache License 2.0 5 votes vote down vote up
private Set<String> findAllMethodNames(JavacNode typeNode) {
  Set<String> methodNames = new LinkedHashSet<>();
  for (JavacNode child : typeNode.down()) {
    if (child.getKind() != AST.Kind.METHOD) continue;
    JCTree.JCMethodDecl methodDecl = (JCTree.JCMethodDecl) child.get();
    long methodFlags = methodDecl.mods.flags;
    //Skip static methods
    if ((methodFlags & Flags.STATIC) != 0) continue;
    methodNames.add(child.getName());
  }
  return methodNames;
}