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

The following examples show how to use org.eclipse.jdt.internal.compiler.ast.MemberValuePair. 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: CompilationUnitStructureRequestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @see ISourceElementRequestor
 */
public void exitMethod(int declarationEnd, Expression defaultValue) {
	SourceMethod handle = (SourceMethod) this.handleStack.peek();
	MethodInfo methodInfo = (MethodInfo) this.infoStack.peek();
	
	SourceMethodElementInfo info = createMethodInfo(methodInfo, handle);
	info.setSourceRangeEnd(declarationEnd);
	
	// remember default value of annotation method
	if (info.isAnnotationMethod() && defaultValue != null) {
		SourceAnnotationMethodInfo annotationMethodInfo = (SourceAnnotationMethodInfo) info;
		annotationMethodInfo.defaultValueStart = defaultValue.sourceStart;
		annotationMethodInfo.defaultValueEnd = defaultValue.sourceEnd;
		JavaElement element = (JavaElement) this.handleStack.peek();
		org.eclipse.jdt.internal.core.MemberValuePair defaultMemberValuePair = new org.eclipse.jdt.internal.core.MemberValuePair(element.getElementName());
		defaultMemberValuePair.value = getMemberValue(defaultMemberValuePair, defaultValue);
		annotationMethodInfo.defaultValue = defaultMemberValuePair;
	}
	
	this.handleStack.pop();
	this.infoStack.pop();
}
 
Example #2
Source File: SelectionParser.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected void consumeMemberValuePair() {
	if (this.indexOfAssistIdentifier() < 0) {
		super.consumeMemberValuePair();
		return;
	}

	char[] simpleName = this.identifierStack[this.identifierPtr];
	long position = this.identifierPositionStack[this.identifierPtr--];
	this.identifierLengthPtr--;
	int end = (int) position;
	int start = (int) (position >>> 32);
	Expression value = this.expressionStack[this.expressionPtr--];
	this.expressionLengthPtr--;
	MemberValuePair memberValuePair = new SelectionOnNameOfMemberValuePair(simpleName, start, end, value);
	pushOnAstStack(memberValuePair);

	this.assistNode = memberValuePair;
	this.lastCheckPoint = memberValuePair.sourceEnd + 1;


}
 
Example #3
Source File: CompletionUnitStructureRequestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected IMemberValuePair[] getMemberValuePairs(MemberValuePair[] memberValuePairs) {
	int membersLength = memberValuePairs.length;
	int membersCount = 0;
	IMemberValuePair[] members = new IMemberValuePair[membersLength];
	next : for (int j = 0; j < membersLength; j++) {
		if (memberValuePairs[j] instanceof CompletionOnMemberValueName) continue next;

		members[membersCount++] = getMemberValuePair(memberValuePairs[j]);
	}

	if (membersCount > membersLength) {
		System.arraycopy(members, 0, members, 0, membersCount);
	}
	return members;
}
 
Example #4
Source File: DefaultBindingResolver.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
synchronized IMemberValuePairBinding resolveMemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair memberValuePair) {
	MemberValuePair valuePair = (MemberValuePair) this.newAstToOldAst.get(memberValuePair);
	if (valuePair != null) {
		return getMemberValuePairBinding(valuePair.compilerElementPair);
	}
	return null;
}
 
Example #5
Source File: CompilationUnitStructureRequestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected IMemberValuePair[] getMemberValuePairs(MemberValuePair[] memberValuePairs) {
	int membersLength = memberValuePairs.length;
	IMemberValuePair[] members = new IMemberValuePair[membersLength];
	for (int j = 0; j < membersLength; j++) {
		members[j] = getMemberValuePair(memberValuePairs[j]);
	}
	return members;
}
 
Example #6
Source File: AndLocator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public int match(MemberValuePair node, MatchingNodeSet nodeSet) {
	int level = IMPOSSIBLE_MATCH;
	for (int i = 0, length = this.patternLocators.length; i < length; i++) {
		int newLevel = this.patternLocators[i].match(node, nodeSet);
		if (newLevel > level) {
			if (newLevel == ACCURATE_MATCH) return ACCURATE_MATCH;
			level = newLevel;
		}
	}
	return level;
}
 
Example #7
Source File: OrLocator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public int match(MemberValuePair node, MatchingNodeSet nodeSet) {
	int level = IMPOSSIBLE_MATCH;
	for (int i = 0, length = this.patternLocators.length; i < length; i++) {
		int newLevel = this.patternLocators[i].match(node, nodeSet);
		if (newLevel > level) {
			if (newLevel == ACCURATE_MATCH) return ACCURATE_MATCH;
			level = newLevel;
		}
	}
	return level;
}
 
Example #8
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 #9
Source File: EclipseHandlerUtil.java    From EasyMPermission with MIT License 5 votes vote down vote up
public static Annotation[] addSuppressWarningsAll(EclipseNode node, ASTNode source, Annotation[] originalAnnotationArray) {
	Annotation[] anns = addAnnotation(source, originalAnnotationArray, TypeConstants.JAVA_LANG_SUPPRESSWARNINGS, new StringLiteral(ALL, 0, 0, 0));
	
	if (Boolean.TRUE.equals(node.getAst().readConfiguration(ConfigurationKeys.ADD_FINDBUGS_SUPPRESSWARNINGS_ANNOTATIONS))) {
		MemberValuePair mvp = new MemberValuePair(JUSTIFICATION, 0, 0, new StringLiteral(GENERATED_CODE, 0, 0, 0));
		anns = addAnnotation(source, anns, EDU_UMD_CS_FINDBUGS_ANNOTATIONS_SUPPRESSFBWARNINGS, mvp);
	}
	
	return anns;
}
 
Example #10
Source File: HandleSneakyThrows.java    From EasyMPermission with MIT License 5 votes vote down vote up
@Override public void handle(AnnotationValues<SneakyThrows> annotation, Annotation source, EclipseNode annotationNode) {
		handleFlagUsage(annotationNode, ConfigurationKeys.SNEAKY_THROWS_FLAG_USAGE, "@SneakyThrows");
		
		List<String> exceptionNames = annotation.getRawExpressions("value");
		List<DeclaredException> exceptions = new ArrayList<DeclaredException>();
		
		MemberValuePair[] memberValuePairs = source.memberValuePairs();
		if (memberValuePairs == null || memberValuePairs.length == 0) {
			exceptions.add(new DeclaredException("java.lang.Throwable", source));
		} else {
			Expression arrayOrSingle = memberValuePairs[0].value;
			final Expression[] exceptionNameNodes;
			if (arrayOrSingle instanceof ArrayInitializer) {
				exceptionNameNodes = ((ArrayInitializer)arrayOrSingle).expressions;
			} else exceptionNameNodes = new Expression[] { arrayOrSingle };
			
			if (exceptionNames.size() != exceptionNameNodes.length) {
				annotationNode.addError(
						"LOMBOK BUG: The number of exception classes in the annotation isn't the same pre- and post- guessing.");
			}
			
			int idx = 0;
			for (String exceptionName : exceptionNames) {
				if (exceptionName.endsWith(".class")) exceptionName = exceptionName.substring(0, exceptionName.length() - 6);
				exceptions.add(new DeclaredException(exceptionName, exceptionNameNodes[idx++]));
			}
		}
		
		
		EclipseNode owner = annotationNode.up();
		switch (owner.getKind()) {
//		case FIELD:
//			return handleField(annotationNode, (FieldDeclaration)owner.get(), exceptions);
		case METHOD:
			handleMethod(annotationNode, (AbstractMethodDeclaration)owner.get(), exceptions);
			break;
		default:
			annotationNode.addError("@SneakyThrows is legal only on methods and constructors.");
		}
	}
 
Example #11
Source File: Extractor.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
public AnnotationData createData(@NonNull String name, @NonNull Annotation annotation) {
    MemberValuePair[] pairs = annotation.memberValuePairs();
    if (pairs == null || pairs.length == 0) {
        return new AnnotationData(name);
    }
    return new AnnotationData(name, pairs);
}
 
Example #12
Source File: SelectionParser.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected void consumeNormalAnnotation(boolean isTypeAnnotation) {
	int index;

	if ((index = this.indexOfAssistIdentifier()) < 0) {
		super.consumeNormalAnnotation(isTypeAnnotation);
		return;
	}

	NormalAnnotation normalAnnotation = null;
	int length = this.identifierLengthStack[this.identifierLengthPtr];
	TypeReference typeReference;

	/* retrieve identifiers subset and whole positions, the assist node positions
		should include the entire replaced source. */

	char[][] subset = identifierSubSet(index);
	this.identifierLengthPtr--;
	this.identifierPtr -= length;
	long[] positions = new long[length];
	System.arraycopy(
		this.identifierPositionStack,
		this.identifierPtr + 1,
		positions,
		0,
		length);

	/* build specific assist on type reference */

	if (index == 0) {
		/* assist inside first identifier */
		typeReference = createSingleAssistTypeReference(
						assistIdentifier(),
						positions[0]);
	} else {
		/* assist inside subsequent identifier */
		typeReference =	createQualifiedAssistTypeReference(
						subset,
						assistIdentifier(),
						positions);
	}
	this.assistNode = typeReference;
	this.lastCheckPoint = typeReference.sourceEnd + 1;

	normalAnnotation = new NormalAnnotation(typeReference, this.intStack[this.intPtr--]);
	if ((length = this.astLengthStack[this.astLengthPtr--]) != 0) {
		System.arraycopy(
			this.astStack,
			(this.astPtr -= length) + 1,
			normalAnnotation.memberValuePairs = new MemberValuePair[length],
			0,
			length);
	}
	normalAnnotation.declarationSourceEnd = this.rParenPos;
	if (isTypeAnnotation) {
		pushOnTypeAnnotationStack(normalAnnotation);
	} else {
		pushOnExpressionStack(normalAnnotation);
	}
}
 
Example #13
Source File: SetGeneratedByVisitor.java    From EasyMPermission with MIT License 4 votes vote down vote up
@Override public boolean visit(MemberValuePair node, BlockScope scope) {
	fixPositions(setGeneratedBy(node, source));
	return super.visit(node, scope);
}
 
Example #14
Source File: CompletionOnAnnotationMemberValuePair.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public CompletionOnAnnotationMemberValuePair(TypeReference type, int sourceStart, MemberValuePair[] memberValuePairs, MemberValuePair completedMemberValuePair) {
	super(type, sourceStart);
	this.memberValuePairs = memberValuePairs;
	this.completedMemberValuePair = completedMemberValuePair;
}
 
Example #15
Source File: EclipseHandlerUtil.java    From EasyMPermission with MIT License 4 votes vote down vote up
public static List<Annotation> unboxAndRemoveAnnotationParameter(Annotation annotation, String annotationName, String errorName, EclipseNode errorNode) {
	if ("value".equals(annotationName)) {
		// We can't unbox this, because SingleMemberAnnotation REQUIRES a value, and this method
		// is supposed to remove the value. That means we need to replace the SMA with either
		// MarkerAnnotation or NormalAnnotation and that is beyond the scope of this method as we
		// don't need that at the time of writing this method; we only unbox onMethod, onParameter
		// and onConstructor. Let's exit early and very obviously:
		throw new UnsupportedOperationException("Lombok cannot unbox 'value' from SingleMemberAnnotation at this time.");
	}
	if (!NormalAnnotation.class.equals(annotation.getClass())) {
		// Prevent MarkerAnnotation, SingleMemberAnnotation, and
		// CompletionOnAnnotationMemberValuePair from triggering this handler.
		return Collections.emptyList();
	}
	
	NormalAnnotation normalAnnotation = (NormalAnnotation) annotation;
	MemberValuePair[] pairs = normalAnnotation.memberValuePairs;
	
	if (pairs == null) return Collections.emptyList();
	
	char[] nameAsCharArray = annotationName.toCharArray();
	
	for (int i = 0; i < pairs.length; i++) {
		if (pairs[i].name == null || !Arrays.equals(nameAsCharArray, pairs[i].name)) continue;
		Expression value = pairs[i].value;
		MemberValuePair[] newPairs = new MemberValuePair[pairs.length - 1];
		if (i > 0) System.arraycopy(pairs, 0, newPairs, 0, i);
		if (i < pairs.length - 1) System.arraycopy(pairs, i + 1, newPairs, i, pairs.length - i - 1);
		normalAnnotation.memberValuePairs = newPairs;
		// We have now removed the annotation parameter and stored '@__({... annotations ...})',
		// which we must now unbox.
		if (!(value instanceof Annotation)) {
			errorNode.addError("The correct format is " + errorName + "@__({@SomeAnnotation, @SomeOtherAnnotation}))");
			return Collections.emptyList();
		}
		
		Annotation atDummyIdentifier = (Annotation) value;
		if (!(atDummyIdentifier.type instanceof SingleTypeReference) ||
				!isAllValidOnXCharacters(((SingleTypeReference) atDummyIdentifier.type).token)) {
			errorNode.addError("The correct format is " + errorName + "@__({@SomeAnnotation, @SomeOtherAnnotation}))");
			return Collections.emptyList();
		}
		
		if (atDummyIdentifier instanceof MarkerAnnotation) {
			// It's @Getter(onMethod=@__). This is weird, but fine.
			return Collections.emptyList();
		}
		
		Expression content = null;
		
		if (atDummyIdentifier instanceof NormalAnnotation) {
			MemberValuePair[] mvps = ((NormalAnnotation) atDummyIdentifier).memberValuePairs;
			if (mvps == null || mvps.length == 0) {
				// It's @Getter(onMethod=@__()). This is weird, but fine.
				return Collections.emptyList();
			}
			if (mvps.length == 1 && Arrays.equals("value".toCharArray(), mvps[0].name)) {
				content = mvps[0].value;
			}
		}
		
		if (atDummyIdentifier instanceof SingleMemberAnnotation) {
			content = ((SingleMemberAnnotation) atDummyIdentifier).memberValue;
		}
		
		if (content == null) {
			errorNode.addError("The correct format is " + errorName + "@__({@SomeAnnotation, @SomeOtherAnnotation}))");
			return Collections.emptyList();
		}
		
		if (content instanceof Annotation) {
			return Collections.singletonList((Annotation) content);
		} else if (content instanceof ArrayInitializer) {
			Expression[] expressions = ((ArrayInitializer) content).expressions;
			List<Annotation> result = new ArrayList<Annotation>();
			if (expressions != null) for (Expression ex : expressions) {
				if (ex instanceof Annotation) result.add((Annotation) ex);
				else {
					errorNode.addError("The correct format is " + errorName + "@__({@SomeAnnotation, @SomeOtherAnnotation}))");
					return Collections.emptyList();
				}
			}
			return result;
		} else {
			errorNode.addError("The correct format is " + errorName + "@__({@SomeAnnotation, @SomeOtherAnnotation}))");
			return Collections.emptyList();
		}
	}
	
	return Collections.emptyList();
}
 
Example #16
Source File: CompilationUnitStructureRequestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected IMemberValuePair getMemberValuePair(MemberValuePair memberValuePair) {
	String memberName = new String(memberValuePair.name);
	org.eclipse.jdt.internal.core.MemberValuePair result = new org.eclipse.jdt.internal.core.MemberValuePair(memberName);
	result.value = getMemberValue(result, memberValuePair.value);
	return result;
}
 
Example #17
Source File: Extractor.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
private AnnotationData(@NonNull String name, @Nullable MemberValuePair[] pairs) {
    this(name);
    attributes = pairs;
    assert attributes == null || attributes.length > 0;
}
 
Example #18
Source File: CompilationUnitStructureRequestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected Object getMemberValue(org.eclipse.jdt.internal.core.MemberValuePair memberValuePair, Expression expression) {
	if (expression instanceof NullLiteral) {
		return null;
	} else if (expression instanceof Literal) {
		((Literal) expression).computeConstant();
		return Util.getAnnotationMemberValue(memberValuePair, expression.constant);
	} else if (expression instanceof org.eclipse.jdt.internal.compiler.ast.Annotation) {
		org.eclipse.jdt.internal.compiler.ast.Annotation annotation = (org.eclipse.jdt.internal.compiler.ast.Annotation) expression;
		Object handle = acceptAnnotation(annotation, null, (JavaElement) this.handleStack.peek());
		memberValuePair.valueKind = IMemberValuePair.K_ANNOTATION;
		return handle;
	} else if (expression instanceof ClassLiteralAccess) {
		ClassLiteralAccess classLiteral = (ClassLiteralAccess) expression;
		char[] name = CharOperation.concatWith(classLiteral.type.getTypeName(), '.');
		memberValuePair.valueKind = IMemberValuePair.K_CLASS;
		return new String(name);
	} else if (expression instanceof QualifiedNameReference) {
		char[] qualifiedName = CharOperation.concatWith(((QualifiedNameReference) expression).tokens, '.');
		memberValuePair.valueKind = IMemberValuePair.K_QUALIFIED_NAME;
		return new String(qualifiedName);
	} else if (expression instanceof SingleNameReference) {
		char[] simpleName = ((SingleNameReference) expression).token;
		if (simpleName == RecoveryScanner.FAKE_IDENTIFIER) {
			memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
			return null;
		}
		memberValuePair.valueKind = IMemberValuePair.K_SIMPLE_NAME;
		return new String(simpleName);
	} else if (expression instanceof ArrayInitializer) {
		memberValuePair.valueKind = -1; // modified below by the first call to getMemberValue(...)
		Expression[] expressions = ((ArrayInitializer) expression).expressions;
		int length = expressions == null ? 0 : expressions.length;
		Object[] values = new Object[length];
		for (int i = 0; i < length; i++) {
			int previousValueKind = memberValuePair.valueKind;
			Object value = getMemberValue(memberValuePair, expressions[i]);
			if (previousValueKind != -1 && memberValuePair.valueKind != previousValueKind) {
				// values are heterogeneous, value kind is thus unknown
				memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
			}
			values[i] = value;
		}
		if (memberValuePair.valueKind == -1)
			memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
		return values;
	} else if (expression instanceof UnaryExpression) {			// to deal with negative numerals (see bug - 248312)
		UnaryExpression unaryExpression = (UnaryExpression) expression;
		if ((unaryExpression.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT == OperatorIds.MINUS) {
			if (unaryExpression.expression instanceof Literal) {
				Literal subExpression = (Literal) unaryExpression.expression;
				subExpression.computeConstant();
				return Util.getNegativeAnnotationMemberValue(memberValuePair, subExpression.constant);
			}
		}
		memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
		return null;
	} else {
		memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
		return null;
	}
}