protected final MethodTree rewriteChildren(MethodTree tree) { ModifiersTree mods = (ModifiersTree)translate(tree.getModifiers()); ExpressionTree restype = (ExpressionTree)translateClassRef(tree.getReturnType()); List<? extends TypeParameterTree> typarams = translateStable(tree.getTypeParameters()); List<? extends VariableTree> params = translateStable(tree.getParameters()); List<? extends ExpressionTree> thrown = translate(tree.getThrows()); ExpressionTree defaultValue = (ExpressionTree)translate(tree.getDefaultValue()); BlockTree body = (BlockTree)translate(tree.getBody()); if (restype!=tree.getReturnType() || !typarams.equals(tree.getTypeParameters()) || !params.equals(tree.getParameters()) || !thrown.equals(tree.getThrows()) || mods!=tree.getModifiers() || defaultValue!=tree.getDefaultValue() || body!=tree.getBody()) { if ((((JCModifiers) mods).flags & Flags.GENERATEDCONSTR) != 0) { mods = make.Modifiers(((JCModifiers) mods).flags & ~Flags.GENERATEDCONSTR, mods.getAnnotations()); } MethodTree n = make.Method(mods, tree.getName().toString(), restype, typarams, params, thrown, body, defaultValue); copyCommentTo(tree,n); copyPosTo(tree,n); tree = n; } return tree; }
public ModifiersTree addModifiersModifier(ModifiersTree modifiers, Modifier modifier) { long c = ((JCModifiers) modifiers).flags & ~Flags.GENERATEDCONSTR; switch (modifier) { case ABSTRACT: c = c | Flags.ABSTRACT; break; case FINAL: c = c | Flags.FINAL; break; case NATIVE: c = c | Flags.NATIVE; break; case PRIVATE: c = c | Flags.PRIVATE; break; case PROTECTED: c = c | Flags.PROTECTED; break; case PUBLIC: c = c | Flags.PUBLIC; break; case STATIC: c = c | Flags.STATIC; break; case STRICTFP: c = c | Flags.STRICTFP; break; case SYNCHRONIZED: c = c | Flags.SYNCHRONIZED; break; case TRANSIENT: c = c | Flags.TRANSIENT; break; case VOLATILE: c = c | Flags.VOLATILE; break; case DEFAULT: c = c | Flags.DEFAULT; break; default: break; } return Modifiers(c, modifiers.getAnnotations()); }
public ModifiersTree removeModifiersModifier(ModifiersTree modifiers, Modifier modifier) { long c = ((JCModifiers) modifiers).flags & ~Flags.GENERATEDCONSTR; switch (modifier) { case ABSTRACT: c = c & ~Flags.ABSTRACT; break; case FINAL: c = c & ~Flags.FINAL; break; case NATIVE: c = c & ~Flags.NATIVE; break; case PRIVATE: c = c & ~Flags.PRIVATE; break; case PROTECTED: c = c & ~Flags.PROTECTED; break; case PUBLIC: c = c & ~Flags.PUBLIC; break; case STATIC: c = c & ~Flags.STATIC; break; case STRICTFP: c = c & ~Flags.STRICTFP; break; case SYNCHRONIZED: c = c & ~Flags.SYNCHRONIZED; break; case TRANSIENT: c = c & ~Flags.TRANSIENT; break; case VOLATILE: c = c & ~Flags.VOLATILE; break; case DEFAULT: c = c & ~Flags.DEFAULT; break; default: break; } return Modifiers(c, modifiers.getAnnotations()); }
private void printModifierKeywords(JCModifiers tree) { long v = flagMod & tree.flags; flagMod = -1L; if ((v & SYNTHETIC) != 0) print("/* synthetic */ "); if ((v & PUBLIC) != 0) print("public "); if ((v & PRIVATE) != 0) print("private "); if ((v & PROTECTED) != 0) print("protected "); if ((v & STATIC) != 0) print("static "); if ((v & FINAL) != 0) print("final "); if ((v & SYNCHRONIZED) != 0) print("synchronized "); if ((v & VOLATILE) != 0) print("volatile "); if ((v & TRANSIENT) != 0) print("transient "); if ((v & NATIVE) != 0) print("native "); if ((v & ABSTRACT) != 0) print("abstract "); if ((v & STRICTFP) != 0) print("strictfp "); if ((v & DEFAULT) != 0 && (v & INTERFACE) == 0) print("default "); }
public JCMethodDecl createCanEqual(JavacNode typeNode, JCTree source, List<JCAnnotation> onParam) { /* protected boolean canEqual(final java.lang.Object other) { * return other instanceof Outer.Inner.MyType; * } */ JavacTreeMaker maker = typeNode.getTreeMaker(); JCModifiers mods = maker.Modifiers(Flags.PROTECTED, List.<JCAnnotation>nil()); JCExpression returnType = maker.TypeIdent(CTC_BOOLEAN); Name canEqualName = typeNode.toName("canEqual"); JCExpression objectType = genJavaLangTypeRef(typeNode, "Object"); Name otherName = typeNode.toName("other"); long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext()); List<JCVariableDecl> params = List.of(maker.VarDef(maker.Modifiers(flags, onParam), otherName, objectType, null)); JCBlock body = maker.Block(0, List.<JCStatement>of( maker.Return(maker.TypeTest(maker.Ident(otherName), createTypeReference(typeNode))))); return recursiveSetGeneratedBy(maker.MethodDef(mods, canEqualName, returnType, List.<JCTypeParameter>nil(), params, List.<JCExpression>nil(), body, null), source, typeNode.getContext()); }
private void generateClearMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source) { JCModifiers mods = maker.Modifiers(Flags.PUBLIC); List<JCTypeParameter> typeParams = List.nil(); List<JCExpression> thrown = List.nil(); List<JCVariableDecl> params = List.nil(); List<JCExpression> jceBlank = List.nil(); JCExpression thisDotKeyField = chainDots(builderType, "this", data.getPluralName() + "$key"); JCExpression thisDotKeyFieldDotClear = chainDots(builderType, "this", data.getPluralName() + "$key", "clear"); JCExpression thisDotValueFieldDotClear = chainDots(builderType, "this", data.getPluralName() + "$value", "clear"); JCStatement clearKeyCall = maker.Exec(maker.Apply(jceBlank, thisDotKeyFieldDotClear, jceBlank)); JCStatement clearValueCall = maker.Exec(maker.Apply(jceBlank, thisDotValueFieldDotClear, jceBlank)); JCExpression cond = maker.Binary(CTC_NOT_EQUAL, thisDotKeyField, maker.Literal(CTC_BOT, null)); JCBlock clearCalls = maker.Block(0, List.of(clearKeyCall, clearValueCall)); JCStatement ifSetCallClear = maker.If(cond, clearCalls, null); List<JCStatement> statements = returnStatement != null ? List.of(ifSetCallClear, returnStatement) : List.of(ifSetCallClear); JCBlock body = maker.Block(0, statements); Name methodName = builderType.toName(HandlerUtil.buildAccessorName("clear", data.getPluralName().toString())); JCMethodDecl method = maker.MethodDef(mods, methodName, returnType, typeParams, params, thrown, body, null); injectMethod(builderType, method); }
private void generateClearMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source) { JCModifiers mods = maker.Modifiers(Flags.PUBLIC); List<JCTypeParameter> typeParams = List.nil(); List<JCExpression> thrown = List.nil(); List<JCVariableDecl> params = List.nil(); List<JCExpression> jceBlank = List.nil(); JCExpression thisDotField = maker.Select(maker.Ident(builderType.toName("this")), data.getPluralName()); JCExpression thisDotFieldDotClear = maker.Select(maker.Select(maker.Ident(builderType.toName("this")), data.getPluralName()), builderType.toName("clear")); JCStatement clearCall = maker.Exec(maker.Apply(jceBlank, thisDotFieldDotClear, jceBlank)); JCExpression cond = maker.Binary(CTC_NOT_EQUAL, thisDotField, maker.Literal(CTC_BOT, null)); JCStatement ifSetCallClear = maker.If(cond, clearCall, null); List<JCStatement> statements = returnStatement != null ? List.of(ifSetCallClear, returnStatement) : List.of(ifSetCallClear); JCBlock body = maker.Block(0, statements); Name methodName = builderType.toName(HandlerUtil.buildAccessorName("clear", data.getPluralName().toString())); JCMethodDecl method = maker.MethodDef(mods, methodName, returnType, typeParams, params, thrown, body, null); injectMethod(builderType, method); }
void generateSingularMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) { List<JCTypeParameter> typeParams = List.nil(); List<JCExpression> thrown = List.nil(); JCModifiers mods = maker.Modifiers(Flags.PUBLIC); ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>(); statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, false, source)); JCExpression thisDotFieldDotAdd = chainDots(builderType, "this", data.getPluralName().toString(), "add"); JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotFieldDotAdd, List.<JCExpression>of(maker.Ident(data.getSingularName()))); statements.append(maker.Exec(invokeAdd)); if (returnStatement != null) statements.append(returnStatement); JCBlock body = maker.Block(0, statements.toList()); Name name = data.getSingularName(); long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext()); if (!fluent) name = builderType.toName(HandlerUtil.buildAccessorName("add", name.toString())); JCExpression paramType = cloneParamType(0, maker, data.getTypeArgs(), builderType, source); JCVariableDecl param = maker.VarDef(maker.Modifiers(paramFlags), data.getSingularName(), paramType, null); JCMethodDecl method = maker.MethodDef(mods, name, returnType, typeParams, List.of(param), thrown, body, null); injectMethod(builderType, method); }
void generatePluralMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) { List<JCTypeParameter> typeParams = List.nil(); List<JCExpression> thrown = List.nil(); JCModifiers mods = maker.Modifiers(Flags.PUBLIC); ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>(); statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, false, source)); JCExpression thisDotFieldDotAdd = chainDots(builderType, "this", data.getPluralName().toString(), "addAll"); JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotFieldDotAdd, List.<JCExpression>of(maker.Ident(data.getPluralName()))); statements.append(maker.Exec(invokeAdd)); if (returnStatement != null) statements.append(returnStatement); JCBlock body = maker.Block(0, statements.toList()); Name name = data.getPluralName(); long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext()); if (!fluent) name = builderType.toName(HandlerUtil.buildAccessorName("addAll", name.toString())); JCExpression paramType = chainDots(builderType, "java", "util", "Collection"); paramType = addTypeArgs(1, true, builderType, paramType, data.getTypeArgs(), source); JCVariableDecl param = maker.VarDef(maker.Modifiers(paramFlags), data.getPluralName(), paramType, null); JCMethodDecl method = maker.MethodDef(mods, name, returnType, typeParams, List.of(param), thrown, body, null); injectMethod(builderType, method); }
public JCMethodDecl createCanEqual(JavacNode typeNode, JCTree source, List<JCAnnotation> onParam) { /* public boolean canEqual(final java.lang.Object other) { * return other instanceof Outer.Inner.MyType; * } */ JavacTreeMaker maker = typeNode.getTreeMaker(); JCModifiers mods = maker.Modifiers(Flags.PROTECTED, List.<JCAnnotation>nil()); JCExpression returnType = maker.TypeIdent(CTC_BOOLEAN); Name canEqualName = typeNode.toName("canEqual"); JCExpression objectType = genJavaLangTypeRef(typeNode, "Object"); Name otherName = typeNode.toName("other"); long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext()); List<JCVariableDecl> params = List.of(maker.VarDef(maker.Modifiers(flags, onParam), otherName, objectType, null)); JCBlock body = maker.Block(0, List.<JCStatement>of( maker.Return(maker.TypeTest(maker.Ident(otherName), createTypeReference(typeNode))))); return recursiveSetGeneratedBy(maker.MethodDef(mods, canEqualName, returnType, List.<JCTypeParameter>nil(), params, List.<JCExpression>nil(), body, null), source, typeNode.getContext()); }
@Override public boolean visitAnnotationDeclaration(AnnotationDeclaration node) { JCModifiers modifiers = (JCModifiers) toTree(node.astModifiers()); modifiers.flags |= Flags.INTERFACE | Flags.ANNOTATION; int start = posOfStructure(node, "interface", true); int end = node.getPosition().getEnd(); if (hasSourceStructures()) { if (modifiers.pos == -1) modifiers.pos = posOfStructure(node, "@", true); endPosTable.put(modifiers, posOfStructure(node, "@", false)); } return set(node, setPos(start, end, treeMaker.ClassDef( modifiers, toName(node.astName()), List.<JCTypeParameter>nil(), null, List.<JCExpression>nil(), node.astBody() == null ? List.<JCTree>nil() : toList(JCTree.class, node.astBody().astMembers()) ))); }
@Override public boolean visitAnnotationMethodDeclaration(AnnotationMethodDeclaration node) { JCMethodDecl methodDef = treeMaker.MethodDef( (JCModifiers)toTree(node.astModifiers()), toName(node.astMethodName()), toExpression(node.astReturnTypeReference()), List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), null, toExpression(node.astDefaultValue()) ); int start = node.astMethodName().getPosition().getStart(); int end = node.getPosition().getEnd(); return set(node, setPos(start, end, methodDef)); }
@Override public boolean visitMethodDeclaration(MethodDeclaration node) { JCMethodDecl methodDef = treeMaker.MethodDef( (JCModifiers)toTree(node.astModifiers()), toName(node.astMethodName()), toExpression(node.astReturnTypeReference()), toList(JCTypeParameter.class, node.astTypeVariables()), toList(JCVariableDecl.class, node.astParameters()), toList(JCExpression.class, node.astThrownTypeReferences()), (JCBlock)toTree(node.astBody()), null ); for (JCVariableDecl decl : methodDef.params) { decl.mods.flags |= Flags.PARAMETER; } int start = node.astMethodName().getPosition().getStart(); int end = node.getPosition().getEnd(); return set(node, setPos(start, end, methodDef)); }
@Override public boolean visitConstructorDeclaration(ConstructorDeclaration node) { JCMethodDecl constrDef = treeMaker.MethodDef( (JCModifiers)toTree(node.astModifiers()), table.init, null, toList(JCTypeParameter.class, node.astTypeVariables()), toList(JCVariableDecl.class, node.astParameters()), toList(JCExpression.class, node.astThrownTypeReferences()), (JCBlock)toTree(node.astBody()), null ); for (JCVariableDecl decl : constrDef.params) { decl.mods.flags |= Flags.PARAMETER; } int start = node.astTypeName().getPosition().getStart(); int end = node.getPosition().getEnd(); return set(node, setPos(start, end, constrDef)); }
private Description fixByModifyingMethod( VisitorState state, JCClassDecl enclosingClass, MethodTree method) { JCModifiers methodModifiers = ((JCMethodDecl) method).getModifiers(); String replacementModifiersString = createReplacementMethodModifiers(state, methodModifiers); JCModifiers enclosingClassModifiers = enclosingClass.getModifiers(); String enclosingClassReplacementModifiersString = createReplacementClassModifiers(state, enclosingClassModifiers); SuggestedFix.Builder fixBuilder = SuggestedFix.builder() .addImport("dagger.multibindings.Multibinds") .replace(methodModifiers, replacementModifiersString) .replace(method.getBody(), ";"); fixBuilder = (enclosingClassModifiers.pos == -1) ? fixBuilder.prefixWith(enclosingClass, enclosingClassReplacementModifiersString) : fixBuilder.replace(enclosingClassModifiers, enclosingClassReplacementModifiersString); return describeMatch(method, fixBuilder.build()); }
private String createReplacementMethodModifiers(VisitorState state, JCModifiers modifiers) { ImmutableList.Builder<String> modifierStringsBuilder = ImmutableList.<String>builder().add("@Multibinds"); for (JCAnnotation annotation : modifiers.annotations) { Name annotationQualifiedName = ASTHelpers.getSymbol(annotation).getQualifiedName(); if (!(annotationQualifiedName.contentEquals("dagger.Provides") || annotationQualifiedName.contentEquals("dagger.producers.Produces") || annotationQualifiedName.contentEquals("dagger.multibindings.ElementsIntoSet"))) { modifierStringsBuilder.add(state.getSourceForNode(annotation)); } } EnumSet<Flag> methodFlags = Flags.asFlagSet(modifiers.flags); methodFlags.remove(Flags.Flag.STATIC); methodFlags.remove(Flags.Flag.FINAL); methodFlags.add(Flags.Flag.ABSTRACT); for (Flag flag : methodFlags) { modifierStringsBuilder.add(flag.toString()); } return Joiner.on(' ').join(modifierStringsBuilder.build()); }
private String createReplacementClassModifiers( VisitorState state, JCModifiers enclosingClassModifiers) { ImmutableList.Builder<String> classModifierStringsBuilder = ImmutableList.builder(); for (JCAnnotation annotation : enclosingClassModifiers.annotations) { classModifierStringsBuilder.add(state.getSourceForNode(annotation)); } EnumSet<Flag> classFlags = Flags.asFlagSet(enclosingClassModifiers.flags); classFlags.remove(Flags.Flag.FINAL); classFlags.add(Flags.Flag.ABSTRACT); for (Flag flag : classFlags) { classModifierStringsBuilder.add(flag.toString()); } return Joiner.on(' ').join(classModifierStringsBuilder.build()); }
/** ClassCreatorRest = Arguments [ClassBody] */ JCNewClass classCreatorRest(int newpos, JCExpression encl, List<JCExpression> typeArgs, JCExpression t) { List<JCExpression> args = arguments(); JCClassDecl body = null; if (S.token() == LBRACE) { int pos = S.pos(); List<JCTree> defs = classOrInterfaceBody(names.empty, false); JCModifiers mods = F.at(Position.NOPOS).Modifiers(0); body = toP(F.at(pos).AnonymousClassDef(mods, defs)); } return toP(F.at(newpos).NewClass(encl, typeArgs, t, args, body)); }
/** VariableDeclaratorsRest = VariableDeclaratorRest { "," VariableDeclarator } * ConstantDeclaratorsRest = ConstantDeclaratorRest { "," ConstantDeclarator } * * @param reqInit Is an initializer always required? * @param dc The documentation comment for the variable declarations, or null. */ <T extends ListBuffer<? super JCVariableDecl>> T variableDeclaratorsRest(int pos, JCModifiers mods, JCExpression type, Name name, boolean reqInit, String dc, T vdefs) { vdefs.append(variableDeclaratorRest(pos, mods, type, name, reqInit, dc)); while (S.token() == COMMA) { // All but last of multiple declarators subsume a comma storeEnd((JCTree)vdefs.elems.last(), S.endPos()); S.nextToken(); vdefs.append(variableDeclarator(mods, type, reqInit, dc)); } return vdefs; }
/** ClassDeclaration = CLASS Ident TypeParametersOpt [EXTENDS Type] * [IMPLEMENTS TypeList] ClassBody * @param mods The modifiers starting the class declaration * @param dc The documentation comment for the class, or null. */ JCClassDecl classDeclaration(JCModifiers mods, String dc) { int pos = S.pos(); accept(CLASS); Name name = ident(); List<JCTypeParameter> typarams = typeParametersOpt(); JCExpression extending = null; if (S.token() == EXTENDS) { S.nextToken(); extending = parseType(); } List<JCExpression> implementing = List.nil(); if (S.token() == IMPLEMENTS) { S.nextToken(); implementing = typeList(); } List<JCTree> defs = classOrInterfaceBody(name, false); JCClassDecl result = toP(F.at(pos).ClassDef( mods, name, typarams, extending, implementing, defs)); attach(result, dc); return result; }
/** InterfaceDeclaration = INTERFACE Ident TypeParametersOpt * [EXTENDS TypeList] InterfaceBody * @param mods The modifiers starting the interface declaration * @param dc The documentation comment for the interface, or null. */ JCClassDecl interfaceDeclaration(JCModifiers mods, String dc) { int pos = S.pos(); accept(INTERFACE); Name name = ident(); List<JCTypeParameter> typarams = typeParametersOpt(); List<JCExpression> extending = List.nil(); if (S.token() == EXTENDS) { S.nextToken(); extending = typeList(); } List<JCTree> defs = classOrInterfaceBody(name, true); JCClassDecl result = toP(F.at(pos).ClassDef( mods, name, typarams, null, extending, defs)); attach(result, dc); return result; }
/** EnumDeclaration = ENUM Ident [IMPLEMENTS TypeList] EnumBody * @param mods The modifiers starting the enum declaration * @param dc The documentation comment for the enum, or null. */ JCClassDecl enumDeclaration(JCModifiers mods, String dc) { int pos = S.pos(); accept(ENUM); Name name = ident(); List<JCExpression> implementing = List.nil(); if (S.token() == IMPLEMENTS) { S.nextToken(); implementing = typeList(); } List<JCTree> defs = enumBody(name); mods.flags |= Flags.ENUM; JCClassDecl result = toP(F.at(pos). ClassDef(mods, name, List.<JCTypeParameter>nil(), null, implementing, defs)); attach(result, dc); return result; }
@Override public Description matchVariable(VariableTree tree, VisitorState state) { if (ANNOTATED_WITH_GUICE_INJECT_MATCHER.matches(tree, state) && FINAL_FIELD_MATCHER.matches(tree, state)) { JCModifiers modifiers = ((JCVariableDecl) tree).getModifiers(); long replacementFlags = modifiers.flags ^ Flags.FINAL; JCModifiers replacementModifiers = TreeMaker.instance(state.context) .Modifiers(replacementFlags, modifiers.annotations); /* * replace new lines with strings, trim whitespace and remove empty parens to make the * suggested fixes look sane */ String replacementModifiersString = replacementModifiers.toString().replace('\n', ' ').replace("()", "").trim(); return describeMatch(modifiers, new SuggestedFix().replace(modifiers, replacementModifiersString)); } return Description.NO_MATCH; }
private JCMethodDecl createCanEqual(JavacNode typeNode, JCTree source) { /* public boolean canEqual(final java.lang.Object other) { * return other instanceof Outer.Inner.MyType; * } */ TreeMaker maker = typeNode.getTreeMaker(); JCModifiers mods = maker.Modifiers(Flags.PUBLIC, List.<JCAnnotation>nil()); JCExpression returnType = maker.TypeIdent(CTC_BOOLEAN); Name canEqualName = typeNode.toName("canEqual"); JCExpression objectType = chainDots(typeNode, "java", "lang", "Object"); Name otherName = typeNode.toName("other"); List<JCVariableDecl> params = List.of(maker.VarDef(maker.Modifiers(Flags.FINAL), otherName, objectType, null)); JCBlock body = maker.Block(0, List.<JCStatement>of( maker.Return(maker.TypeTest(maker.Ident(otherName), createTypeReference(typeNode))))); return recursiveSetGeneratedBy(maker.MethodDef(mods, canEqualName, returnType, List.<JCTypeParameter>nil(), params, List.<JCExpression>nil(), body, null), source); }
@Override public IndexedClassDecl ClassDef(JCModifiers mods, Name name, List<JCTypeParameter> typarams, JCExpression extending, List<JCExpression> implementing, List<JCTree> defs) { IndexedClassDecl result = new IndexedClassDecl(mods, name, typarams, extending, implementing, defs, null); result.pos = pos; return result; }
protected IndexedClassDecl(JCModifiers mods, Name name, List<JCTypeParameter> typarams, JCExpression extending, List<JCExpression> implementing, List<JCTree> defs, ClassSymbol sym) { super(mods, name, typarams, extending, implementing, defs, sym); this.index = -1; }
@Override protected JCClassDecl classDeclaration(JCModifiers mods, Comment dc) { if (cancelService != null) { cancelService.abortIfCanceled(); } return super.classDeclaration(mods, dc); }
@Override protected JCClassDecl interfaceDeclaration(JCModifiers mods, Comment dc) { if (cancelService != null) { cancelService.abortIfCanceled(); } return super.interfaceDeclaration(mods, dc); }
@Override protected JCClassDecl enumDeclaration(JCModifiers mods, Comment dc) { if (cancelService != null) { cancelService.abortIfCanceled(); } return super.enumDeclaration(mods, dc); }
@Override protected JCTree methodDeclaratorRest(int pos, JCModifiers mods, JCExpression type, Name name, List<JCTypeParameter> typarams, boolean isInterface, boolean isVoid, Comment dc) { if (cancelService != null) { cancelService.abortIfCanceled(); } return super.methodDeclaratorRest(pos, mods, type, name, typarams, isInterface, isVoid, dc); }
private void check(CharSequence name, ModifiersTree modifiers) { long sysflags = ((JCModifiers) modifiers).flags; System.err.println(name + ": " + modifiers.getFlags() + " | " + Flags.toString(sysflags)); if (name.toString().startsWith("x_")) { String expected = "[" + name.toString().substring(2) + "]"; String found = modifiers.getFlags().toString(); if (!found.equals(expected)) throw new AssertionError("expected: " + expected + "; found: " + found); } }
public static void addConstructorProperties(JCModifiers mods, JavacNode node, List<JavacNode> fields) { if (fields.isEmpty()) return; JavacTreeMaker maker = node.getTreeMaker(); JCExpression constructorPropertiesType = chainDots(node, "java", "beans", "ConstructorProperties"); ListBuffer<JCExpression> fieldNames = new ListBuffer<JCExpression>(); for (JavacNode field : fields) { Name fieldName = removePrefixFromField(field); fieldNames.append(maker.Literal(fieldName.toString())); } JCExpression fieldNamesArray = maker.NewArray(null, List.<JCExpression>nil(), fieldNames.toList()); JCAnnotation annotation = maker.Annotation(constructorPropertiesType, List.of(fieldNamesArray)); mods.annotations = mods.annotations.append(annotation); }
@Override public void handle(AnnotationValues<Value> annotation, JCAnnotation ast, JavacNode annotationNode) { @SuppressWarnings("deprecation") Class<? extends Annotation> oldExperimentalValue = lombok.experimental.Value.class; handleFlagUsage(annotationNode, ConfigurationKeys.VALUE_FLAG_USAGE, "@Value"); deleteAnnotationIfNeccessary(annotationNode, Value.class, oldExperimentalValue); JavacNode typeNode = annotationNode.up(); boolean notAClass = !isClass(typeNode); if (notAClass) { annotationNode.addError("@Value is only supported on a class."); return; } String staticConstructorName = annotation.getInstance().staticConstructor(); if (!hasAnnotationAndDeleteIfNeccessary(NonFinal.class, typeNode)) { JCModifiers jcm = ((JCClassDecl) typeNode.get()).mods; if ((jcm.flags & Flags.FINAL) == 0) { jcm.flags |= Flags.FINAL; typeNode.rebuild(); } } new HandleFieldDefaults().generateFieldDefaultsForType(typeNode, annotationNode, AccessLevel.PRIVATE, true, true); // TODO move this to the end OR move it to the top in eclipse. new HandleConstructor().generateAllArgsConstructor(typeNode, AccessLevel.PUBLIC, staticConstructorName, SkipIfConstructorExists.YES, annotationNode); new HandleGetter().generateGetterForType(typeNode, annotationNode, AccessLevel.PUBLIC, true); new HandleEqualsAndHashCode().generateEqualsAndHashCodeForType(typeNode, annotationNode); new HandleToString().generateToStringForType(typeNode, annotationNode); }
private void createPrivateDefaultConstructor(JavacNode typeNode) { JavacTreeMaker maker = typeNode.getTreeMaker(); JCModifiers mods = maker.Modifiers(Flags.PRIVATE, List.<JCAnnotation>nil()); Name name = typeNode.toName("<init>"); JCBlock block = maker.Block(0L, createThrowStatement(typeNode, maker)); JCMethodDecl methodDef = maker.MethodDef(mods, name, null, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), block, null); JCMethodDecl constructor = recursiveSetGeneratedBy(methodDef, typeNode.get(), typeNode.getContext()); JavacHandlerUtil.injectMethod(typeNode, constructor); }
public static void addSuppressWarningsAll(JCModifiers mods, JavacNode node, int pos, JCTree source, Context context) { if (!LombokOptionsFactory.getDelombokOptions(context).getFormatPreferences().generateSuppressWarnings()) return; addAnnotation(mods, node, pos, source, context, "java.lang.SuppressWarnings", node.getTreeMaker().Literal("all")); if (Boolean.TRUE.equals(node.getAst().readConfiguration(ConfigurationKeys.ADD_FINDBUGS_SUPPRESSWARNINGS_ANNOTATIONS))) { JavacTreeMaker maker = node.getTreeMaker(); JCExpression arg = maker.Assign(maker.Ident(node.toName("justification")), maker.Literal("generated code")); addAnnotation(mods, node, pos, source, context, "edu.umd.cs.findbugs.annotations.SuppressFBWarnings", arg); } }
public static void addGenerated(JCModifiers mods, JavacNode node, int pos, JCTree source, Context context) { if (!LombokOptionsFactory.getDelombokOptions(context).getFormatPreferences().generateGenerated()) return; if (!Boolean.FALSE.equals(node.getAst().readConfiguration(ConfigurationKeys.ADD_GENERATED_ANNOTATIONS))) { addAnnotation(mods, node, pos, source, context, "javax.annotation.Generated", node.getTreeMaker().Literal("lombok")); } }
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)); } } }