List of usage examples for org.eclipse.jdt.internal.compiler.ast MethodDeclaration MethodDeclaration
public MethodDeclaration(CompilationResult compilationResult)
From source file:com.codenvy.ide.ext.java.server.internal.compiler.parser.SourceTypeConverter.java
License:Open Source License
private AbstractMethodDeclaration convert(SourceMethod methodHandle, SourceMethodElementInfo methodInfo, CompilationResult compilationResult) throws JavaModelException { AbstractMethodDeclaration method;/*from ww w .ja va 2s . co m*/ /* only source positions available */ int start = methodInfo.getNameSourceStart(); int end = methodInfo.getNameSourceEnd(); /* https://bugs.eclipse.org/bugs/show_bug.cgi?id=324850, Even when this type is being constructed on behalf of a 1.4 project we must internalize type variables properly in order to be able to recognize usages of them in the method signature, to apply substitutions and thus to be able to detect overriding in the presence of generics. If we simply drop them, when the method signature refers to the type parameter, we won't know it should be bound to the type parameter and perform incorrect lookup and may mistakenly end up with missing types */ TypeParameter[] typeParams = null; char[][] typeParameterNames = methodInfo.getTypeParameterNames(); if (typeParameterNames != null) { int parameterCount = typeParameterNames.length; if (parameterCount > 0) { // method's type parameters must be null if no type parameter char[][][] typeParameterBounds = methodInfo.getTypeParameterBounds(); typeParams = new TypeParameter[parameterCount]; for (int i = 0; i < parameterCount; i++) { typeParams[i] = createTypeParameter(typeParameterNames[i], typeParameterBounds[i], start, end); } } } int modifiers = methodInfo.getModifiers(); if (methodInfo.isConstructor()) { ConstructorDeclaration decl = new ConstructorDeclaration(compilationResult); decl.bits &= ~ASTNode.IsDefaultConstructor; method = decl; decl.typeParameters = typeParams; } else { MethodDeclaration decl; if (methodInfo.isAnnotationMethod()) { AnnotationMethodDeclaration annotationMethodDeclaration = new AnnotationMethodDeclaration( compilationResult); /* conversion of default value */ SourceAnnotationMethodInfo annotationMethodInfo = (SourceAnnotationMethodInfo) methodInfo; boolean hasDefaultValue = annotationMethodInfo.defaultValueStart != -1 || annotationMethodInfo.defaultValueEnd != -1; if ((this.flags & FIELD_INITIALIZATION) != 0) { if (hasDefaultValue) { char[] defaultValueSource = CharOperation.subarray(getSource(), annotationMethodInfo.defaultValueStart, annotationMethodInfo.defaultValueEnd + 1); if (defaultValueSource != null) { Expression expression = parseMemberValue(defaultValueSource); if (expression != null) { annotationMethodDeclaration.defaultValue = expression; } } else { // could not retrieve the default value hasDefaultValue = false; } } } if (hasDefaultValue) modifiers |= ClassFileConstants.AccAnnotationDefault; decl = annotationMethodDeclaration; } else { decl = new MethodDeclaration(compilationResult); } // convert return type decl.returnType = createTypeReference(methodInfo.getReturnTypeName(), start, end); // type parameters decl.typeParameters = typeParams; method = decl; } method.selector = methodHandle.getElementName().toCharArray(); boolean isVarargs = (modifiers & ClassFileConstants.AccVarargs) != 0; method.modifiers = modifiers & ~ClassFileConstants.AccVarargs; method.sourceStart = start; method.sourceEnd = end; method.declarationSourceStart = methodInfo.getDeclarationSourceStart(); method.declarationSourceEnd = methodInfo.getDeclarationSourceEnd(); // convert 1.5 specific constructs only if compliance is 1.5 or above if (this.has1_5Compliance) { /* convert annotations */ method.annotations = convertAnnotations(methodHandle); } /* convert arguments */ String[] argumentTypeSignatures = methodHandle.getParameterTypes(); char[][] argumentNames = methodInfo.getArgumentNames(); int argumentCount = argumentTypeSignatures == null ? 0 : argumentTypeSignatures.length; if (argumentCount > 0) { ILocalVariable[] parameters = methodHandle.getParameters(); long position = ((long) start << 32) + end; method.arguments = new Argument[argumentCount]; for (int i = 0; i < argumentCount; i++) { TypeReference typeReference = createTypeReference(argumentTypeSignatures[i], start, end); if (isVarargs && i == argumentCount - 1) { typeReference.bits |= ASTNode.IsVarArgs; } method.arguments[i] = new Argument(argumentNames[i], position, typeReference, ClassFileConstants.AccDefault); // do not care whether was final or not // convert 1.5 specific constructs only if compliance is 1.5 or above if (this.has1_5Compliance) { /* convert annotations */ method.arguments[i].annotations = convertAnnotations(parameters[i]); } } } /* convert thrown exceptions */ char[][] exceptionTypeNames = methodInfo.getExceptionTypeNames(); int exceptionCount = exceptionTypeNames == null ? 0 : exceptionTypeNames.length; if (exceptionCount > 0) { method.thrownExceptions = new TypeReference[exceptionCount]; for (int i = 0; i < exceptionCount; i++) { method.thrownExceptions[i] = createTypeReference(exceptionTypeNames[i], start, end); } } /* convert local and anonymous types */ if ((this.flags & LOCAL_TYPE) != 0) { IJavaElement[] children = methodInfo.getChildren(); int typesLength = children.length; if (typesLength != 0) { Statement[] statements = new Statement[typesLength]; for (int i = 0; i < typesLength; i++) { SourceType type = (SourceType) children[i]; TypeDeclaration localType = convert(type, compilationResult); if ((localType.bits & ASTNode.IsAnonymousType) != 0) { QualifiedAllocationExpression expression = new QualifiedAllocationExpression(localType); expression.type = localType.superclass; localType.superclass = null; localType.superInterfaces = null; localType.allocation = expression; statements[i] = expression; } else { statements[i] = localType; } } method.statements = statements; } } return method; }
From source file:com.codenvy.ide.ext.java.server.internal.core.BinaryTypeConverter.java
License:Open Source License
private AbstractMethodDeclaration convert(IMethod method, IType type) throws JavaModelException { AbstractMethodDeclaration methodDeclaration; org.eclipse.jdt.internal.compiler.ast.TypeParameter[] typeParams = null; // convert 1.5 specific constructs only if compliance is 1.5 or above if (this.has1_5Compliance) { /* convert type parameters */ ITypeParameter[] typeParameters = method.getTypeParameters(); if (typeParameters != null && typeParameters.length > 0) { int parameterCount = typeParameters.length; typeParams = new org.eclipse.jdt.internal.compiler.ast.TypeParameter[parameterCount]; for (int i = 0; i < parameterCount; i++) { ITypeParameter typeParameter = typeParameters[i]; typeParams[i] = createTypeParameter(typeParameter.getElementName().toCharArray(), stringArrayToCharArray(typeParameter.getBounds()), 0, 0); }//w w w . ja va2 s. c om } } if (method.isConstructor()) { ConstructorDeclaration decl = new ConstructorDeclaration(this.compilationResult); decl.bits &= ~ASTNode.IsDefaultConstructor; decl.typeParameters = typeParams; methodDeclaration = decl; } else { MethodDeclaration decl = type.isAnnotation() ? new AnnotationMethodDeclaration(this.compilationResult) : new MethodDeclaration(this.compilationResult); /* convert return type */ TypeReference typeReference = createTypeReference(method.getReturnType()); if (typeReference == null) return null; decl.returnType = typeReference; decl.typeParameters = typeParams; methodDeclaration = decl; } methodDeclaration.selector = method.getElementName().toCharArray(); int flags = method.getFlags(); boolean isVarargs = Flags.isVarargs(flags); methodDeclaration.modifiers = flags & ~Flags.AccVarargs; /* convert arguments */ String[] argumentTypeNames = method.getParameterTypes(); String[] argumentNames = method.getParameterNames(); int argumentCount = argumentTypeNames == null ? 0 : argumentTypeNames.length; // Ignore synthetic arguments (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=212224) int startIndex = (method.isConstructor() && type.isMember() && !Flags.isStatic(type.getFlags())) ? 1 : 0; argumentCount -= startIndex; methodDeclaration.arguments = new Argument[argumentCount]; for (int i = 0; i < argumentCount; i++) { String argumentTypeName = argumentTypeNames[startIndex + i]; TypeReference typeReference = createTypeReference(argumentTypeName); if (typeReference == null) return null; if (isVarargs && i == argumentCount - 1) { typeReference.bits |= ASTNode.IsVarArgs; } methodDeclaration.arguments[i] = new Argument(argumentNames[i].toCharArray(), 0, typeReference, ClassFileConstants.AccDefault); // do not care whether was final or not } /* convert thrown exceptions */ String[] exceptionTypeNames = method.getExceptionTypes(); int exceptionCount = exceptionTypeNames == null ? 0 : exceptionTypeNames.length; if (exceptionCount > 0) { methodDeclaration.thrownExceptions = new TypeReference[exceptionCount]; for (int i = 0; i < exceptionCount; i++) { TypeReference typeReference = createTypeReference(exceptionTypeNames[i]); if (typeReference == null) return null; methodDeclaration.thrownExceptions[i] = typeReference; } } return methodDeclaration; }
From source file:com.google.gwt.dev.javac.BinaryTypeReferenceRestrictionsCheckerTest.java
License:Apache License
/** * Creates a mock {@link CompilationUnitDeclaration} that has binary type * references in a superclass reference, in a method return type, in an * annotation and in a local variable declaration. It then checks that the we * find all of these locations except for the one used in an annotation. *///from w ww . ja v a2s . c o m public void testFindAllBinaryTypeReferenceSites() { CompilationResult compilationResult = new CompilationResult("TestCompilationUnit.java".toCharArray(), 0, 0, 0); CompilationUnitDeclaration cud = new CompilationUnitDeclaration(null, compilationResult, 1); LookupEnvironment lookupEnvironment = createMockLookupEnvironment(); cud.scope = new CompilationUnitScope(cud, lookupEnvironment); TypeDeclaration typeDeclaration = new TypeDeclaration(compilationResult); typeDeclaration.scope = new ClassScope(cud.scope, null); typeDeclaration.staticInitializerScope = new MethodScope(typeDeclaration.scope, null, false); cud.types = new TypeDeclaration[] { typeDeclaration }; BinaryTypeBinding binaryTypeBinding = new BinaryTypeBinding(null, new MockBinaryType(BINARY_TYPE_NAME), lookupEnvironment); typeDeclaration.superclass = createMockBinaryTypeReference(binaryTypeBinding); MethodDeclaration methodDeclaration = new MethodDeclaration(compilationResult); methodDeclaration.scope = new MethodScope(typeDeclaration.scope, null, false); methodDeclaration.returnType = createMockBinaryTypeReference(binaryTypeBinding); LocalDeclaration localDeclaration = new LocalDeclaration(null, 0, 0); localDeclaration.type = createMockBinaryTypeReference(binaryTypeBinding); methodDeclaration.statements = new Statement[] { localDeclaration }; SingleMemberAnnotation annotation = new SingleMemberAnnotation( createMockBinaryTypeReference(binaryTypeBinding), 0); annotation.memberValue = annotation.type; typeDeclaration.annotations = new Annotation[] { annotation }; typeDeclaration.methods = new AbstractMethodDeclaration[] { methodDeclaration }; /* * Check that we find binary type references in the following expected * locations. */ Expression[] expectedExpressions = new Expression[] { typeDeclaration.superclass, methodDeclaration.returnType, localDeclaration.type }; List<BinaryTypeReferenceSite> binaryTypeReferenceSites = BinaryTypeReferenceRestrictionsChecker .findAllBinaryTypeReferenceSites(cud); assertEquals(expectedExpressions.length, binaryTypeReferenceSites.size()); for (int i = 0; i < binaryTypeReferenceSites.size(); ++i) { BinaryTypeReferenceSite binaryTypeReferenceSite = binaryTypeReferenceSites.get(i); assertSame(binaryTypeBinding, binaryTypeReferenceSite.getBinaryTypeBinding()); assertSame(expectedExpressions[i], binaryTypeReferenceSite.getExpression()); } }
From source file:lombok.eclipse.agent.PatchDelegate.java
License:Open Source License
private static MethodDeclaration createDelegateMethod(char[] name, EclipseNode typeNode, BindingTuple pair, CompilationResult compilationResult, EclipseNode annNode, DelegateReceiver delegateReceiver) { /* public <T, U, ...> ReturnType methodName(ParamType1 name1, ParamType2 name2, ...) throws T1, T2, ... { * (return) delegate.<T, U>methodName(name1, name2); * }/*from w w w . j a v a 2 s . c o m*/ */ boolean isVarargs = (pair.base.modifiers & ClassFileConstants.AccVarargs) != 0; try { checkConflictOfTypeVarNames(pair, typeNode); } catch (CantMakeDelegates e) { annNode.addError( "There's a conflict in the names of type parameters. Fix it by renaming the following type parameters of your class: " + e.conflicted); return null; } ASTNode source = annNode.get(); int pS = source.sourceStart, pE = source.sourceEnd; MethodBinding binding = pair.parameterized; MethodDeclaration method = new MethodDeclaration(compilationResult); setGeneratedBy(method, source); method.sourceStart = pS; method.sourceEnd = pE; method.modifiers = ClassFileConstants.AccPublic; method.returnType = makeType(binding.returnType, source, false); boolean isDeprecated = binding.isDeprecated(); method.selector = binding.selector; if (binding.thrownExceptions != null && binding.thrownExceptions.length > 0) { method.thrownExceptions = new TypeReference[binding.thrownExceptions.length]; for (int i = 0; i < method.thrownExceptions.length; i++) { method.thrownExceptions[i] = makeType(binding.thrownExceptions[i], source, false); } } MessageSend call = new MessageSend(); call.sourceStart = pS; call.sourceEnd = pE; call.nameSourcePosition = pos(source); setGeneratedBy(call, source); call.receiver = delegateReceiver.get(source, name); call.selector = binding.selector; if (binding.typeVariables != null && binding.typeVariables.length > 0) { method.typeParameters = new TypeParameter[binding.typeVariables.length]; call.typeArguments = new TypeReference[binding.typeVariables.length]; for (int i = 0; i < method.typeParameters.length; i++) { method.typeParameters[i] = new TypeParameter(); method.typeParameters[i].sourceStart = pS; method.typeParameters[i].sourceEnd = pE; setGeneratedBy(method.typeParameters[i], source); method.typeParameters[i].name = binding.typeVariables[i].sourceName; call.typeArguments[i] = new SingleTypeReference(binding.typeVariables[i].sourceName, pos(source)); setGeneratedBy(call.typeArguments[i], source); ReferenceBinding super1 = binding.typeVariables[i].superclass; ReferenceBinding[] super2 = binding.typeVariables[i].superInterfaces; if (super2 == null) super2 = new ReferenceBinding[0]; if (super1 != null || super2.length > 0) { int offset = super1 == null ? 0 : 1; method.typeParameters[i].bounds = new TypeReference[super2.length + offset - 1]; if (super1 != null) method.typeParameters[i].type = makeType(super1, source, false); else method.typeParameters[i].type = makeType(super2[0], source, false); int ctr = 0; for (int j = (super1 == null) ? 1 : 0; j < super2.length; j++) { method.typeParameters[i].bounds[ctr] = makeType(super2[j], source, false); method.typeParameters[i].bounds[ctr++].bits |= ASTNode.IsSuperType; } } } } if (isDeprecated) { method.annotations = new Annotation[] { generateDeprecatedAnnotation(source) }; } method.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; if (binding.parameters != null && binding.parameters.length > 0) { method.arguments = new Argument[binding.parameters.length]; call.arguments = new Expression[method.arguments.length]; for (int i = 0; i < method.arguments.length; i++) { AbstractMethodDeclaration sourceElem; try { sourceElem = pair.base.sourceMethod(); } catch (Exception e) { sourceElem = null; } char[] argName; if (sourceElem == null) argName = ("arg" + i).toCharArray(); else { argName = sourceElem.arguments[i].name; } method.arguments[i] = new Argument(argName, pos(source), makeType(binding.parameters[i], source, false), ClassFileConstants.AccFinal); setGeneratedBy(method.arguments[i], source); call.arguments[i] = new SingleNameReference(argName, pos(source)); setGeneratedBy(call.arguments[i], source); } if (isVarargs) { method.arguments[method.arguments.length - 1].type.bits |= ASTNode.IsVarArgs; } } Statement body; if (method.returnType instanceof SingleTypeReference && ((SingleTypeReference) method.returnType).token == TypeConstants.VOID) { body = call; } else { body = new ReturnStatement(call, source.sourceStart, source.sourceEnd); setGeneratedBy(body, source); } method.statements = new Statement[] { body }; return method; }
From source file:lombok.eclipse.handlers.ast.EclipseASTMaker.java
License:Open Source License
@Override public ASTNode visitMethodDecl(final lombok.ast.MethodDecl node, final Void p) { MethodDeclaration methodDeclaration = new MethodDeclaration( ((CompilationUnitDeclaration) sourceNode.top().get()).compilationResult); setGeneratedByAndCopyPos(methodDeclaration, source, posHintOf(node)); methodDeclaration.modifiers = modifiersFor(node.getModifiers()); methodDeclaration.returnType = build(node.getReturnType(), TypeReference.class); methodDeclaration.annotations = toArray(build(node.getAnnotations()), new Annotation[0]); methodDeclaration.selector = node.getName().toCharArray(); methodDeclaration.thrownExceptions = toArray(build(node.getThrownExceptions()), new TypeReference[0]); methodDeclaration.typeParameters = toArray(build(node.getTypeParameters()), new TypeParameter[0]); methodDeclaration.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; methodDeclaration.arguments = toArray(build(node.getArguments()), new Argument[0]); if (node.isImplementing()) methodDeclaration.modifiers |= AccImplementing; if (node.noBody() || ((methodDeclaration.modifiers & AccAbstract) != 0)) { methodDeclaration.modifiers |= AccSemicolonBody; } else {//from w ww . j a v a 2 s.co m methodDeclaration.statements = toArray(build(node.getStatements()), new Statement[0]); } methodDeclaration.javadoc = build(node.getJavaDoc()); return methodDeclaration; }
From source file:lombok.eclipse.handlers.ast.EclipseASTMaker.java
License:Open Source License
@Override public ASTNode visitWrappedMethodDecl(final lombok.ast.WrappedMethodDecl node, final Void p) { MethodDeclaration methodDeclaration = new MethodDeclaration( ((CompilationUnitDeclaration) sourceNode.top().get()).compilationResult); setGeneratedByAndCopyPos(methodDeclaration, source, posHintOf(node)); MethodBinding abstractMethod = (MethodBinding) node.getWrappedObject(); if (node.getReturnType() == null) { node.withReturnType(Type(abstractMethod.returnType)); }//from w w w . ja v a 2s . com if (node.getThrownExceptions().isEmpty()) for (ReferenceBinding thrownException : Each.elementIn(abstractMethod.thrownExceptions)) { node.withThrownException(Type(thrownException)); } if (node.getArguments().isEmpty() && Is.notEmpty(abstractMethod.parameters)) for (int i = 0; i < abstractMethod.parameters.length; i++) { node.withArgument(Arg(Type(abstractMethod.parameters[i]), "arg" + i)); } if (node.getTypeParameters().isEmpty()) for (TypeVariableBinding binding : Each.elementIn(abstractMethod.typeVariables)) { ReferenceBinding super1 = binding.superclass; ReferenceBinding[] super2 = binding.superInterfaces; lombok.ast.TypeParam typeParameter = TypeParam(As.string(binding.sourceName)); if (super2 == null) super2 = new ReferenceBinding[0]; if (super1 != null || super2.length > 0) { if (super1 != null) typeParameter.withBound(Type(super1)); for (ReferenceBinding bound : super2) { typeParameter.withBound(Type(bound).makeSuperType()); } } node.withTypeParameter(typeParameter); } methodDeclaration.modifiers = (abstractMethod.getAccessFlags() & (~AccAbstract)); methodDeclaration.returnType = build(node.getReturnType(), TypeReference.class); methodDeclaration.annotations = toArray(build(node.getAnnotations()), new Annotation[0]); methodDeclaration.selector = abstractMethod.selector; methodDeclaration.thrownExceptions = toArray(build(node.getThrownExceptions()), new TypeReference[0]); methodDeclaration.typeParameters = toArray(build(node.getTypeParameters()), new TypeParameter[0]); methodDeclaration.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; methodDeclaration.arguments = toArray(build(node.getArguments()), new Argument[0]); if (node.isImplementing()) methodDeclaration.modifiers |= AccImplementing; if (node.noBody()) { methodDeclaration.modifiers |= AccSemicolonBody; } else { methodDeclaration.statements = toArray(build(node.getStatements()), new Statement[0]); } return methodDeclaration; }
From source file:lombok.eclipse.handlers.HandleBuilder.java
License:Open Source License
private MethodDeclaration generateToBuilderMethod(String methodName, String builderClassName, EclipseNode type, TypeParameter[] typeParams, List<BuilderFieldData> builderFields, boolean fluent, ASTNode source) { // return new ThingieBuilder<A, B>().setA(this.a).setB(this.b); int pS = source.sourceStart, pE = source.sourceEnd; long p = (long) pS << 32 | pE; MethodDeclaration out = new MethodDeclaration( ((CompilationUnitDeclaration) type.top().get()).compilationResult); out.selector = methodName.toCharArray(); out.modifiers = ClassFileConstants.AccPublic; out.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; out.returnType = namePlusTypeParamsToTypeReference(builderClassName.toCharArray(), typeParams, p); AllocationExpression invoke = new AllocationExpression(); invoke.type = namePlusTypeParamsToTypeReference(builderClassName.toCharArray(), typeParams, p); Expression receiver = invoke; for (BuilderFieldData bfd : builderFields) { char[] setterName = fluent ? bfd.name : HandlerUtil.buildAccessorName("set", new String(bfd.name)).toCharArray(); MessageSend ms = new MessageSend(); if (bfd.obtainVia == null || !bfd.obtainVia.field().isEmpty()) { char[] fieldName = bfd.obtainVia == null ? bfd.rawName : bfd.obtainVia.field().toCharArray(); FieldReference fr = new FieldReference(fieldName, 0); fr.receiver = new ThisReference(0, 0); ms.arguments = new Expression[] { fr }; } else {/*from www. j a v a2 s . c om*/ String obtainName = bfd.obtainVia.method(); boolean obtainIsStatic = bfd.obtainVia.isStatic(); MessageSend obtainExpr = new MessageSend(); obtainExpr.receiver = obtainIsStatic ? new SingleNameReference(type.getName().toCharArray(), 0) : new ThisReference(0, 0); obtainExpr.selector = obtainName.toCharArray(); if (obtainIsStatic) obtainExpr.arguments = new Expression[] { new ThisReference(0, 0) }; ms.arguments = new Expression[] { obtainExpr }; } ms.receiver = receiver; ms.selector = setterName; receiver = ms; } out.statements = new Statement[] { new ReturnStatement(receiver, pS, pE) }; out.traverse(new SetGeneratedByVisitor(source), ((TypeDeclaration) type.get()).scope); return out; }
From source file:lombok.eclipse.handlers.HandleBuilder.java
License:Open Source License
private MethodDeclaration generateCleanMethod(List<BuilderFieldData> builderFields, EclipseNode builderType, ASTNode source) {//from w w w.ja va 2 s.co m List<Statement> statements = new ArrayList<Statement>(); for (BuilderFieldData bfd : builderFields) { if (bfd.singularData != null && bfd.singularData.getSingularizer() != null) { bfd.singularData.getSingularizer().appendCleaningCode(bfd.singularData, builderType, statements); } } FieldReference thisUnclean = new FieldReference(CLEAN_FIELD_NAME, 0); thisUnclean.receiver = new ThisReference(0, 0); statements.add(new Assignment(thisUnclean, new FalseLiteral(0, 0), 0)); MethodDeclaration decl = new MethodDeclaration( ((CompilationUnitDeclaration) builderType.top().get()).compilationResult); decl.selector = CLEAN_METHOD_NAME; decl.modifiers = ClassFileConstants.AccPrivate; decl.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; decl.returnType = TypeReference.baseTypeReference(TypeIds.T_void, 0); decl.statements = statements.toArray(new Statement[0]); decl.traverse(new SetGeneratedByVisitor(source), (ClassScope) null); return decl; }
From source file:lombok.eclipse.handlers.HandleBuilder.java
License:Open Source License
public MethodDeclaration generateBuildMethod(String name, char[] staticName, TypeReference returnType, List<BuilderFieldData> builderFields, EclipseNode type, TypeReference[] thrownExceptions, boolean addCleaning, ASTNode source) { MethodDeclaration out = new MethodDeclaration( ((CompilationUnitDeclaration) type.top().get()).compilationResult); out.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; List<Statement> statements = new ArrayList<Statement>(); if (addCleaning) { FieldReference thisUnclean = new FieldReference(CLEAN_FIELD_NAME, 0); thisUnclean.receiver = new ThisReference(0, 0); Expression notClean = new UnaryExpression(thisUnclean, OperatorIds.NOT); MessageSend invokeClean = new MessageSend(); invokeClean.selector = CLEAN_METHOD_NAME; statements.add(new IfStatement(notClean, invokeClean, 0, 0)); }/*from ww w .java2 s .c o m*/ for (BuilderFieldData bfd : builderFields) { if (bfd.singularData != null && bfd.singularData.getSingularizer() != null) { bfd.singularData.getSingularizer().appendBuildCode(bfd.singularData, type, statements, bfd.name); } } List<Expression> args = new ArrayList<Expression>(); for (BuilderFieldData bfd : builderFields) { args.add(new SingleNameReference(bfd.name, 0L)); } if (addCleaning) { FieldReference thisUnclean = new FieldReference(CLEAN_FIELD_NAME, 0); thisUnclean.receiver = new ThisReference(0, 0); statements.add(new Assignment(thisUnclean, new TrueLiteral(0, 0), 0)); } out.modifiers = ClassFileConstants.AccPublic; out.selector = name.toCharArray(); out.thrownExceptions = copyTypes(thrownExceptions); out.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; out.returnType = returnType; if (staticName == null) { AllocationExpression allocationStatement = new AllocationExpression(); allocationStatement.type = copyType(out.returnType); allocationStatement.arguments = args.isEmpty() ? null : args.toArray(new Expression[args.size()]); statements.add(new ReturnStatement(allocationStatement, 0, 0)); } else { MessageSend invoke = new MessageSend(); invoke.selector = staticName; invoke.receiver = new SingleNameReference(type.up().getName().toCharArray(), 0); TypeParameter[] tps = ((TypeDeclaration) type.get()).typeParameters; if (tps != null) { TypeReference[] trs = new TypeReference[tps.length]; for (int i = 0; i < trs.length; i++) { trs[i] = new SingleTypeReference(tps[i].name, 0); } invoke.typeArguments = trs; } invoke.arguments = args.isEmpty() ? null : args.toArray(new Expression[args.size()]); if (returnType instanceof SingleTypeReference && Arrays.equals(TypeConstants.VOID, ((SingleTypeReference) returnType).token)) { statements.add(invoke); } else { statements.add(new ReturnStatement(invoke, 0, 0)); } } out.statements = statements.isEmpty() ? null : statements.toArray(new Statement[statements.size()]); out.traverse(new SetGeneratedByVisitor(source), (ClassScope) null); return out; }
From source file:lombok.eclipse.handlers.HandleBuilder.java
License:Open Source License
public MethodDeclaration generateBuilderMethod(String builderMethodName, String builderClassName, EclipseNode type, TypeParameter[] typeParams, ASTNode source) { int pS = source.sourceStart, pE = source.sourceEnd; long p = (long) pS << 32 | pE; MethodDeclaration out = new MethodDeclaration( ((CompilationUnitDeclaration) type.top().get()).compilationResult); out.selector = builderMethodName.toCharArray(); out.modifiers = ClassFileConstants.AccPublic | ClassFileConstants.AccStatic; out.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; out.returnType = namePlusTypeParamsToTypeReference(builderClassName.toCharArray(), typeParams, p); out.typeParameters = copyTypeParams(typeParams, source); AllocationExpression invoke = new AllocationExpression(); invoke.type = namePlusTypeParamsToTypeReference(builderClassName.toCharArray(), typeParams, p); out.statements = new Statement[] { new ReturnStatement(invoke, pS, pE) }; out.traverse(new SetGeneratedByVisitor(source), ((TypeDeclaration) type.get()).scope); return out;//from ww w . ja v a 2 s .c o m }