List of usage examples for org.eclipse.jdt.internal.compiler.ast QualifiedAllocationExpression QualifiedAllocationExpression
public QualifiedAllocationExpression(TypeDeclaration anonymousType)
From source file:com.codenvy.ide.ext.java.server.internal.compiler.parser.SourceTypeConverter.java
License:Open Source License
private Initializer convert(InitializerElementInfo initializerInfo, CompilationResult compilationResult) throws JavaModelException { Block block = new Block(0); Initializer initializer = new Initializer(block, ClassFileConstants.AccDefault); int start = initializerInfo.getDeclarationSourceStart(); int end = initializerInfo.getDeclarationSourceEnd(); initializer.sourceStart = initializer.declarationSourceStart = start; initializer.sourceEnd = initializer.declarationSourceEnd = end; initializer.modifiers = initializerInfo.getModifiers(); /* convert local and anonymous types */ IJavaElement[] children = initializerInfo.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; }//from w ww. j a v a2s .c o m } block.statements = statements; } return initializer; }
From source file:com.codenvy.ide.ext.java.server.internal.compiler.parser.SourceTypeConverter.java
License:Open Source License
private QualifiedAllocationExpression convert(IJavaElement localType, FieldDeclaration enumConstant, CompilationResult compilationResult) throws JavaModelException { TypeDeclaration anonymousLocalTypeDeclaration = convert((SourceType) localType, compilationResult); QualifiedAllocationExpression expression = new QualifiedAllocationExpression(anonymousLocalTypeDeclaration); expression.type = anonymousLocalTypeDeclaration.superclass; anonymousLocalTypeDeclaration.superclass = null; anonymousLocalTypeDeclaration.superInterfaces = null; anonymousLocalTypeDeclaration.allocation = expression; if (enumConstant != null) { anonymousLocalTypeDeclaration.modifiers &= ~ClassFileConstants.AccEnum; expression.enumConstant = enumConstant; expression.type = null;/*from w w w .j a v a 2 s. c om*/ } return expression; }
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 .j a v a 2s .c o 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:lombok.eclipse.handlers.ast.EclipseASTMaker.java
License:Open Source License
@Override public ASTNode visitNew(final lombok.ast.New node, final Void p) { final AllocationExpression allocationExpression; if (node.getAnonymousType() != null) { allocationExpression = new QualifiedAllocationExpression( build(node.getAnonymousType(), TypeDeclaration.class)); } else {/*from www .j ava2s .c om*/ allocationExpression = new AllocationExpression(); } setGeneratedByAndCopyPos(allocationExpression, source, posHintOf(node)); allocationExpression.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; allocationExpression.type = build(node.getType()); allocationExpression.typeArguments = toArray(build(node.getTypeArgs()), new TypeReference[0]); allocationExpression.arguments = toArray(build(node.getArgs()), new Expression[0]); return allocationExpression; }
From source file:lombok.eclipse.handlers.HandleRelations.java
License:Open Source License
private static FieldDeclaration createField(Object anno, EclipseNode fieldNode, ASTNode source) { int pS = source.sourceStart, pE = source.sourceEnd; long p = (long) pS << 32 | pE; FieldDeclaration result = null;//from w w w. ja va 2 s .c o m FieldDeclaration fieldDecl = (FieldDeclaration) fieldNode.get(); String relatedFieldName = null; boolean isOneToOne = false; boolean isUnique = false; String baseTypeName = new String(((TypeDeclaration) fieldNode.up().get()).name); char[] qualifiedRelationTypeName = null; char[] singleRelationTypeName = null; TypeReference fieldType = null; TypeReference baseType = createTypeReference(baseTypeName.split("\\."), p); setGeneratedBy(baseType, source); TypeReference referenceType = fieldDecl.type; if (anno instanceof OneToOne) { isOneToOne = true; relatedFieldName = ((OneToOne) anno).field(); qualifiedRelationTypeName = OneToOneRelation.class.getName().toCharArray(); singleRelationTypeName = OneToOneRelation.class.getSimpleName().toCharArray(); } else { relatedFieldName = ((OneToMany) anno).field(); isUnique = ((OneToMany) anno).unique(); if (referenceType instanceof ParameterizedSingleTypeReference) { referenceType = ((ParameterizedSingleTypeReference) referenceType).typeArguments[0]; } else if (referenceType instanceof ParameterizedQualifiedTypeReference) { ParameterizedQualifiedTypeReference type = (ParameterizedQualifiedTypeReference) referenceType; referenceType = type.typeArguments[type.typeArguments.length - 1][0]; } qualifiedRelationTypeName = OneToManyRelation.class.getName().toCharArray(); singleRelationTypeName = OneToManyRelation.class.getSimpleName().toCharArray(); } addImportIfNotExists((CompilationUnitDeclaration) fieldNode.top().get(), qualifiedRelationTypeName, source); fieldType = new ParameterizedSingleTypeReference(singleRelationTypeName, new TypeReference[] { baseType, referenceType }, 0, p); setGeneratedBy(fieldType, source); fieldType.sourceStart = pS; fieldType.sourceEnd = fieldType.statementEnd = pE; CompilationResult compResult = ((CompilationUnitDeclaration) fieldNode.top().get()).compilationResult; final TypeDeclaration typeDeclaration = new TypeDeclaration(compResult); setGeneratedBy(typeDeclaration, source); typeDeclaration.name = CharOperation.NO_CHAR; typeDeclaration.bits |= (ASTNode.IsAnonymousType | ASTNode.IsLocalType); typeDeclaration.bodyStart = source.sourceStart; typeDeclaration.bodyEnd = source.sourceEnd; typeDeclaration.declarationSourceStart = source.sourceStart; typeDeclaration.declarationSourceEnd = source.sourceEnd; typeDeclaration.methods = new AbstractMethodDeclaration[] { createGetReferencedKeyMethod(source, relatedFieldName, isOneToOne, baseType, referenceType, compResult), createSetReferencedObjectMethod(fieldDecl, source, relatedFieldName, isOneToOne, isUnique, baseType, referenceType, (CompilationUnitDeclaration) fieldNode.top().get()), createSetRelatedIdMethod(source, relatedFieldName, isOneToOne, baseType, referenceType, compResult) }; typeDeclaration.addClinit(); QualifiedAllocationExpression allocation = new QualifiedAllocationExpression(typeDeclaration); setGeneratedBy(allocation, source); allocation.sourceStart = pS; allocation.sourceEnd = allocation.statementEnd = pE; allocation.type = fieldType; result = new FieldDeclaration(toUpperCase(new String(fieldDecl.name)).toCharArray(), 0, -1); setGeneratedBy(result, source); result.declarationSourceEnd = -1; result.type = fieldType; result.initialization = allocation; result.modifiers = ClassFileConstants.AccPrivate | ClassFileConstants.AccFinal | ClassFileConstants.AccStatic; return result; }
From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java
License:Open Source License
protected void consumeAllocationHeader() { // ClassInstanceCreationExpression ::= 'new' ClassType '(' ArgumentListopt ')' ClassBodyopt // ClassBodyopt produces a null item on the astStak if it produces NO class body // An empty class body produces a 0 on the length stack..... if (this.currentElement == null) { return; // should never occur, this consumeRule is only used in recovery mode }//from w w w. j av a2 s . com if (this.currentToken == TokenNameLBRACE) { // beginning of an anonymous type TypeDeclaration anonymousType = new TypeDeclaration(this.compilationUnit.compilationResult); anonymousType.name = CharOperation.NO_CHAR; anonymousType.bits |= (ASTNode.IsAnonymousType | ASTNode.IsLocalType); anonymousType.sourceStart = this.intStack[this.intPtr--]; anonymousType.declarationSourceStart = anonymousType.sourceStart; anonymousType.sourceEnd = this.rParenPos; // closing parenthesis QualifiedAllocationExpression alloc = new QualifiedAllocationExpression(anonymousType); alloc.type = getTypeReference(0); alloc.sourceStart = anonymousType.sourceStart; alloc.sourceEnd = anonymousType.sourceEnd; this.lastCheckPoint = anonymousType.bodyStart = this.scanner.currentPosition; this.currentElement = this.currentElement.add(anonymousType, 0); this.lastIgnoredToken = -1; this.currentToken = 0; // opening brace already taken into account return; } this.lastCheckPoint = this.scanner.startPosition; // force to restart at this exact position this.restartRecovery = true; // request to restart from here on }
From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java
License:Open Source License
protected void consumeEnterAnonymousClassBody(boolean qualified) { // EnterAnonymousClassBody ::= $empty TypeReference typeReference = getTypeReference(0); TypeDeclaration anonymousType = new TypeDeclaration(this.compilationUnit.compilationResult); anonymousType.name = CharOperation.NO_CHAR; anonymousType.bits |= (ASTNode.IsAnonymousType | ASTNode.IsLocalType); QualifiedAllocationExpression alloc = new QualifiedAllocationExpression(anonymousType); markEnclosingMemberWithLocalType();//from w w w . j av a 2s . co m pushOnAstStack(anonymousType); alloc.sourceEnd = this.rParenPos; //the position has been stored explicitly int argumentLength; if ((argumentLength = this.expressionLengthStack[this.expressionLengthPtr--]) != 0) { this.expressionPtr -= argumentLength; System.arraycopy(this.expressionStack, this.expressionPtr + 1, alloc.arguments = new Expression[argumentLength], 0, argumentLength); } if (qualified) { this.expressionLengthPtr--; alloc.enclosingInstance = this.expressionStack[this.expressionPtr--]; } alloc.type = typeReference; anonymousType.sourceEnd = alloc.sourceEnd; //position at the type while it impacts the anonymous declaration anonymousType.sourceStart = anonymousType.declarationSourceStart = alloc.type.sourceStart; alloc.sourceStart = this.intStack[this.intPtr--]; pushOnExpressionStack(alloc); anonymousType.bodyStart = this.scanner.currentPosition; this.listLength = 0; // will be updated when reading super-interfaces // flush the comments related to the anonymous this.scanner.commentPtr = -1; // recovery if (this.currentElement != null) { this.lastCheckPoint = anonymousType.bodyStart; this.currentElement = this.currentElement.add(anonymousType, 0); if (!(this.currentElement instanceof RecoveredAnnotation)) { this.currentToken = 0; // opening brace already taken into account } else { this.ignoreNextOpeningBrace = true; this.currentElement.bracketBalance++; } this.lastIgnoredToken = -1; } }
From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java
License:Open Source License
protected void consumeEnumConstantHeader() { FieldDeclaration enumConstant = (FieldDeclaration) this.astStack[this.astPtr]; boolean foundOpeningBrace = this.currentToken == TokenNameLBRACE; if (foundOpeningBrace) { // qualified allocation expression TypeDeclaration anonymousType = new TypeDeclaration(this.compilationUnit.compilationResult); anonymousType.name = CharOperation.NO_CHAR; anonymousType.bits |= (ASTNode.IsAnonymousType | ASTNode.IsLocalType); final int start = this.scanner.startPosition; anonymousType.declarationSourceStart = start; anonymousType.sourceStart = start; anonymousType.sourceEnd = start; // closing parenthesis anonymousType.modifiers = 0;/*from w ww. ja va 2s. c o m*/ anonymousType.bodyStart = this.scanner.currentPosition; markEnclosingMemberWithLocalType(); consumeNestedType(); this.variablesCounter[this.nestedType]++; pushOnAstStack(anonymousType); QualifiedAllocationExpression allocationExpression = new QualifiedAllocationExpression(anonymousType); allocationExpression.enumConstant = enumConstant; // fill arguments if needed int length; if ((length = this.expressionLengthStack[this.expressionLengthPtr--]) != 0) { this.expressionPtr -= length; System.arraycopy(this.expressionStack, this.expressionPtr + 1, allocationExpression.arguments = new Expression[length], 0, length); } enumConstant.initialization = allocationExpression; } else { AllocationExpression allocationExpression = new AllocationExpression(); allocationExpression.enumConstant = enumConstant; // fill arguments if needed int length; if ((length = this.expressionLengthStack[this.expressionLengthPtr--]) != 0) { this.expressionPtr -= length; System.arraycopy(this.expressionStack, this.expressionPtr + 1, allocationExpression.arguments = new Expression[length], 0, length); } enumConstant.initialization = allocationExpression; } // initialize the starting position of the allocation expression enumConstant.initialization.sourceStart = enumConstant.declarationSourceStart; // recovery if (this.currentElement != null) { if (foundOpeningBrace) { TypeDeclaration anonymousType = (TypeDeclaration) this.astStack[this.astPtr]; this.currentElement = this.currentElement.add(anonymousType, 0); this.lastCheckPoint = anonymousType.bodyStart; this.lastIgnoredToken = -1; this.currentToken = 0; // opening brace already taken into account } else { if (this.currentToken == TokenNameSEMICOLON) { RecoveredType currentType = currentRecoveryType(); if (currentType != null) { currentType.insideEnumConstantPart = false; } } this.lastCheckPoint = this.scanner.startPosition; // force to restart at this exact position this.lastIgnoredToken = -1; this.restartRecovery = true; } } }
From source file:org.eclipse.jdt.internal.compiler.parser.SourceTypeConverter.java
License:Open Source License
private AbstractMethodDeclaration convert(SourceMethod methodHandle, SourceMethodElementInfo methodInfo, CompilationResult compilationResult) throws JavaModelException { AbstractMethodDeclaration method;//from w ww. java 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) { 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 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; }