From cbaa53a9b54589f05a580d63dfc065be03bb4b88 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Wed, 21 Mar 2012 11:41:59 +0100 Subject: Implementation of @Accessors. --- src/core/lombok/javac/handlers/HandleGetter.java | 11 ++- src/core/lombok/javac/handlers/HandleSetter.java | 19 +++-- .../lombok/javac/handlers/JavacHandlerUtil.java | 87 +++++++++++++++++++++- 3 files changed, 106 insertions(+), 11 deletions(-) (limited to 'src/core/lombok/javac/handlers') diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java index c286ed24..e49b0ccb 100644 --- a/src/core/lombok/javac/handlers/HandleGetter.java +++ b/src/core/lombok/javac/handlers/HandleGetter.java @@ -190,9 +190,14 @@ public class HandleGetter extends JavacAnnotationHandler { } } - String methodName = toGetterName(fieldDecl); + String methodName = toGetterName(fieldNode); - for (String altName : toAllGetterNames(fieldDecl)) { + if (methodName == null) { + source.addWarning("Not generating getter for this field: It does not fit your @Accessors prefix list."); + return; + } + + for (String altName : toAllGetterNames(fieldNode)) { switch (methodExists(altName, fieldNode, false)) { case EXISTS_BY_LOMBOK: return; @@ -231,7 +236,7 @@ public class HandleGetter extends JavacAnnotationHandler { } JCBlock methodBody = treeMaker.Block(0, statements); - Name methodName = field.toName(toGetterName(fieldNode)); + Name methodName = field.toName(toGetterName(field)); List methodGenericParams = List.nil(); List parameters = List.nil(); diff --git a/src/core/lombok/javac/handlers/HandleSetter.java b/src/core/lombok/javac/handlers/HandleSetter.java index db1fa724..93ea5a5b 100644 --- a/src/core/lombok/javac/handlers/HandleSetter.java +++ b/src/core/lombok/javac/handlers/HandleSetter.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2011 The Project Lombok Authors. + * Copyright (C) 2009-2012 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -162,9 +162,14 @@ public class HandleSetter extends JavacAnnotationHandler { } JCVariableDecl fieldDecl = (JCVariableDecl)fieldNode.get(); - String methodName = toSetterName(fieldDecl); + String methodName = toSetterName(fieldNode); - for (String altName : toAllSetterNames(fieldDecl)) { + if (methodName == null) { + source.addWarning("Not generating setter for this field: It does not fit your @Accessors prefix list."); + return; + } + + for (String altName : toAllSetterNames(fieldNode)) { switch (methodExists(altName, fieldNode, false)) { case EXISTS_BY_LOMBOK: return; @@ -184,10 +189,14 @@ public class HandleSetter extends JavacAnnotationHandler { long access = toJavacModifier(level) | (fieldDecl.mods.flags & Flags.STATIC); - injectMethod(fieldNode.up(), createSetter(access, fieldNode, fieldNode.getTreeMaker(), source.get())); + JCMethodDecl createdSetter = createSetter(access, fieldNode, fieldNode.getTreeMaker(), source.get()); + injectMethod(fieldNode.up(), createdSetter); } private JCMethodDecl createSetter(long access, JavacNode field, TreeMaker treeMaker, JCTree source) { + String setterName = toSetterName(field); + if (setterName == null) return null; + JCVariableDecl fieldDecl = (JCVariableDecl) field.get(); JCExpression fieldRef = createFieldAccessor(treeMaker, field, FieldAccess.ALWAYS_FIELD); @@ -206,7 +215,7 @@ public class HandleSetter extends JavacAnnotationHandler { } JCBlock methodBody = treeMaker.Block(0, statements); - Name methodName = field.toName(toSetterName(fieldDecl)); + Name methodName = field.toName(setterName); List annsOnParam = nonNulls.appendList(nullables); JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(Flags.FINAL, annsOnParam), fieldDecl.name, fieldDecl.vartype, null); //WARNING: Do not use field.getSymbolTable().voidType - that field has gone through non-backwards compatible API changes within javac1.6. diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index 244f7d38..245636b7 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2011 The Project Lombok Authors. + * Copyright (C) 2009-2012 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -37,9 +37,11 @@ import lombok.AccessLevel; import lombok.Data; import lombok.Getter; import lombok.core.AnnotationValues; +import lombok.core.TransformationsUtil; import lombok.core.TypeResolver; import lombok.core.AST.Kind; import lombok.core.AnnotationValues.AnnotationValue; +import lombok.experimental.Accessors; import lombok.javac.Javac; import lombok.javac.JavacNode; @@ -271,6 +273,84 @@ public class JavacHandlerUtil { NOT_EXISTS, EXISTS_BY_LOMBOK, EXISTS_BY_USER; } + /** + * Translates the given field into all possible getter names. + * Convenient wrapper around {@link TransformationsUtil#toAllGetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}. + */ + public static java.util.List toAllGetterNames(JavacNode field) { + String fieldName = field.getName(); + + boolean isBoolean = ((JCVariableDecl) field.get()).vartype.toString().equals("boolean"); + + AnnotationValues accessors = JavacHandlerUtil.getAccessorsForField(field); + + return TransformationsUtil.toAllGetterNames(accessors, fieldName, isBoolean); + } + + /** + * @return the likely getter name for the stated field. (e.g. private boolean foo; to isFoo). + * + * Convenient wrapper around {@link TransformationsUtil#toGetterName(lombok.core.AnnotationValues, CharSequence, boolean)}. + */ + public static String toGetterName(JavacNode field) { + String fieldName = field.getName(); + + boolean isBoolean = ((JCVariableDecl) field.get()).vartype.toString().equals("boolean"); + + AnnotationValues accessors = JavacHandlerUtil.getAccessorsForField(field); + + return TransformationsUtil.toGetterName(accessors, fieldName, isBoolean); + } + + /** + * Translates the given field into all possible setter names. + * Convenient wrapper around {@link TransformationsUtil#toAllSetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}. + */ + public static java.util.List toAllSetterNames(JavacNode field) { + String fieldName = field.getName(); + + boolean isBoolean = ((JCVariableDecl) field.get()).vartype.toString().equals("boolean"); + + AnnotationValues accessors = JavacHandlerUtil.getAccessorsForField(field); + + return TransformationsUtil.toAllSetterNames(accessors, fieldName, isBoolean); + } + + /** + * @return the likely setter name for the stated field. (e.g. private boolean foo; to setFoo). + * + * Convenient wrapper around {@link TransformationsUtil#toSetterName(lombok.core.AnnotationValues, CharSequence, boolean)}. + */ + public static String toSetterName(JavacNode field) { + String fieldName = field.getName(); + + boolean isBoolean = ((JCVariableDecl) field.get()).toString().equals("boolean"); + + AnnotationValues accessors = JavacHandlerUtil.getAccessorsForField(field); + + return TransformationsUtil.toSetterName(accessors, fieldName, isBoolean); + } + + public static AnnotationValues getAccessorsForField(JavacNode field) { + for (JavacNode node : field.down()) { + if (annotationTypeMatches(Accessors.class, node)) { + return createAnnotation(Accessors.class, node); + } + } + + JavacNode current = field.up(); + while (current != null) { + for (JavacNode node : field.down()) { + if (annotationTypeMatches(Accessors.class, node)) { + return createAnnotation(Accessors.class, node); + } + } + current = field.up(); + } + + return AnnotationValues.of(Accessors.class, field); + } + /** * Checks if there is a field with the provided name. * @@ -382,7 +462,7 @@ public class JavacHandlerUtil { private static GetterMethod findGetter(JavacNode field) { JCVariableDecl decl = (JCVariableDecl)field.get(); JavacNode typeNode = field.up(); - for (String potentialGetterName : toAllGetterNames(decl)) { + for (String potentialGetterName : toAllGetterNames(field)) { for (JavacNode potentialGetter : typeNode.down()) { if (potentialGetter.getKind() != Kind.METHOD) continue; JCMethodDecl method = (JCMethodDecl) potentialGetter.get(); @@ -424,7 +504,8 @@ public class JavacHandlerUtil { } if (hasGetterAnnotation) { - String getterName = toGetterName(decl); + String getterName = toGetterName(field); + if (getterName == null) return null; return new GetterMethod(field.toName(getterName), decl.vartype); } -- cgit