org.eclipse.jdt.core.dom.MemberValuePair Java Examples

The following examples show how to use org.eclipse.jdt.core.dom.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: StyledStringVisitor.java    From JDeodorant with MIT License 6 votes vote down vote up
public boolean visit(NormalAnnotation annotation) {
	/*
	 * NormalAnnotation: @ TypeName ( [ MemberValuePair { , MemberValuePair } ] )
	 */
	activateDiffStyle(annotation);
	appendAtSign();
	handleExpression(annotation.getTypeName());
	appendOpenParenthesis();
	for(int i=0; i<annotation.values().size(); i++) {
		visit((MemberValuePair) annotation.values().get(i));
		if(i < annotation.values().size() - 1) {
			appendComma();
		}
	}
	appendClosedParenthesis();
	deactivateDiffStyle(annotation);
	return false;
}
 
Example #2
Source File: NewAnnotationMemberProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Type getNewType(ASTRewrite rewrite) {
	AST ast= rewrite.getAST();
	Type newTypeNode= null;
	ITypeBinding binding= null;
	if (fInvocationNode.getLocationInParent() == MemberValuePair.NAME_PROPERTY) {
		Expression value= ((MemberValuePair) fInvocationNode.getParent()).getValue();
		binding= value.resolveTypeBinding();
	} else if (fInvocationNode instanceof Expression) {
		binding= ((Expression) fInvocationNode).resolveTypeBinding();
	}
	if (binding != null) {
		ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(fInvocationNode, getImportRewrite());
		newTypeNode= getImportRewrite().addImport(binding, ast, importRewriteContext);
	}
	if (newTypeNode == null) {
		newTypeNode= ast.newSimpleType(ast.newSimpleName("String")); //$NON-NLS-1$
	}
	addLinkedPosition(rewrite.track(newTypeNode), false, KEY_TYPE);
	return newTypeNode;
}
 
Example #3
Source File: NewAnnotationMemberProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private SimpleName getNewName(ASTRewrite rewrite) {
	AST ast= rewrite.getAST();
	String name;
	if (fInvocationNode.getLocationInParent() == MemberValuePair.NAME_PROPERTY) {
		name= ((SimpleName) fInvocationNode).getIdentifier();
		if (ast == fInvocationNode.getAST()) {
			addLinkedPosition(rewrite.track(fInvocationNode), true, KEY_NAME);
		}
	} else {
		name= "value"; //$NON-NLS-1$
	}


	SimpleName newNameNode= ast.newSimpleName(name);
	addLinkedPosition(rewrite.track(newNameNode), false, KEY_NAME);
	return newNameNode;
}
 
Example #4
Source File: ASTNodes.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the topmost ancestor of <code>node</code> that is a {@link Type} (but not a {@link UnionType}).
 * <p>
 * <b>Note:</b> The returned node often resolves to a different binding than the given <code>node</code>!
 * 
 * @param node the starting node, can be <code>null</code>
 * @return the topmost type or <code>null</code> if the node is not a descendant of a type node
 * @see #getNormalizedNode(ASTNode)
 */
public static Type getTopMostType(ASTNode node) {
	ASTNode result= null;
	while (node instanceof Type && !(node instanceof UnionType)
			|| node instanceof Name
			|| node instanceof Annotation || node instanceof MemberValuePair
			|| node instanceof Expression) { // Expression could maybe be reduced to expression node types that can appear in an annotation
		result= node;
		node= node.getParent();
	}
	
	if (result instanceof Type)
		return (Type) result;
	
	return null;
}
 
Example #5
Source File: FlowAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void endVisit(MemberValuePair node) {
	if (skipNode(node))
		return;

	FlowInfo name= getFlowInfo(node.getName());
	FlowInfo value= getFlowInfo(node.getValue());
	if (name instanceof LocalFlowInfo) {
		LocalFlowInfo llhs= (LocalFlowInfo)name;
		llhs.setWriteAccess(fFlowContext);
	}
	GenericSequentialFlowInfo info= createSequential(node);
	// first process value and then name.
	info.merge(value, fFlowContext);
	info.merge(name, fFlowContext);

}
 
Example #6
Source File: UMLAnnotation.java    From RefactoringMiner with MIT License 6 votes vote down vote up
public UMLAnnotation(CompilationUnit cu, String filePath, Annotation annotation) {
	this.typeName = annotation.getTypeName().getFullyQualifiedName();
	this.locationInfo = new LocationInfo(cu, filePath, annotation, CodeElementType.ANNOTATION);
	if(annotation instanceof SingleMemberAnnotation) {
		SingleMemberAnnotation singleMemberAnnotation = (SingleMemberAnnotation)annotation;
		this.value = new AbstractExpression(cu, filePath, singleMemberAnnotation.getValue(), CodeElementType.SINGLE_MEMBER_ANNOTATION_VALUE);
	}
	else if(annotation instanceof NormalAnnotation) {
		NormalAnnotation normalAnnotation = (NormalAnnotation)annotation;
		List<MemberValuePair> pairs = normalAnnotation.values();
		for(MemberValuePair pair : pairs) {
			AbstractExpression value = new AbstractExpression(cu, filePath, pair.getValue(), CodeElementType.NORMAL_ANNOTATION_MEMBER_VALUE_PAIR);
			memberValuePairs.put(pair.getName().getIdentifier(), value);
		}
	}
}
 
Example #7
Source File: NewAnnotationMemberProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private Type getNewType(ASTRewrite rewrite) {
	AST ast= rewrite.getAST();
	Type newTypeNode= null;
	ITypeBinding binding= null;
	if (fInvocationNode.getLocationInParent() == MemberValuePair.NAME_PROPERTY) {
		Expression value= ((MemberValuePair) fInvocationNode.getParent()).getValue();
		binding= value.resolveTypeBinding();
	} else if (fInvocationNode instanceof Expression) {
		binding= ((Expression) fInvocationNode).resolveTypeBinding();
	}
	if (binding != null) {
		ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(fInvocationNode, getImportRewrite());
		newTypeNode= getImportRewrite().addImport(binding, ast, importRewriteContext);
	}
	if (newTypeNode == null) {
		newTypeNode= ast.newSimpleType(ast.newSimpleName("String")); //$NON-NLS-1$
	}
	return newTypeNode;
}
 
Example #8
Source File: FlowAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void endVisit(MemberValuePair node) {
	if (skipNode(node)) {
		return;
	}

	FlowInfo name = getFlowInfo(node.getName());
	FlowInfo value = getFlowInfo(node.getValue());
	if (name instanceof LocalFlowInfo) {
		LocalFlowInfo llhs = (LocalFlowInfo) name;
		llhs.setWriteAccess(fFlowContext);
	}
	GenericSequentialFlowInfo info = createSequential(node);
	// first process value and then name.
	info.merge(value, fFlowContext);
	info.merge(name, fFlowContext);

}
 
Example #9
Source File: NewAnnotationMemberProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private SimpleName getNewName(ASTRewrite rewrite) {
	AST ast= rewrite.getAST();
	String name;
	if (fInvocationNode.getLocationInParent() == MemberValuePair.NAME_PROPERTY) {
		name= ((SimpleName) fInvocationNode).getIdentifier();
	} else {
		name= "value"; //$NON-NLS-1$
	}


	SimpleName newNameNode= ast.newSimpleName(name);
	return newNameNode;
}
 
Example #10
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean visit(final MemberValuePair node) {
  node.getName().accept(this);
  this.appendToBuffer("=");
  node.getValue().accept(this);
  return false;
}
 
Example #11
Source File: JavaASTUtils.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns an annotation's value. If the annotation not a single-member
 * annotation, this is the value corresponding to the key named "value".
 */
@SuppressWarnings("unchecked")
public static Expression getAnnotationValue(Annotation annotation) {
  if (annotation instanceof SingleMemberAnnotation) {
    return ((SingleMemberAnnotation) annotation).getValue();
  } else if (annotation instanceof NormalAnnotation) {
    NormalAnnotation normalAnnotation = (NormalAnnotation) annotation;
    for (MemberValuePair pair : (List<MemberValuePair>) normalAnnotation.values()) {
      if (pair.getName().getIdentifier().equals("value")) {
        return pair.getValue();
      }
    }
  }
  return null;
}
 
Example #12
Source File: JsonDeserializeAdder.java    From SparkBuilderGenerator with MIT License 5 votes vote down vote up
private NormalAnnotation createAnnotation(AST ast, CompilationUnitModificationDomain compilationUnitModificationDomain, TypeDeclaration builderType) {
    TypeLiteral typeLiteral = createBuilderClassReferenceLiteral(ast, compilationUnitModificationDomain, builderType);

    NormalAnnotation jsonDeserializeAnnotation = ast.newNormalAnnotation();
    jsonDeserializeAnnotation.setTypeName(ast.newSimpleName(JSON_DESERIALIZE_CLASS_NAME));

    MemberValuePair builderAttribute = ast.newMemberValuePair();
    builderAttribute.setName(ast.newSimpleName("builder"));
    builderAttribute.setValue(typeLiteral);

    jsonDeserializeAnnotation.values().add(builderAttribute);

    return jsonDeserializeAnnotation;
}
 
Example #13
Source File: SemanticHighlightings.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean consumes(SemanticToken token) {
	SimpleName node = token.getNode();
	if (node.getParent() instanceof MemberValuePair) {
		IBinding binding = token.getBinding();
		boolean isAnnotationElement = binding != null && binding.getKind() == IBinding.METHOD;

		return isAnnotationElement;
	}

	return false;
}
 
Example #14
Source File: StyledStringVisitor.java    From JDeodorant with MIT License 5 votes vote down vote up
public boolean visit(MemberValuePair pair) {
	/*
	 * MemberValuePair: SimpleName = Expression
	 */
	handleExpression(pair.getName());
	appendEquals();
	handleExpression(pair.getValue());
	return false;
}
 
Example #15
Source File: JsonDeserializeRemover.java    From SparkBuilderGenerator with MIT License 5 votes vote down vote up
private boolean isBuilderDeserializer(NormalAnnotation annotation) {
    List<MemberValuePair> values = annotation.values();
    if (values.size() != 1) {
        return false;
    }
    return values.get(0)
            .getName()
            .toString()
            .equals("builder");
}
 
Example #16
Source File: SemanticHighlightings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean consumes(SemanticToken token) {
	SimpleName node= token.getNode();
	if (node.getParent() instanceof MemberValuePair) {
		IBinding binding= token.getBinding();
		boolean isAnnotationElement= binding != null && binding.getKind() == IBinding.METHOD;

		return isAnnotationElement;
	}

	return false;
}
 
Example #17
Source File: SuppressWarningsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static Expression findValue(List<MemberValuePair> keyValues) {
	for (int i= 0, len= keyValues.size(); i < len; i++) {
		MemberValuePair curr= keyValues.get(i);
		if ("value".equals(curr.getName().getIdentifier())) { //$NON-NLS-1$
			return curr.getValue();
		}
	}
	return null;
}
 
Example #18
Source File: SuppressWarningsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static void addRemoveUnusedSuppressWarningProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ASTNode coveringNode= problem.getCoveringNode(context.getASTRoot());
	if (!(coveringNode instanceof StringLiteral))
		return;

	StringLiteral literal= (StringLiteral) coveringNode;

	if (coveringNode.getParent() instanceof MemberValuePair) {
		coveringNode= coveringNode.getParent();
	}

	ASTNode parent= coveringNode.getParent();

	ASTRewrite rewrite= ASTRewrite.create(coveringNode.getAST());
	if (parent instanceof SingleMemberAnnotation) {
		rewrite.remove(parent, null);
	} else if (parent instanceof NormalAnnotation) {
		NormalAnnotation annot= (NormalAnnotation) parent;
		if (annot.values().size() == 1) {
			rewrite.remove(annot, null);
		} else {
			rewrite.remove(coveringNode, null);
		}
	} else if (parent instanceof ArrayInitializer) {
		rewrite.remove(coveringNode, null);
	} else {
		return;
	}
	String label= Messages.format(CorrectionMessages.SuppressWarningsSubProcessor_remove_annotation_label, literal.getLiteralValue());
	Image image= JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE);
	ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_ANNOTATION, image);
	proposals.add(proposal);
}
 
Example #19
Source File: AstMatchingNodeFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public boolean visit(MemberValuePair node) {
	if (node.subtreeMatch(fMatcher, fNodeToMatch))
		return matches(node);
	return super.visit(node);
}
 
Example #20
Source File: CopyQualifiedNameAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private Object getSelectedElement(JavaEditor editor) {
	ISourceViewer viewer= editor.getViewer();
	if (viewer == null)
		return null;

	Point selectedRange= viewer.getSelectedRange();
	int length= selectedRange.y;
	int offset= selectedRange.x;

	ITypeRoot element= JavaUI.getEditorInputTypeRoot(editor.getEditorInput());
	if (element == null)
		return null;

	CompilationUnit ast= SharedASTProvider.getAST(element, SharedASTProvider.WAIT_YES, null);
	if (ast == null)
		return null;

	NodeFinder finder= new NodeFinder(ast, offset, length);
	ASTNode node= finder.getCoveringNode();

	IBinding binding= null;
	if (node instanceof Name) {
		binding= getConstructorBindingIfAvailable((Name)node);
		if (binding != null)
			return binding;
		binding= ((Name)node).resolveBinding();
	} else if (node instanceof MethodInvocation) {
		binding= ((MethodInvocation)node).resolveMethodBinding();
	} else if (node instanceof MethodDeclaration) {
		binding= ((MethodDeclaration)node).resolveBinding();
	} else if (node instanceof Type) {
		binding= ((Type)node).resolveBinding();
	} else if (node instanceof AnonymousClassDeclaration) {
		binding= ((AnonymousClassDeclaration)node).resolveBinding();
	} else if (node instanceof TypeDeclaration) {
		binding= ((TypeDeclaration)node).resolveBinding();
	} else if (node instanceof CompilationUnit) {
		return ((CompilationUnit)node).getJavaElement();
	} else if (node instanceof Expression) {
		binding= ((Expression)node).resolveTypeBinding();
	} else if (node instanceof ImportDeclaration) {
		binding= ((ImportDeclaration)node).resolveBinding();
	} else if (node instanceof MemberRef) {
		binding= ((MemberRef)node).resolveBinding();
	} else if (node instanceof MemberValuePair) {
		binding= ((MemberValuePair)node).resolveMemberValuePairBinding();
	} else if (node instanceof PackageDeclaration) {
		binding= ((PackageDeclaration)node).resolveBinding();
	} else if (node instanceof TypeParameter) {
		binding= ((TypeParameter)node).resolveBinding();
	} else if (node instanceof VariableDeclaration) {
		binding= ((VariableDeclaration)node).resolveBinding();
	}

	if (binding != null)
		return binding.getJavaElement();

	return null;
}
 
Example #21
Source File: GenericVisitor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public boolean visit(MemberValuePair node) {
	return visitNode(node);
}
 
Example #22
Source File: GenericVisitor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void endVisit(MemberValuePair node) {
	endVisitNode(node);
}
 
Example #23
Source File: JsonPOJOBuilderAdderFragment.java    From SparkBuilderGenerator with MIT License 4 votes vote down vote up
private MemberValuePair createAnnotationAttribute(AST ast, String attributeName, String attributeValue) {
    MemberValuePair buildMethodNameAttribute = ast.newMemberValuePair();
    buildMethodNameAttribute.setName(ast.newSimpleName(attributeName));
    buildMethodNameAttribute.setValue(createStringLitereal(ast, attributeValue));
    return buildMethodNameAttribute;
}