org.eclipse.jdt.internal.compiler.ast.NameReference Java Examples

The following examples show how to use org.eclipse.jdt.internal.compiler.ast.NameReference. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PatchExtensionMethodCompletionProposal.java    From EasyMPermission with MIT License 6 votes vote down vote up
static TypeBinding getFirstParameterType(TypeDeclaration decl, CompletionProposalCollector completionProposalCollector) {
		TypeBinding firstParameterType = null;
		ASTNode node = getAssistNode(completionProposalCollector);
		if (node == null) return null;
		if (!(node instanceof CompletionOnQualifiedNameReference) && !(node instanceof CompletionOnSingleNameReference) && !(node instanceof CompletionOnMemberAccess)) return null;
		
		// Never offer on 'super.<autocomplete>'.
		if (node instanceof FieldReference && ((FieldReference)node).receiver instanceof SuperReference) return null;
		
		if (node instanceof NameReference) {
			Binding binding = ((NameReference) node).binding;
			// Unremark next block to allow a 'blank' autocomplete to list any extensions that apply to the current scope, but make sure we're not in a static context first, which this doesn't do.
			// Lacking good use cases, and having this particular concept be a little tricky on javac, means for now we don't support extension methods like this. this.X() will be fine, though.
			
/*			if ((node instanceof SingleNameReference) && (((SingleNameReference) node).token.length == 0)) {
				firstParameterType = decl.binding;
			} else */if (binding instanceof VariableBinding) {
				firstParameterType = ((VariableBinding) binding).type;
			}
		} else if (node instanceof FieldReference) {
			firstParameterType = ((FieldReference) node).actualReceiverType;
		}
		return firstParameterType;
	}
 
Example #2
Source File: PatchExtensionMethod.java    From EasyMPermission with MIT License 6 votes vote down vote up
private static NameReference createNameRef(TypeBinding typeBinding, ASTNode source) {
	long p = ((long) source.sourceStart << 32) | source.sourceEnd;
	char[] pkg = typeBinding.qualifiedPackageName();
	char[] basename = typeBinding.qualifiedSourceName();
	
	StringBuilder sb = new StringBuilder();
	if (pkg != null) sb.append(pkg);
	if (sb.length() > 0) sb.append(".");
	sb.append(basename);
	
	String tName = sb.toString();
	
	if (tName.indexOf('.') == -1) {
		return new SingleNameReference(basename, p);
	} else {
		char[][] sources;
		String[] in = tName.split("\\.");
		sources = new char[in.length][];
		for (int i = 0; i < in.length; i++) sources[i] = in[i].toCharArray();
		long[] poss = new long[in.length];
		Arrays.fill(poss, p);
		return new QualifiedNameReference(sources, poss, source.sourceStart, source.sourceEnd);
	}
}
 
Example #3
Source File: Extractor.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unused")
static boolean hasSourceRetention(@NonNull Annotation[] annotations) {
    for (Annotation annotation : annotations) {
        String typeName = Extractor.getFqn(annotation);
        if ("java.lang.annotation.Retention".equals(typeName)) {
            MemberValuePair[] pairs = annotation.memberValuePairs();
            if (pairs == null || pairs.length != 1) {
                warning("Expected exactly one parameter passed to @Retention");
                return false;
            }
            MemberValuePair pair = pairs[0];
            Expression value = pair.value;
            if (value instanceof NameReference) {
                NameReference reference = (NameReference) value;
                Binding binding = reference.binding;
                if (binding != null) {
                    if (binding instanceof FieldBinding) {
                        FieldBinding fb = (FieldBinding) binding;
                        if ("SOURCE".equals(new String(fb.name)) &&
                                "java.lang.annotation.RetentionPolicy".equals(
                                        new String(fb.declaringClass.readableName()))) {
                            return true;
                        }
                    }
                }
            }
        }
    }

    return false;
}
 
Example #4
Source File: EclipseHandlerUtil.java    From EasyMPermission with MIT License 5 votes vote down vote up
static Expression createFieldAccessor(EclipseNode field, FieldAccess fieldAccess, ASTNode source, char[] receiver) {
	int pS = source.sourceStart, pE = source.sourceEnd;
	long p = (long)pS << 32 | pE;
	
	boolean lookForGetter = lookForGetter(field, fieldAccess);
	
	GetterMethod getter = lookForGetter ? findGetter(field) : null;
	
	if (getter == null) {
		NameReference ref;
		
		char[][] tokens = new char[2][];
		tokens[0] = receiver;
		tokens[1] = field.getName().toCharArray();
		long[] poss = {p, p};
		
		ref = new QualifiedNameReference(tokens, poss, pS, pE);
		setGeneratedBy(ref, source);
		return ref;
	}
	
	MessageSend call = new MessageSend();
	setGeneratedBy(call, source);
	call.sourceStart = pS; call.statementEnd = call.sourceEnd = pE;
	call.receiver = new SingleNameReference(receiver, p);
	setGeneratedBy(call.receiver, source);
	call.selector = getter.name;
	return call;
}
 
Example #5
Source File: EclipseHandlerUtil.java    From EasyMPermission with MIT License 5 votes vote down vote up
public static NameReference createNameReference(String name, Annotation source) {
	int pS = source.sourceStart, pE = source.sourceEnd;
	long p = (long)pS << 32 | pE;
	
	char[][] nameTokens = fromQualifiedName(name);
	long[] pos = new long[nameTokens.length];
	Arrays.fill(pos, p);
	
	QualifiedNameReference nameReference = new QualifiedNameReference(nameTokens, pos, pS, pE);
	nameReference.statementEnd = pE;
	
	setGeneratedBy(nameReference, source);
	return nameReference;
}
 
Example #6
Source File: SelectionParser.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected NameReference getUnspecifiedReferenceOptimized() {

	int index = indexOfAssistIdentifier();
	NameReference reference = super.getUnspecifiedReferenceOptimized();

	if (index >= 0){
		if (!this.diet){
			this.restartRecovery	= true;	// force to restart in recovery mode
			this.lastIgnoredToken = -1;
		}
		this.isOrphanCompletionNode = true;
	}
	return reference;
}
 
Example #7
Source File: JavaStatementPostfixContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Calculates the beginning position of a given {@link ASTNode}
 * @param node
 * @return
 */
protected int getNodeBegin(ASTNode node) {
	if (node instanceof NameReference) {
		return ((NameReference) node).sourceStart;
	} else if (node instanceof FieldReference) {
		return ((FieldReference) node).receiver.sourceStart;
	} else if (node instanceof MessageSend) {
		return ((MessageSend) node).receiver.sourceStart;
	}
	return node.sourceStart;
}
 
Example #8
Source File: JavaStatementPostfixContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected Binding resolveNodeToBinding(ASTNode node) {
	if (node instanceof NameReference) {
   		NameReference nr = (NameReference) node;
		if (nr.binding instanceof VariableBinding) {
			VariableBinding vb = (VariableBinding) nr.binding;
			return vb.type;
		}
	} else if (node instanceof FieldReference) {
   		FieldReference fr = (FieldReference) node;
		return fr.receiver.resolvedType;
	}
	return null;
}
 
Example #9
Source File: EcjParser.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
@Nullable
public ResolvedNode resolve(@NonNull JavaContext context, @NonNull Node node) {
    Object nativeNode = getNativeNode(node);
    if (nativeNode == null) {
        return null;
    }

    if (nativeNode instanceof NameReference) {
        return resolve(((NameReference) nativeNode).binding);
    } else if (nativeNode instanceof TypeReference) {
        return resolve(((TypeReference) nativeNode).resolvedType);
    } else if (nativeNode instanceof MessageSend) {
        return resolve(((MessageSend) nativeNode).binding);
    } else if (nativeNode instanceof AllocationExpression) {
        return resolve(((AllocationExpression) nativeNode).binding);
    } else if (nativeNode instanceof TypeDeclaration) {
        return resolve(((TypeDeclaration) nativeNode).binding);
    } else if (nativeNode instanceof ExplicitConstructorCall) {
        return resolve(((ExplicitConstructorCall) nativeNode).binding);
    } else if (nativeNode instanceof Annotation) {
        AnnotationBinding compilerAnnotation =
                ((Annotation) nativeNode).getCompilerAnnotation();
        if (compilerAnnotation != null) {
            return new EcjResolvedAnnotation(compilerAnnotation);
        }
        return resolve(((Annotation) nativeNode).resolvedType);
    } else if (nativeNode instanceof AbstractMethodDeclaration) {
        return resolve(((AbstractMethodDeclaration) nativeNode).binding);
    } else if (nativeNode instanceof AbstractVariableDeclaration) {
        if (nativeNode instanceof LocalDeclaration) {
            return resolve(((LocalDeclaration) nativeNode).binding);
        } else if (nativeNode instanceof FieldDeclaration) {
            FieldDeclaration fieldDeclaration = (FieldDeclaration) nativeNode;
            if (fieldDeclaration.initialization instanceof AllocationExpression) {
                AllocationExpression allocation =
                        (AllocationExpression)fieldDeclaration.initialization;
                if (allocation.binding != null) {
                    // Field constructor call: this is an enum constant.
                    return new EcjResolvedMethod(allocation.binding);
                }
            }
            return resolve(fieldDeclaration.binding);
        }
    }

    // TODO: Handle org.eclipse.jdt.internal.compiler.ast.SuperReference. It
    // doesn't contain an actual method binding; the parent node call should contain
    // it, but is missing a native node reference; investigate the ECJ bridge's super
    // handling.

    return null;
}
 
Example #10
Source File: HandleSetter.java    From EasyMPermission with MIT License 4 votes vote down vote up
static MethodDeclaration createSetter(TypeDeclaration parent, EclipseNode fieldNode, String name, boolean shouldReturnThis, int modifier, EclipseNode sourceNode, List<Annotation> onMethod, List<Annotation> onParam) {
	FieldDeclaration field = (FieldDeclaration) fieldNode.get();
	ASTNode source = sourceNode.get();
	int pS = source.sourceStart, pE = source.sourceEnd;
	long p = (long)pS << 32 | pE;
	MethodDeclaration method = new MethodDeclaration(parent.compilationResult);
	method.modifiers = modifier;
	if (shouldReturnThis) {
		method.returnType = cloneSelfType(fieldNode, source);
	}
	
	if (method.returnType == null) {
		method.returnType = TypeReference.baseTypeReference(TypeIds.T_void, 0);
		method.returnType.sourceStart = pS; method.returnType.sourceEnd = pE;
		shouldReturnThis = false;
	}
	Annotation[] deprecated = null;
	if (isFieldDeprecated(fieldNode)) {
		deprecated = new Annotation[] { generateDeprecatedAnnotation(source) };
	}
	method.annotations = copyAnnotations(source, onMethod.toArray(new Annotation[0]), deprecated);
	Argument param = new Argument(field.name, p, copyType(field.type, source), Modifier.FINAL);
	param.sourceStart = pS; param.sourceEnd = pE;
	method.arguments = new Argument[] { param };
	method.selector = name.toCharArray();
	method.binding = null;
	method.thrownExceptions = null;
	method.typeParameters = null;
	method.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
	Expression fieldRef = createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source);
	NameReference fieldNameRef = new SingleNameReference(field.name, p);
	Assignment assignment = new Assignment(fieldRef, fieldNameRef, (int)p);
	assignment.sourceStart = pS; assignment.sourceEnd = assignment.statementEnd = pE;
	method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart;
	method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd;
	
	Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN);
	Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN);
	List<Statement> statements = new ArrayList<Statement>(5);
	if (nonNulls.length == 0) {
		statements.add(assignment);
	} else {
		Statement nullCheck = generateNullCheck(field, sourceNode);
		if (nullCheck != null) statements.add(nullCheck);
		statements.add(assignment);
	}
	
	if (shouldReturnThis) {
		ThisReference thisRef = new ThisReference(pS, pE);
		ReturnStatement returnThis = new ReturnStatement(thisRef, pS, pE);
		statements.add(returnThis);
	}
	method.statements = statements.toArray(new Statement[0]);
	param.annotations = copyAnnotations(source, nonNulls, nullables, onParam.toArray(new Annotation[0]));
	
	method.traverse(new SetGeneratedByVisitor(source), parent.scope);
	return method;
}
 
Example #11
Source File: SelectionParser.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public NameReference createQualifiedAssistNameReference(char[][] previousIdentifiers, char[] assistName, long[] positions){
	return new SelectionOnQualifiedNameReference(
					previousIdentifiers,
					assistName,
					positions);
}
 
Example #12
Source File: SelectionParser.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public NameReference createSingleAssistNameReference(char[] assistName, long position) {
	return new SelectionOnSingleNameReference(assistName, position);
}
 
Example #13
Source File: SelectionParser.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected NameReference getUnspecifiedReference(boolean rejectTypeAnnotations) {
	/* build a (unspecified) NameReference which may be qualified*/

	int completionIndex;

	/* no need to take action if not inside completed identifiers */
	if ((completionIndex = indexOfAssistIdentifier()) < 0) {
		return super.getUnspecifiedReference(rejectTypeAnnotations);
	}

	if (rejectTypeAnnotations) {
		consumeNonTypeUseName();
	}
	int length = this.identifierLengthStack[this.identifierLengthPtr];
	if (CharOperation.equals(assistIdentifier(), SUPER)){
		Reference reference;
		if (completionIndex > 0){ // qualified super
			// discard 'super' from identifier stacks
			// There is some voodoo going on here in combination with SelectionScanne#scanIdentifierOrKeyword, do in Rome as Romans do and leave the stacks at the right depth.
			this.identifierLengthStack[this.identifierLengthPtr] = completionIndex;
			int ptr = this.identifierPtr -= (length - completionIndex);
			pushOnGenericsLengthStack(0);
			pushOnGenericsIdentifiersLengthStack(this.identifierLengthStack[this.identifierLengthPtr]);
			for (int i = 0; i < completionIndex; i++) {
				pushOnTypeAnnotationLengthStack(0);
			}
			reference =
				new SelectionOnQualifiedSuperReference(
					getTypeReference(0),
					(int)(this.identifierPositionStack[ptr+1] >>> 32),
					(int) this.identifierPositionStack[ptr+1]);
		} else { // standard super
			this.identifierPtr -= length;
			this.identifierLengthPtr--;
			reference = new SelectionOnSuperReference((int)(this.identifierPositionStack[this.identifierPtr+1] >>> 32), (int) this.identifierPositionStack[this.identifierPtr+1]);
		}
		pushOnAstStack(reference);
		this.assistNode = reference;
		this.lastCheckPoint = reference.sourceEnd + 1;
		if (!this.diet || this.dietInt != 0){
			this.restartRecovery	= true;	// force to restart in recovery mode
			this.lastIgnoredToken = -1;
		}
		this.isOrphanCompletionNode = true;
		return new SingleNameReference(CharOperation.NO_CHAR, 0); // dummy reference
	}
	NameReference nameReference;
	/* retrieve identifiers subset and whole positions, the completion node positions
		should include the entire replaced source. */
	char[][] subset = identifierSubSet(completionIndex);
	this.identifierLengthPtr--;
	this.identifierPtr -= length;
	long[] positions = new long[length];
	System.arraycopy(
		this.identifierPositionStack,
		this.identifierPtr + 1,
		positions,
		0,
		length);
	/* build specific completion on name reference */
	if (completionIndex == 0) {
		/* completion inside first identifier */
		nameReference = createSingleAssistNameReference(assistIdentifier(), positions[0]);
	} else {
		/* completion inside subsequent identifier */
		nameReference = createQualifiedAssistNameReference(subset, assistIdentifier(), positions);
	}
	this.assistNode = nameReference;
	this.lastCheckPoint = nameReference.sourceEnd + 1;
	if (!this.diet){
		this.restartRecovery	= true;	// force to restart in recovery mode
		this.lastIgnoredToken = -1;
	}
	this.isOrphanCompletionNode = true;
	return nameReference;
}
 
Example #14
Source File: AssistParser.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected NameReference getUnspecifiedReferenceOptimized() {

	int completionIndex;

	/* no need to take action if not inside completed identifiers */
	if ((completionIndex = indexOfAssistIdentifier()) < 0) {
		return super.getUnspecifiedReferenceOptimized();
	}

	consumeNonTypeUseName();
	
	/* retrieve identifiers subset and whole positions, the completion node positions
		should include the entire replaced source. */
	int length = this.identifierLengthStack[this.identifierLengthPtr];
	char[][] subset = identifierSubSet(completionIndex);
	this.identifierLengthPtr--;
	this.identifierPtr -= length;
	long[] positions = new long[length];
	System.arraycopy(
		this.identifierPositionStack,
		this.identifierPtr + 1,
		positions,
		0,
		length);

	/* build specific completion on name reference */
	NameReference reference;
	if (completionIndex == 0) {
		/* completion inside first identifier */
		reference = createSingleAssistNameReference(assistIdentifier(), positions[0]);
	} else {
		/* completion inside subsequent identifier */
		reference = createQualifiedAssistNameReference(subset, assistIdentifier(), positions);
	}
	reference.bits &= ~ASTNode.RestrictiveFlagMASK;
	reference.bits |= Binding.LOCAL | Binding.FIELD;

	this.assistNode = reference;
	this.lastCheckPoint = reference.sourceEnd + 1;
	return reference;
}
 
Example #15
Source File: AssistParser.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 votes vote down vote up
public abstract NameReference createQualifiedAssistNameReference(char[][] previousIdentifiers, char[] assistName, long[] positions); 
Example #16
Source File: AssistParser.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 votes vote down vote up
public abstract NameReference createSingleAssistNameReference(char[] assistName, long position);