aboutsummaryrefslogtreecommitdiff
path: root/src/lombok/eclipse
diff options
context:
space:
mode:
Diffstat (limited to 'src/lombok/eclipse')
-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
7 files changed, 99 insertions, 36 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)};