aboutsummaryrefslogtreecommitdiff
path: root/src/lombok
diff options
context:
space:
mode:
Diffstat (limited to 'src/lombok')
-rw-r--r--src/lombok/eclipse/handlers/EclipseHandlerUtil.java (renamed from src/lombok/eclipse/handlers/PKG.java)105
-rw-r--r--src/lombok/eclipse/handlers/HandleData.java8
-rw-r--r--src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java6
-rw-r--r--src/lombok/eclipse/handlers/HandleGetter.java4
-rw-r--r--src/lombok/eclipse/handlers/HandleSetter.java4
-rw-r--r--src/lombok/eclipse/handlers/HandleSynchronized.java4
-rw-r--r--src/lombok/eclipse/handlers/HandleToString.java4
-rw-r--r--src/lombok/javac/handlers/HandleData.java4
-rw-r--r--src/lombok/javac/handlers/HandleEqualsAndHashCode.java2
-rw-r--r--src/lombok/javac/handlers/HandleGetter.java2
-rw-r--r--src/lombok/javac/handlers/HandleSetter.java2
-rw-r--r--src/lombok/javac/handlers/HandleSneakyThrows.java2
-rw-r--r--src/lombok/javac/handlers/HandleSynchronized.java2
-rw-r--r--src/lombok/javac/handlers/HandleToString.java2
-rw-r--r--src/lombok/javac/handlers/JavacHandlerUtil.java (renamed from src/lombok/javac/handlers/PKG.java)76
15 files changed, 155 insertions, 72 deletions
diff --git a/src/lombok/eclipse/handlers/PKG.java b/src/lombok/eclipse/handlers/EclipseHandlerUtil.java
index 1d5c6ae1..88559436 100644
--- a/src/lombok/eclipse/handlers/PKG.java
+++ b/src/lombok/eclipse/handlers/EclipseHandlerUtil.java
@@ -23,7 +23,6 @@ package lombok.eclipse.handlers;
import static lombok.eclipse.Eclipse.fromQualifiedName;
-import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
@@ -56,30 +55,45 @@ import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
-class PKG {
- private PKG() {}
+/**
+ * Container for static utility methods useful to handlers written for eclipse.
+ */
+public class EclipseHandlerUtil {
+ private EclipseHandlerUtil() {
+ //Prevent instantiation
+ }
- static boolean isPrimitive(TypeReference ref) {
+ /**
+ * Checks if the given type reference represents a primitive type.
+ */
+ public static boolean isPrimitive(TypeReference ref) {
if (ref.dimensions() > 0) return false;
return TransformationsUtil.PRIMITIVE_TYPE_NAME_PATTERN.matcher(Eclipse.toQualifiedName(ref.getTypeName())).matches();
}
- static int toModifier(AccessLevel value) {
+ /**
+ * Turns an {@code AccessLevel} instance into the flag bit used by eclipse.
+ */
+ public static int toEclipseModifier(AccessLevel value) {
switch (value) {
case MODULE:
case PACKAGE:
return 0;
default:
case PUBLIC:
- return Modifier.PUBLIC;
+ return ClassFileConstants.AccPublic;
case PROTECTED:
- return Modifier.PROTECTED;
+ return ClassFileConstants.AccProtected;
case PRIVATE:
- return Modifier.PRIVATE;
+ return ClassFileConstants.AccPrivate;
}
}
- static boolean nameEquals(char[][] typeName, String string) {
+ /**
+ * Checks if an eclipse-style array-of-array-of-characters to represent a fully qualified name ('foo.bar.baz'), matches a plain
+ * string containing the same fully qualified name with dots in the string.
+ */
+ public static boolean nameEquals(char[][] typeName, String string) {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (char[] elem : typeName) {
@@ -91,11 +105,18 @@ class PKG {
return string.contentEquals(sb);
}
- enum MemberExistsResult {
+ /** Serves as return value for the methods that check for the existence of fields and methods. */
+ public enum MemberExistsResult {
NOT_EXISTS, EXISTS_BY_USER, EXISTS_BY_LOMBOK;
}
- static MemberExistsResult fieldExists(String fieldName, EclipseNode node) {
+ /**
+ * Checks if there is a field with the provided name.
+ *
+ * @param fieldName the field name to check for.
+ * @param node Any node that represents the Type (TypeDeclaration) to look in, or any child node thereof.
+ */
+ public static MemberExistsResult fieldExists(String fieldName, EclipseNode node) {
while (node != null && !(node.get() instanceof TypeDeclaration)) {
node = node.up();
}
@@ -116,7 +137,14 @@ class PKG {
return MemberExistsResult.NOT_EXISTS;
}
- static MemberExistsResult methodExists(String methodName, EclipseNode node) {
+ /**
+ * Checks if there is a method with the provided name. In case of multiple methods (overloading), only
+ * the first method decides if EXISTS_BY_USER or EXISTS_BY_LOMBOK is returned.
+ *
+ * @param methodName the method name to check for.
+ * @param node Any node that represents the Type (TypeDeclaration) to look in, or any child node thereof.
+ */
+ public static MemberExistsResult methodExists(String methodName, EclipseNode node) {
while (node != null && !(node.get() instanceof TypeDeclaration)) {
node = node.up();
}
@@ -137,7 +165,13 @@ class PKG {
return MemberExistsResult.NOT_EXISTS;
}
- static MemberExistsResult constructorExists(EclipseNode node) {
+ /**
+ * Checks if there is a (non-default) constructor. In case of multiple constructors (overloading), only
+ * the first constructor decides if EXISTS_BY_USER or EXISTS_BY_LOMBOK is returned.
+ *
+ * @param node Any node that represents the Type (TypeDeclaration) to look in, or any child node thereof.
+ */
+ public static MemberExistsResult constructorExists(EclipseNode node) {
while (node != null && !(node.get() instanceof TypeDeclaration)) {
node = node.up();
}
@@ -157,7 +191,11 @@ class PKG {
return MemberExistsResult.NOT_EXISTS;
}
- static EclipseNode getExistingLombokConstructor(EclipseNode node) {
+ /**
+ * Returns the constructor that's already been generated by lombok.
+ * Provide any node that represents the type (TypeDeclaration) to look in, or any child node thereof.
+ */
+ public static EclipseNode getExistingLombokConstructor(EclipseNode node) {
while (node != null && !(node.get() instanceof TypeDeclaration)) {
node = node.up();
}
@@ -177,7 +215,11 @@ class PKG {
return null;
}
- static EclipseNode getExistingLombokMethod(String methodName, EclipseNode node) {
+ /**
+ * Returns the method that's already been generated by lombok with the given name.
+ * Provide any node that represents the type (TypeDeclaration) to look in, or any child node thereof.
+ */
+ public static EclipseNode getExistingLombokMethod(String methodName, EclipseNode node) {
while (node != null && !(node.get() instanceof TypeDeclaration)) {
node = node.up();
}
@@ -198,7 +240,10 @@ class PKG {
return null;
}
- static void injectField(EclipseNode type, FieldDeclaration field) {
+ /**
+ * Inserts a field into an existing type. The type must represent a {@code TypeDeclaration}.
+ */
+ public static void injectField(EclipseNode type, FieldDeclaration field) {
TypeDeclaration parent = (TypeDeclaration) type.get();
if (parent.fields == null) {
@@ -214,7 +259,10 @@ class PKG {
type.add(field, Kind.FIELD).recursiveSetHandled();
}
- static void injectMethod(EclipseNode type, AbstractMethodDeclaration method) {
+ /**
+ * Inserts a method into an existing type. The type must represent a {@code TypeDeclaration}.
+ */
+ public static void injectMethod(EclipseNode type, AbstractMethodDeclaration method) {
TypeDeclaration parent = (TypeDeclaration) type.get();
if (parent.methods == null) {
@@ -245,7 +293,12 @@ class PKG {
type.add(method, Kind.METHOD).recursiveSetHandled();
}
- static Annotation[] findAnnotations(FieldDeclaration field, Pattern namePattern) {
+ /**
+ * 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 Annotation[] findAnnotations(FieldDeclaration field, Pattern namePattern) {
List<Annotation> result = new ArrayList<Annotation>();
if (field.annotations == null) return new Annotation[0];
for (Annotation annotation : field.annotations) {
@@ -261,7 +314,11 @@ class PKG {
return result.toArray(new Annotation[0]);
}
- static Statement generateNullCheck(AbstractVariableDeclaration variable, ASTNode source) {
+ /**
+ * Generates a new statement that checks if the given variable is null, and if so, throws a {@code NullPointerException} with the
+ * variable name as message.
+ */
+ public static Statement generateNullCheck(AbstractVariableDeclaration variable, ASTNode source) {
int pS = source.sourceStart, pE = source.sourceEnd;
long p = (long)pS << 32 | pE;
@@ -287,7 +344,10 @@ class PKG {
return ifStatement;
}
- static MarkerAnnotation makeMarkerAnnotation(char[][] name, ASTNode source) {
+ /**
+ * Create an annotation of the given name, and is marked as being generated by the given source.
+ */
+ public static MarkerAnnotation makeMarkerAnnotation(char[][] name, ASTNode source) {
long pos = source.sourceStart << 32 | source.sourceEnd;
TypeReference typeRef = new QualifiedTypeReference(name, new long[] {pos, pos, pos});
Eclipse.setGeneratedBy(typeRef, source);
@@ -297,7 +357,10 @@ class PKG {
return ann;
}
- static List<Integer> createListOfNonExistentFields(List<String> list, EclipseNode type, boolean excludeStandard, boolean excludeTransient) {
+ /**
+ * 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, EclipseNode type, boolean excludeStandard, boolean excludeTransient) {
boolean[] matched = new boolean[list.size()];
for (EclipseNode child : type.down()) {
diff --git a/src/lombok/eclipse/handlers/HandleData.java b/src/lombok/eclipse/handlers/HandleData.java
index 22538a53..8c4e07ce 100644
--- a/src/lombok/eclipse/handlers/HandleData.java
+++ b/src/lombok/eclipse/handlers/HandleData.java
@@ -22,7 +22,7 @@
package lombok.eclipse.handlers;
import static lombok.eclipse.Eclipse.*;
-import static lombok.eclipse.handlers.PKG.*;
+import static lombok.eclipse.handlers.EclipseHandlerUtil.*;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
@@ -37,7 +37,7 @@ import lombok.core.AST.Kind;
import lombok.eclipse.Eclipse;
import lombok.eclipse.EclipseAnnotationHandler;
import lombok.eclipse.EclipseNode;
-import lombok.eclipse.handlers.PKG.MemberExistsResult;
+import lombok.eclipse.handlers.EclipseHandlerUtil.MemberExistsResult;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.AllocationExpression;
@@ -132,7 +132,7 @@ public class HandleData implements EclipseAnnotationHandler<Data> {
((CompilationUnitDeclaration) type.top().get()).compilationResult);
Eclipse.setGeneratedBy(constructor, source);
- constructor.modifiers = PKG.toModifier(isPublic ? AccessLevel.PUBLIC : AccessLevel.PRIVATE);
+ constructor.modifiers = EclipseHandlerUtil.toEclipseModifier(isPublic ? AccessLevel.PUBLIC : AccessLevel.PRIVATE);
constructor.annotations = null;
constructor.selector = ((TypeDeclaration)type.get()).name;
constructor.constructorCall = new ExplicitConstructorCall(ExplicitConstructorCall.ImplicitSuper);
@@ -189,7 +189,7 @@ public class HandleData implements EclipseAnnotationHandler<Data> {
((CompilationUnitDeclaration) type.top().get()).compilationResult);
Eclipse.setGeneratedBy(constructor, source);
- constructor.modifiers = PKG.toModifier(AccessLevel.PUBLIC) | Modifier.STATIC;
+ constructor.modifiers = EclipseHandlerUtil.toEclipseModifier(AccessLevel.PUBLIC) | Modifier.STATIC;
TypeDeclaration typeDecl = (TypeDeclaration) type.get();
if (typeDecl.typeParameters != null && typeDecl.typeParameters.length > 0) {
TypeReference[] refs = new TypeReference[typeDecl.typeParameters.length];
diff --git a/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java
index c53938d1..7c0980c8 100644
--- a/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java
+++ b/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java
@@ -21,7 +21,7 @@
*/
package lombok.eclipse.handlers;
-import static lombok.eclipse.handlers.PKG.*;
+import static lombok.eclipse.handlers.EclipseHandlerUtil.*;
import static lombok.eclipse.Eclipse.copyTypes;
@@ -245,7 +245,7 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA
((CompilationUnitDeclaration) type.top().get()).compilationResult);
Eclipse.setGeneratedBy(method, source);
- method.modifiers = PKG.toModifier(AccessLevel.PUBLIC);
+ method.modifiers = EclipseHandlerUtil.toEclipseModifier(AccessLevel.PUBLIC);
method.returnType = TypeReference.baseTypeReference(TypeIds.T_int, 0);
Eclipse.setGeneratedBy(method.returnType, source);
method.annotations = new Annotation[] {makeMarkerAnnotation(TypeConstants.JAVA_LANG_OVERRIDE, source)};
@@ -425,7 +425,7 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA
MethodDeclaration method = new MethodDeclaration(
((CompilationUnitDeclaration) type.top().get()).compilationResult);
Eclipse.setGeneratedBy(method, source);
- method.modifiers = PKG.toModifier(AccessLevel.PUBLIC);
+ method.modifiers = EclipseHandlerUtil.toEclipseModifier(AccessLevel.PUBLIC);
method.returnType = TypeReference.baseTypeReference(TypeIds.T_boolean, 0);
method.returnType.sourceStart = pS; method.returnType.sourceEnd = pE;
Eclipse.setGeneratedBy(method.returnType, source);
diff --git a/src/lombok/eclipse/handlers/HandleGetter.java b/src/lombok/eclipse/handlers/HandleGetter.java
index 86ab4c88..7bff701e 100644
--- a/src/lombok/eclipse/handlers/HandleGetter.java
+++ b/src/lombok/eclipse/handlers/HandleGetter.java
@@ -22,7 +22,7 @@
package lombok.eclipse.handlers;
import static lombok.eclipse.Eclipse.*;
-import static lombok.eclipse.handlers.PKG.*;
+import static lombok.eclipse.handlers.EclipseHandlerUtil.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.core.AnnotationValues;
@@ -96,7 +96,7 @@ public class HandleGetter implements EclipseAnnotationHandler<Getter> {
boolean isBoolean = nameEquals(fieldType.getTypeName(), "boolean") && fieldType.dimensions() == 0;
String getterName = TransformationsUtil.toGetterName(fieldName, isBoolean);
- int modifier = toModifier(level) | (field.modifiers & ClassFileConstants.AccStatic);
+ int modifier = toEclipseModifier(level) | (field.modifiers & ClassFileConstants.AccStatic);
for (String altName : TransformationsUtil.toAllGetterNames(fieldName, isBoolean)) {
switch (methodExists(altName, fieldNode)) {
diff --git a/src/lombok/eclipse/handlers/HandleSetter.java b/src/lombok/eclipse/handlers/HandleSetter.java
index 2f342992..9bd10af3 100644
--- a/src/lombok/eclipse/handlers/HandleSetter.java
+++ b/src/lombok/eclipse/handlers/HandleSetter.java
@@ -22,7 +22,7 @@
package lombok.eclipse.handlers;
import static lombok.eclipse.Eclipse.*;
-import static lombok.eclipse.handlers.PKG.*;
+import static lombok.eclipse.handlers.EclipseHandlerUtil.*;
import java.lang.reflect.Modifier;
@@ -101,7 +101,7 @@ public class HandleSetter implements EclipseAnnotationHandler<Setter> {
FieldDeclaration field = (FieldDeclaration) fieldNode.get();
String setterName = TransformationsUtil.toSetterName(new String(field.name));
- int modifier = toModifier(level) | (field.modifiers & ClassFileConstants.AccStatic);
+ int modifier = toEclipseModifier(level) | (field.modifiers & ClassFileConstants.AccStatic);
switch (methodExists(setterName, fieldNode)) {
case EXISTS_BY_LOMBOK:
diff --git a/src/lombok/eclipse/handlers/HandleSynchronized.java b/src/lombok/eclipse/handlers/HandleSynchronized.java
index 0666ace7..fde36192 100644
--- a/src/lombok/eclipse/handlers/HandleSynchronized.java
+++ b/src/lombok/eclipse/handlers/HandleSynchronized.java
@@ -21,7 +21,7 @@
*/
package lombok.eclipse.handlers;
-import static lombok.eclipse.handlers.PKG.*;
+import static lombok.eclipse.handlers.EclipseHandlerUtil.*;
import java.lang.reflect.Modifier;
@@ -31,7 +31,7 @@ import lombok.core.AST.Kind;
import lombok.eclipse.Eclipse;
import lombok.eclipse.EclipseAnnotationHandler;
import lombok.eclipse.EclipseNode;
-import lombok.eclipse.handlers.PKG.MemberExistsResult;
+import lombok.eclipse.handlers.EclipseHandlerUtil.MemberExistsResult;
import org.eclipse.jdt.internal.compiler.ast.Annotation;
import org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression;
diff --git a/src/lombok/eclipse/handlers/HandleToString.java b/src/lombok/eclipse/handlers/HandleToString.java
index b8bbb064..d5a4c398 100644
--- a/src/lombok/eclipse/handlers/HandleToString.java
+++ b/src/lombok/eclipse/handlers/HandleToString.java
@@ -21,7 +21,7 @@
*/
package lombok.eclipse.handlers;
-import static lombok.eclipse.handlers.PKG.*;
+import static lombok.eclipse.handlers.EclipseHandlerUtil.*;
import java.util.ArrayList;
import java.util.Arrays;
@@ -274,7 +274,7 @@ public class HandleToString implements EclipseAnnotationHandler<ToString> {
MethodDeclaration method = new MethodDeclaration(((CompilationUnitDeclaration) type.top().get()).compilationResult);
Eclipse.setGeneratedBy(method, source);
- method.modifiers = toModifier(AccessLevel.PUBLIC);
+ method.modifiers = toEclipseModifier(AccessLevel.PUBLIC);
method.returnType = new QualifiedTypeReference(TypeConstants.JAVA_LANG_STRING, new long[] {p, p, p});
Eclipse.setGeneratedBy(method.returnType, source);
method.annotations = new Annotation[] {makeMarkerAnnotation(TypeConstants.JAVA_LANG_OVERRIDE, source)};
diff --git a/src/lombok/javac/handlers/HandleData.java b/src/lombok/javac/handlers/HandleData.java
index 8d80808f..f1b395e1 100644
--- a/src/lombok/javac/handlers/HandleData.java
+++ b/src/lombok/javac/handlers/HandleData.java
@@ -21,7 +21,7 @@
*/
package lombok.javac.handlers;
-import static lombok.javac.handlers.PKG.*;
+import static lombok.javac.handlers.JavacHandlerUtil.*;
import java.lang.reflect.Modifier;
@@ -31,7 +31,7 @@ import lombok.core.TransformationsUtil;
import lombok.core.AST.Kind;
import lombok.javac.JavacAnnotationHandler;
import lombok.javac.JavacNode;
-import lombok.javac.handlers.PKG.MemberExistsResult;
+import lombok.javac.handlers.JavacHandlerUtil.MemberExistsResult;
import org.mangosdk.spi.ProviderFor;
diff --git a/src/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/lombok/javac/handlers/HandleEqualsAndHashCode.java
index f1eae2a7..61a4ef63 100644
--- a/src/lombok/javac/handlers/HandleEqualsAndHashCode.java
+++ b/src/lombok/javac/handlers/HandleEqualsAndHashCode.java
@@ -21,7 +21,7 @@
*/
package lombok.javac.handlers;
-import static lombok.javac.handlers.PKG.*;
+import static lombok.javac.handlers.JavacHandlerUtil.*;
import lombok.EqualsAndHashCode;
import lombok.core.AnnotationValues;
import lombok.core.AST.Kind;
diff --git a/src/lombok/javac/handlers/HandleGetter.java b/src/lombok/javac/handlers/HandleGetter.java
index 064454d9..e60e426d 100644
--- a/src/lombok/javac/handlers/HandleGetter.java
+++ b/src/lombok/javac/handlers/HandleGetter.java
@@ -21,7 +21,7 @@
*/
package lombok.javac.handlers;
-import static lombok.javac.handlers.PKG.*;
+import static lombok.javac.handlers.JavacHandlerUtil.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.core.AnnotationValues;
diff --git a/src/lombok/javac/handlers/HandleSetter.java b/src/lombok/javac/handlers/HandleSetter.java
index 33bc34a9..84032e9c 100644
--- a/src/lombok/javac/handlers/HandleSetter.java
+++ b/src/lombok/javac/handlers/HandleSetter.java
@@ -21,7 +21,7 @@
*/
package lombok.javac.handlers;
-import static lombok.javac.handlers.PKG.*;
+import static lombok.javac.handlers.JavacHandlerUtil.*;
import lombok.AccessLevel;
import lombok.Setter;
import lombok.core.AnnotationValues;
diff --git a/src/lombok/javac/handlers/HandleSneakyThrows.java b/src/lombok/javac/handlers/HandleSneakyThrows.java
index 167737f9..e7879dd1 100644
--- a/src/lombok/javac/handlers/HandleSneakyThrows.java
+++ b/src/lombok/javac/handlers/HandleSneakyThrows.java
@@ -21,7 +21,7 @@
*/
package lombok.javac.handlers;
-import static lombok.javac.handlers.PKG.chainDots;
+import static lombok.javac.handlers.JavacHandlerUtil.chainDots;
import java.util.ArrayList;
import java.util.Collection;
diff --git a/src/lombok/javac/handlers/HandleSynchronized.java b/src/lombok/javac/handlers/HandleSynchronized.java
index 4573deda..b607ea72 100644
--- a/src/lombok/javac/handlers/HandleSynchronized.java
+++ b/src/lombok/javac/handlers/HandleSynchronized.java
@@ -21,7 +21,7 @@
*/
package lombok.javac.handlers;
-import static lombok.javac.handlers.PKG.*;
+import static lombok.javac.handlers.JavacHandlerUtil.*;
import org.mangosdk.spi.ProviderFor;
diff --git a/src/lombok/javac/handlers/HandleToString.java b/src/lombok/javac/handlers/HandleToString.java
index d1c98525..f7251ab8 100644
--- a/src/lombok/javac/handlers/HandleToString.java
+++ b/src/lombok/javac/handlers/HandleToString.java
@@ -21,7 +21,7 @@
*/
package lombok.javac.handlers;
-import static lombok.javac.handlers.PKG.*;
+import static lombok.javac.handlers.JavacHandlerUtil.*;
import lombok.ToString;
import lombok.core.AnnotationValues;
diff --git a/src/lombok/javac/handlers/PKG.java b/src/lombok/javac/handlers/JavacHandlerUtil.java
index d6fd1c61..4a0a6031 100644
--- a/src/lombok/javac/handlers/PKG.java
+++ b/src/lombok/javac/handlers/JavacHandlerUtil.java
@@ -21,7 +21,6 @@
*/
package lombok.javac.handlers;
-import java.lang.reflect.Modifier;
import java.util.regex.Pattern;
import lombok.AccessLevel;
@@ -43,19 +42,26 @@ import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.Name;
/**
- * Container for static utility methods relevant to this package.
+ * Container for static utility methods useful to handlers written for javac.
*/
-class PKG {
- private PKG() {
+public class JavacHandlerUtil {
+ private JavacHandlerUtil() {
//Prevent instantiation
}
- static boolean isPrimitive(JCExpression ref) {
+ /**
+ * Checks if the given expression (that really ought to refer to a type expression) represents a primitive type.
+ */
+ public static boolean isPrimitive(JCExpression ref) {
String typeName = ref.toString();
return TransformationsUtil.PRIMITIVE_TYPE_NAME_PATTERN.matcher(typeName).matches();
}
- static java.util.List<String> toAllGetterNames(JCVariableDecl field) {
+ /**
+ * Translates the given field into all possible getter names.
+ * Convenient wrapper around {@link TransformationsUtil#toAllGetterNames(String, boolean)}.
+ */
+ public static java.util.List<String> toAllGetterNames(JCVariableDecl field) {
CharSequence fieldName = field.name;
boolean isBoolean = field.vartype.toString().equals("boolean");
@@ -65,8 +71,10 @@ class PKG {
/**
* @return the likely getter name for the stated field. (e.g. private boolean foo; to isFoo).
+ *
+ * Convenient wrapper around {@link TransformationsUtil#toGetterName(String, boolean)}.
*/
- static String toGetterName(JCVariableDecl field) {
+ public static String toGetterName(JCVariableDecl field) {
CharSequence fieldName = field.name;
boolean isBoolean = field.vartype.toString().equals("boolean");
@@ -76,15 +84,17 @@ class PKG {
/**
* @return the likely setter name for the stated field. (e.g. private boolean foo; to setFoo).
+ *
+ * Convenient wrapper around {@link TransformationsUtil.toSetterName(String)}.
*/
- static String toSetterName(JCVariableDecl field) {
+ public static String toSetterName(JCVariableDecl field) {
CharSequence fieldName = field.name;
return TransformationsUtil.toSetterName(fieldName);
}
/** Serves as return value for the methods that check for the existence of fields and methods. */
- enum MemberExistsResult {
+ public enum MemberExistsResult {
NOT_EXISTS, EXISTS_BY_USER, EXISTS_BY_LOMBOK;
}
@@ -92,9 +102,9 @@ class PKG {
* Checks if there is a field with the provided name.
*
* @param fieldName the field name to check for.
- * @param node Any node that represents the Type (JCClassDecl) to check for, or any child node thereof.
+ * @param node Any node that represents the Type (JCClassDecl) to look in, or any child node thereof.
*/
- static MemberExistsResult fieldExists(String fieldName, JavacNode node) {
+ public static MemberExistsResult fieldExists(String fieldName, JavacNode node) {
while (node != null && !(node.get() instanceof JCClassDecl)) {
node = node.up();
}
@@ -119,9 +129,9 @@ class PKG {
* the first method decides if EXISTS_BY_USER or EXISTS_BY_LOMBOK is returned.
*
* @param methodName the method name to check for.
- * @param node Any node that represents the Type (JCClassDecl) to check for, or any child node thereof.
+ * @param node Any node that represents the Type (JCClassDecl) to look in, or any child node thereof.
*/
- static MemberExistsResult methodExists(String methodName, JavacNode node) {
+ public static MemberExistsResult methodExists(String methodName, JavacNode node) {
while (node != null && !(node.get() instanceof JCClassDecl)) {
node = node.up();
}
@@ -145,9 +155,9 @@ class PKG {
* Checks if there is a (non-default) constructor. In case of multiple constructors (overloading), only
* the first constructor decides if EXISTS_BY_USER or EXISTS_BY_LOMBOK is returned.
*
- * @param node Any node that represents the Type (JCClassDecl) to check for, or any child node thereof.
+ * @param node Any node that represents the Type (JCClassDecl) to look in, or any child node thereof.
*/
- static MemberExistsResult constructorExists(JavacNode node) {
+ public static MemberExistsResult constructorExists(JavacNode node) {
while (node != null && !(node.get() instanceof JCClassDecl)) {
node = node.up();
}
@@ -170,21 +180,19 @@ class PKG {
/**
* Turns an {@code AccessLevel} instance into the flag bit used by javac.
- *
- * @see java.lang.Modifier
*/
- static int toJavacModifier(AccessLevel accessLevel) {
+ public static int toJavacModifier(AccessLevel accessLevel) {
switch (accessLevel) {
case MODULE:
case PACKAGE:
return 0;
default:
case PUBLIC:
- return Modifier.PUBLIC;
+ return Flags.PUBLIC;
case PRIVATE:
- return Modifier.PRIVATE;
+ return Flags.PRIVATE;
case PROTECTED:
- return Modifier.PROTECTED;
+ return Flags.PROTECTED;
}
}
@@ -193,7 +201,7 @@ class PKG {
*
* Also takes care of updating the JavacAST.
*/
- static void injectField(JavacNode typeNode, JCVariableDecl field) {
+ public static void injectField(JavacNode typeNode, JCVariableDecl field) {
JCClassDecl type = (JCClassDecl) typeNode.get();
type.defs = type.defs.append(field);
@@ -207,7 +215,7 @@ class PKG {
*
* Also takes care of updating the JavacAST.
*/
- static void injectMethod(JavacNode typeNode, JCMethodDecl method) {
+ public static void injectMethod(JavacNode typeNode, JCMethodDecl method) {
JCClassDecl type = (JCClassDecl) typeNode.get();
if (method.getName().contentEquals("<init>")) {
@@ -250,7 +258,7 @@ class PKG {
* @see com.sun.tools.javac.tree.JCTree.JCIdent
* @see com.sun.tools.javac.tree.JCTree.JCFieldAccess
*/
- static JCExpression chainDots(TreeMaker maker, JavacNode node, String... elems) {
+ public static JCExpression chainDots(TreeMaker maker, JavacNode node, String... elems) {
assert elems != null;
assert elems.length > 0;
@@ -262,7 +270,12 @@ class PKG {
return e;
}
- static List<JCAnnotation> findAnnotations(JavacNode fieldNode, Pattern namePattern) {
+ /**
+ * 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) {
List<JCAnnotation> result = List.nil();
for (JavacNode child : fieldNode.down()) {
if (child.getKind() == Kind.ANNOTATION) {
@@ -276,9 +289,13 @@ class PKG {
}
}
return result;
- }
+ }
- static JCStatement generateNullCheck(TreeMaker treeMaker, JavacNode variable) {
+ /**
+ * Generates a new statement that checks if the given variable is null, and if so, throws a {@code NullPointerException} with the
+ * variable name as message.
+ */
+ public static JCStatement generateNullCheck(TreeMaker treeMaker, JavacNode variable) {
JCVariableDecl varDecl = (JCVariableDecl) variable.get();
if (isPrimitive(varDecl.vartype)) return null;
Name fieldName = varDecl.name;
@@ -288,7 +305,10 @@ class PKG {
return treeMaker.If(treeMaker.Binary(JCTree.EQ, treeMaker.Ident(fieldName), treeMaker.Literal(TypeTags.BOT, null)), throwStatement, null);
}
- static List<Integer> createListOfNonExistentFields(List<String> list, JavacNode type, boolean excludeStandard, boolean excludeTransient) {
+ /**
+ * 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()) {