/** {@inheritDoc} */ @Override protected EclipseNode buildTree(ASTNode node, Kind kind) { switch (kind) { case COMPILATION_UNIT: return buildCompilationUnit((CompilationUnitDeclaration) node); case TYPE: return buildType((TypeDeclaration) node); case FIELD: return buildField((FieldDeclaration) node); case INITIALIZER: return buildInitializer((Initializer) node); case METHOD: return buildMethod((AbstractMethodDeclaration) node); case ARGUMENT: return buildLocal((Argument) node, kind); case LOCAL: return buildLocal((LocalDeclaration) node, kind); case STATEMENT: return buildStatement((Statement) node); case ANNOTATION: return buildAnnotation((Annotation) node, false); default: throw new AssertionError("Did not expect to arrive here: " + kind); } }
@Override public boolean visit(Argument argument, BlockScope scope) { Annotation[] annotations = argument.annotations; ReferenceContext referenceContext = scope.referenceContext(); if (referenceContext instanceof AbstractMethodDeclaration) { MethodBinding binding = ((AbstractMethodDeclaration) referenceContext).binding; if (binding != null) { TypeDeclaration typeDeclaration = scope.referenceType(); typeDeclaration.binding.resolveTypesFor(binding); if (argument.binding != null) { argument.binding = new AptSourceLocalVariableBinding(argument.binding, binding); } } if (annotations != null) { this.resolveAnnotations( scope, annotations, argument.binding); } } return false; }
protected Argument typeElidedArgument() { char[] selector = this.identifierStack[this.identifierPtr]; if (selector != assistIdentifier()){ return super.typeElidedArgument(); } this.identifierLengthPtr--; char[] identifierName = this.identifierStack[this.identifierPtr]; long namePositions = this.identifierPositionStack[this.identifierPtr--]; Argument argument = new SelectionOnArgumentName( identifierName, namePositions, null, // elided type ClassFileConstants.AccDefault, true); argument.declarationSourceStart = (int) (namePositions >>> 32); this.assistNode = argument; return argument; }
protected char[][][] getArguments(Argument[] arguments) { int argumentLength = arguments.length; char[][] argumentTypes = new char[argumentLength][]; char[][] argumentNames = new char[argumentLength][]; int argumentCount = 0; next : for (int i = 0; i < argumentLength; i++) { Argument argument = arguments[i]; if (argument instanceof CompletionOnArgumentName && argument.name.length == 0) continue next; argumentTypes[argumentCount] = CharOperation.concatWith(argument.type.getParameterizedTypeName(), '.'); argumentNames[argumentCount++] = argument.name; } if (argumentCount < argumentLength) { System.arraycopy(argumentTypes, 0, argumentTypes = new char[argumentCount][], 0, argumentCount); System.arraycopy(argumentNames, 0, argumentNames = new char[argumentCount][], 0, argumentCount); } return new char[][][] {argumentTypes, argumentNames}; }
protected Object[][] getArgumentInfos(Argument[] arguments) { int argumentLength = arguments.length; char[][] argumentTypes = new char[argumentLength][]; char[][] argumentNames = new char[argumentLength][]; ParameterInfo[] parameterInfos = new ParameterInfo[argumentLength]; for (int i = 0; i < argumentLength; i++) { Argument argument = arguments[i]; argumentTypes[i] = CharOperation.concatWith(argument.type.getParameterizedTypeName(), '.'); char[] name = argument.name; argumentNames[i] = name; ParameterInfo parameterInfo = new ParameterInfo(); parameterInfo.declarationStart = argument.declarationSourceStart; parameterInfo.declarationEnd = argument.declarationSourceEnd; parameterInfo.nameSourceStart = argument.sourceStart; parameterInfo.nameSourceEnd = argument.sourceEnd; parameterInfo.modifiers = argument.modifiers; parameterInfo.name = name; parameterInfos[i] = parameterInfo; } return new Object[][] { parameterInfos, new char[][][] { argumentTypes, argumentNames } }; }
public void parameterLackingNonnullAnnotation(Argument argument, ReferenceBinding declaringClass, char[][] inheritedAnnotationName) { int sourceStart = 0, sourceEnd = 0; if (argument != null) { sourceStart = argument.type.sourceStart; sourceEnd = argument.type.sourceEnd; } else if (this.referenceContext instanceof TypeDeclaration) { sourceStart = ((TypeDeclaration) this.referenceContext).sourceStart; sourceEnd = ((TypeDeclaration) this.referenceContext).sourceEnd; } this.handle( IProblem.ParameterLackingNonNullAnnotation, new String[] { new String(declaringClass.readableName()), CharOperation.toString(inheritedAnnotationName)}, new String[] { new String(declaringClass.shortReadableName()), new String(inheritedAnnotationName[inheritedAnnotationName.length-1])}, sourceStart, sourceEnd); }
@Override public boolean visit(ConstructorDeclaration constructorDeclaration, ClassScope scope) { Annotation[] annotations = constructorDeclaration.annotations; if (annotations != null) { MethodBinding constructorBinding = constructorDeclaration.binding; if (constructorBinding == null) { return false; } ((SourceTypeBinding) constructorBinding.declaringClass).resolveTypesFor(constructorBinding); this.resolveAnnotations( constructorDeclaration.scope, annotations, constructorBinding); } Argument[] arguments = constructorDeclaration.arguments; if (arguments != null) { int argumentLength = arguments.length; for (int i = 0; i < argumentLength; i++) { arguments[i].traverse(this, constructorDeclaration.scope); } } return false; }
@Override public boolean visit(MethodDeclaration methodDeclaration, ClassScope scope) { Annotation[] annotations = methodDeclaration.annotations; if (annotations != null) { MethodBinding methodBinding = methodDeclaration.binding; if (methodBinding == null) { return false; } ((SourceTypeBinding) methodBinding.declaringClass).resolveTypesFor(methodBinding); this.resolveAnnotations( methodDeclaration.scope, annotations, methodBinding); } Argument[] arguments = methodDeclaration.arguments; if (arguments != null) { int argumentLength = arguments.length; for (int i = 0; i < argumentLength; i++) { arguments[i].traverse(this, methodDeclaration.scope); } } return false; }
@Override public void endVisit(TryStatement x, BlockScope scope) { try { SourceInfo info = makeSourceInfo(x); JBlock finallyBlock = pop(x.finallyBlock); List<JBlock> catchBlocks = pop(x.catchBlocks); JBlock tryBlock = pop(x.tryBlock); List<JLocalRef> catchArgs = new ArrayList<JLocalRef>(); if (x.catchBlocks != null) { for (Argument argument : x.catchArguments) { JLocal local = (JLocal) curMethod.locals.get(argument.binding); catchArgs.add(new JLocalRef(info, local)); } } push(new JTryStatement(info, tryBlock, catchArgs, catchBlocks, finallyBlock)); } catch (Throwable e) { throw translateException(x, e); } }
private Collection<EclipseNode> buildArguments(Argument[] children) { List<EclipseNode> childNodes = new ArrayList<EclipseNode>(); if (children != null) for (LocalDeclaration local : children) { addIfNotNull(childNodes, buildLocal(local, Kind.ARGUMENT)); } return childNodes; }
void generateSingularMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType, boolean fluent) { LombokImmutableList<String> suffixes = getArgumentSuffixes(); char[][] names = new char[suffixes.size()][]; for (int i = 0; i < suffixes.size(); i++) { String s = suffixes.get(i); char[] n = data.getSingularName(); names[i] = s.isEmpty() ? n : s.toCharArray(); } MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult); md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; md.modifiers = ClassFileConstants.AccPublic; List<Statement> statements = new ArrayList<Statement>(); statements.add(createConstructBuilderVarIfNeeded(data, builderType)); FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L); thisDotField.receiver = new ThisReference(0, 0); MessageSend thisDotFieldDotAdd = new MessageSend(); thisDotFieldDotAdd.arguments = new Expression[suffixes.size()]; for (int i = 0; i < suffixes.size(); i++) { thisDotFieldDotAdd.arguments[i] = new SingleNameReference(names[i], 0L); } thisDotFieldDotAdd.receiver = thisDotField; thisDotFieldDotAdd.selector = getAddMethodName().toCharArray(); statements.add(thisDotFieldDotAdd); if (returnStatement != null) statements.add(returnStatement); md.statements = statements.toArray(new Statement[statements.size()]); md.arguments = new Argument[suffixes.size()]; for (int i = 0; i < suffixes.size(); i++) { TypeReference tr = cloneParamType(i, data.getTypeArgs(), builderType); md.arguments[i] = new Argument(names[i], 0, tr, 0); } md.returnType = returnType; md.selector = fluent ? data.getSingularName() : HandlerUtil.buildAccessorName(getAddMethodName(), new String(data.getSingularName())).toCharArray(); data.setGeneratedByRecursive(md); injectMethod(builderType, md); }
void generateSingularMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType, boolean fluent) { MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult); md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; md.modifiers = ClassFileConstants.AccPublic; List<Statement> statements = new ArrayList<Statement>(); statements.add(createConstructBuilderVarIfNeeded(data, builderType, false)); FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L); thisDotField.receiver = new ThisReference(0, 0); MessageSend thisDotFieldDotAdd = new MessageSend(); thisDotFieldDotAdd.arguments = new Expression[] {new SingleNameReference(data.getSingularName(), 0L)}; thisDotFieldDotAdd.receiver = thisDotField; thisDotFieldDotAdd.selector = "add".toCharArray(); statements.add(thisDotFieldDotAdd); if (returnStatement != null) statements.add(returnStatement); md.statements = statements.toArray(new Statement[statements.size()]); TypeReference paramType = cloneParamType(0, data.getTypeArgs(), builderType); Argument param = new Argument(data.getSingularName(), 0, paramType, 0); md.arguments = new Argument[] {param}; md.returnType = returnType; md.selector = fluent ? data.getSingularName() : HandlerUtil.buildAccessorName("add", new String(data.getSingularName())).toCharArray(); data.setGeneratedByRecursive(md); injectMethod(builderType, md); }
void generatePluralMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType, boolean fluent) { MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult); md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; md.modifiers = ClassFileConstants.AccPublic; List<Statement> statements = new ArrayList<Statement>(); statements.add(createConstructBuilderVarIfNeeded(data, builderType, false)); FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L); thisDotField.receiver = new ThisReference(0, 0); MessageSend thisDotFieldDotAddAll = new MessageSend(); thisDotFieldDotAddAll.arguments = new Expression[] {new SingleNameReference(data.getPluralName(), 0L)}; thisDotFieldDotAddAll.receiver = thisDotField; thisDotFieldDotAddAll.selector = "addAll".toCharArray(); statements.add(thisDotFieldDotAddAll); if (returnStatement != null) statements.add(returnStatement); md.statements = statements.toArray(new Statement[statements.size()]); TypeReference paramType = new QualifiedTypeReference(TypeConstants.JAVA_UTIL_COLLECTION, NULL_POSS); paramType = addTypeArgs(1, true, builderType, paramType, data.getTypeArgs()); Argument param = new Argument(data.getPluralName(), 0, paramType, 0); md.arguments = new Argument[] {param}; md.returnType = returnType; md.selector = fluent ? data.getPluralName() : HandlerUtil.buildAccessorName("addAll", new String(data.getPluralName())).toCharArray(); data.setGeneratedByRecursive(md); injectMethod(builderType, md); }
public static String[] typeParameterSignatures(AbstractMethodDeclaration method) { Argument[] args = method.arguments; if (args != null) { int length = args.length; String[] signatures = new String[length]; for (int i = 0; i < args.length; i++) { Argument arg = args[i]; signatures[i] = typeSignature(arg.type); } return signatures; } return CharOperation.NO_STRINGS; }
@Override public boolean visit(ConstructorDeclaration constructorDeclaration, ClassScope scope) { Annotation[] annotations = constructorDeclaration.annotations; if (annotations != null) { MethodBinding constructorBinding = constructorDeclaration.binding; if (constructorBinding == null) { return false; } ((SourceTypeBinding) constructorBinding.declaringClass).resolveTypesFor(constructorBinding); this.resolveAnnotations( constructorDeclaration.scope, annotations, constructorBinding); } TypeParameter[] typeParameters = constructorDeclaration.typeParameters; if (typeParameters != null) { int typeParametersLength = typeParameters.length; for (int i = 0; i < typeParametersLength; i++) { typeParameters[i].traverse(this, constructorDeclaration.scope); } } Argument[] arguments = constructorDeclaration.arguments; if (arguments != null) { int argumentLength = arguments.length; for (int i = 0; i < argumentLength; i++) { arguments[i].traverse(this, constructorDeclaration.scope); } } return false; }
@Override public boolean visit(MethodDeclaration methodDeclaration, ClassScope scope) { Annotation[] annotations = methodDeclaration.annotations; if (annotations != null) { MethodBinding methodBinding = methodDeclaration.binding; if (methodBinding == null) { return false; } ((SourceTypeBinding) methodBinding.declaringClass).resolveTypesFor(methodBinding); this.resolveAnnotations( methodDeclaration.scope, annotations, methodBinding); } TypeParameter[] typeParameters = methodDeclaration.typeParameters; if (typeParameters != null) { int typeParametersLength = typeParameters.length; for (int i = 0; i < typeParametersLength; i++) { typeParameters[i].traverse(this, methodDeclaration.scope); } } Argument[] arguments = methodDeclaration.arguments; if (arguments != null) { int argumentLength = arguments.length; for (int i = 0; i < argumentLength; i++) { arguments[i].traverse(this, methodDeclaration.scope); } } return false; }
private LocalVariable[] acceptMethodParameters(Argument[] arguments, JavaElement methodHandle, MethodInfo methodInfo) { if (arguments == null) return null; LocalVariable[] result = new LocalVariable[arguments.length]; Annotation[][] paramAnnotations = new Annotation[arguments.length][]; for(int i = 0; i < arguments.length; i++) { Argument argument = arguments[i]; AnnotatableInfo localVarInfo = new AnnotatableInfo(); localVarInfo.setSourceRangeStart(argument.declarationSourceStart); localVarInfo.setSourceRangeEnd(argument.declarationSourceStart); localVarInfo.setNameSourceStart(argument.sourceStart); localVarInfo.setNameSourceEnd(argument.sourceEnd); String paramTypeSig = JavaModelManager.getJavaModelManager().intern(Signature.createTypeSignature(methodInfo.parameterTypes[i], false)); result[i] = new LocalVariable( methodHandle, new String(argument.name), argument.declarationSourceStart, argument.declarationSourceEnd, argument.sourceStart, argument.sourceEnd, paramTypeSig, argument.annotations, argument.modifiers, true); this.newElements.put(result[i], localVarInfo); this.infoStack.push(localVarInfo); this.handleStack.push(result[i]); if (argument.annotations != null) { paramAnnotations[i] = new Annotation[argument.annotations.length]; for (int j = 0; j < argument.annotations.length; j++ ) { org.eclipse.jdt.internal.compiler.ast.Annotation annotation = argument.annotations[j]; acceptAnnotation(annotation, localVarInfo, result[i]); } } this.infoStack.pop(); this.handleStack.pop(); } return result; }
private void completeArgumentAnnotationInfo(Argument[] arguments, List allAnnotationContexts) { for (int i = 0, max = arguments.length; i < max; i++) { Argument argument = arguments[i]; if ((argument.bits & ASTNode.HasTypeAnnotations) != 0) { argument.getAllAnnotationContexts(AnnotationTargetTypeConstants.METHOD_FORMAL_PARAMETER, i, allAnnotationContexts); } } }
protected void consumeCatchHeader() { // CatchDeclaration ::= 'catch' '(' FormalParameter ')' '{' if (this.currentElement == null){ return; // should never occur, this consumeRule is only used in recovery mode } // current element should be a block due to the presence of the opening brace if (!(this.currentElement instanceof RecoveredBlock)){ if(!(this.currentElement instanceof RecoveredMethod)) { return; } RecoveredMethod rMethod = (RecoveredMethod) this.currentElement; if(!(rMethod.methodBody == null && rMethod.bracketBalance > 0)) { return; } } Argument arg = (Argument)this.astStack[this.astPtr--]; // convert argument to local variable LocalDeclaration localDeclaration = new LocalDeclaration(arg.name, arg.sourceStart, arg.sourceEnd); localDeclaration.type = arg.type; localDeclaration.declarationSourceStart = arg.declarationSourceStart; localDeclaration.declarationSourceEnd = arg.declarationSourceEnd; this.currentElement = this.currentElement.add(localDeclaration, 0); this.lastCheckPoint = this.scanner.startPosition; // force to restart at this exact position this.restartRecovery = true; // request to restart from here on this.lastIgnoredToken = -1; }
protected void consumeLambdaHeader() { // LambdaHeader ::= LambdaParameters '->' Synthetic/fake production with a synthetic non-terminal. Body not seen yet. int arrowPosition = this.scanner.currentPosition - 1; Argument [] arguments = null; int length = this.astLengthStack[this.astLengthPtr--]; this.astPtr -= length; //arguments if (length != 0) { System.arraycopy( this.astStack, this.astPtr + 1, arguments = new Argument[length], 0, length); } for (int i = 0; i < length; i++) { final Argument argument = arguments[i]; if (argument.isReceiver()) { problemReporter().illegalThis(argument); } if (argument.name.length == 1 && argument.name[0] == '_') problemReporter().illegalUseOfUnderscoreAsAnIdentifier(argument.sourceStart, argument.sourceEnd, true); // true == lambdaParameter } LambdaExpression lexp = (LambdaExpression) this.astStack[this.astPtr]; lexp.setArguments(arguments); lexp.setArrowPosition(arrowPosition); lexp.sourceEnd = this.intStack[this.intPtr--]; // ')' position or identifier position. lexp.sourceStart = this.intStack[this.intPtr--]; // '(' position or identifier position. lexp.hasParentheses = (this.scanner.getSource()[lexp.sourceStart] == '('); this.listLength -= arguments == null ? 0 : arguments.length; // not necessary really. this.processingLambdaParameterList = false; if (this.currentElement != null) { this.lastCheckPoint = arrowPosition + 1; // we don't want the typed formal parameters to be processed by recovery. } }
protected Argument typeElidedArgument() { this.identifierLengthPtr--; char[] identifierName = this.identifierStack[this.identifierPtr]; long namePositions = this.identifierPositionStack[this.identifierPtr--]; Argument arg = new Argument( identifierName, namePositions, null, // elided type ClassFileConstants.AccDefault, true); arg.declarationSourceStart = (int) (namePositions >>> 32); return arg; }
protected void consumeTypeElidedLambdaParameter(boolean parenthesized) { // LambdaParameters ::= Identifier // TypeElidedFormalParameter ::= Modifiersopt Identifier int modifier = ClassFileConstants.AccDefault; int annotationLength = 0; int modifiersStart = 0; if (parenthesized) { // The grammar is permissive enough to allow optional modifiers for the parenthesized version, they should be rejected if present. modifiersStart = this.intStack[this.intPtr--]; modifier = this.intStack[this.intPtr--]; // pop annotations annotationLength = this.expressionLengthStack[this.expressionLengthPtr--]; this.expressionPtr -= annotationLength; } Argument arg = typeElidedArgument(); if (modifier != ClassFileConstants.AccDefault || annotationLength != 0) { problemReporter().illegalModifiersForElidedType(arg); arg.declarationSourceStart = modifiersStart; } if (!parenthesized) { // in the absence of '(' and ')', record positions. pushOnIntStack(arg.declarationSourceStart); pushOnIntStack(arg.declarationSourceEnd); } pushOnAstStack(arg); this.listLength++; // not relevant really. }
public void argumentTypeCannotBeVoid(ASTNode methodDecl, Argument arg) { String[] arguments = new String[] { new String(arg.name) }; this.handle( IProblem.ArgumentTypeCannotBeVoid, arguments, arguments, methodDecl.sourceStart, methodDecl.sourceEnd); }
public void argumentTypeCannotBeVoidArray(Argument arg) { this.handle( IProblem.CannotAllocateVoidArray, NoArgument, NoArgument, arg.type.sourceStart, arg.type.sourceEnd); }
public void lambdaParameterTypeMismatched(Argument argument, TypeReference type, TypeBinding expectedParameterType) { String name = new String(argument.name); String expectedTypeFullName = new String(expectedParameterType.readableName()); String expectedTypeShortName = new String(expectedParameterType.shortReadableName()); this.handle( expectedParameterType.isTypeVariable() ? IProblem.IncompatibleLambdaParameterType : IProblem.lambdaParameterTypeMismatched, new String[] { name, expectedTypeFullName }, new String[] { name, expectedTypeShortName }, type.sourceStart, type.sourceEnd); }
public void illegalExtendedDimensions(Argument argument) { this.handle( IProblem.IllegalExtendedDimensionsForVarArgs, NoArgument, NoArgument, argument.sourceStart, argument.sourceEnd); }
public void illegalVararg(Argument argType, AbstractMethodDeclaration methodDecl) { String[] arguments = new String[] {CharOperation.toString(argType.type.getTypeName()), new String(methodDecl.selector)}; this.handle( IProblem.IllegalVararg, arguments, arguments, argType.sourceStart, argType.sourceEnd); }
public void illegalVarargInLambda(Argument argType) { String[] arguments = new String[] { CharOperation.toString(argType.type.getTypeName())}; this.handle( IProblem.IllegalVarargInLambda, arguments, arguments, argType.sourceStart, argType.sourceEnd); }
public void illegalThisDeclaration(Argument argument) { String[] arguments = NoArgument; this.handle( IProblem.IllegalDeclarationOfThisParameter, arguments, arguments, argument.sourceStart, argument.sourceEnd); }
public void illegalSourceLevelForThis(Argument argument) { String[] arguments = NoArgument; this.handle( IProblem.ExplicitThisParameterNotBelow18, arguments, arguments, argument.sourceStart, argument.sourceEnd); }
public void illegalThis(Argument argument) { String[] arguments = NoArgument; this.handle( IProblem.ExplicitThisParameterNotInLambda, arguments, arguments, argument.sourceStart, argument.sourceEnd); }