/** * @param cu * @return */ public static String findPathGeneratorInGraphWalkerAnnotation(ICompilationUnit cu) { CompilationUnit ast = parse(cu); Map<String, String> ret = new HashMap<String, String>(); ast.accept(new ASTVisitor() { public boolean visit(MemberValuePair node) { String name = node.getName().getFullyQualifiedName(); if ("value".equals(name) && node.getParent() != null && node.getParent() instanceof NormalAnnotation) { IAnnotationBinding annoBinding = ((NormalAnnotation) node.getParent()).resolveAnnotationBinding(); String qname = annoBinding.getAnnotationType().getQualifiedName(); if (GraphWalker.class.getName().equals(qname)) { StringLiteral sl = (StringLiteral) node.getValue(); ret.put("ret", sl.getLiteralValue()); } } return true; } }); return ret.get("ret"); }
/** * 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; }
@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); }
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; }
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; }
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; }
@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); }
/** * 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; }
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; }
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; }
/** * Returns the annotations for a given <code>modifierList</code>. * * @param modifierList the modifier list to be processed * @return An ArraySet with all annotations */ @OperationMeta(returnGenerics = JavaAnnotation.class) public Set<JavaAnnotation> annotations(List<IExtendedModifier> modifierList) { List<JavaAnnotation> list = new ArrayList<JavaAnnotation>(); for (Object modifier : modifierList) { if (modifier instanceof org.eclipse.jdt.core.dom.Annotation) { org.eclipse.jdt.core.dom.Annotation annot = (org.eclipse.jdt.core.dom.Annotation) modifier; // Get annotation name and value Map<String, String> fields = new HashMap<String, String>(); // possibly the unqualified name as not resolved unless not given as qualified name! String name = annot.getTypeName().getFullyQualifiedName(); if (annot instanceof SingleMemberAnnotation) { fields.put(JavaAnnotation.DEFAULT_FIELD, toString(((SingleMemberAnnotation) modifier).getValue())); } else if (annot instanceof NormalAnnotation) { @SuppressWarnings("unchecked") List<MemberValuePair> values = ((NormalAnnotation) annot).values(); for (MemberValuePair pair : values) { fields.put(pair.getName().getIdentifier(), toString(pair.getValue())); } } list.add(new JavaAnnotation(name, fields, this)); } } return new ArraySet<JavaAnnotation>(list.toArray(new JavaAnnotation[list.size()]), JavaAnnotation.class); }
@SuppressWarnings("unchecked") public void writeSuppressWarning(BodyDeclaration target) { AST ast = target.getAST(); NormalAnnotation annotation = ast.newNormalAnnotation(); annotation.setTypeName(ast.newSimpleName(SuppressWarnings.class.getSimpleName())); MemberValuePair memberValuePair = ast.newMemberValuePair(); memberValuePair.setName(ast.newSimpleName("value")); StringLiteral stringLiteral = ast.newStringLiteral(); stringLiteral.setLiteralValue("static-access"); memberValuePair.setValue(stringLiteral); annotation.values().add(memberValuePair); target.modifiers().add(annotation); }
@SuppressWarnings("unchecked") public void writeEnumField(EnumConstantDeclaration enumField, QSpecialElement elem) { AST ast = enumField.getAST(); NormalAnnotation normalAnnotation = ast.newNormalAnnotation(); String name = new String("*" + enumField.getName()); if (elem.getValue() != null && !name.equals(elem.getValue())) { normalAnnotation.setTypeName(ast.newSimpleName(Special.class.getSimpleName())); MemberValuePair memberValuePair = ast.newMemberValuePair(); memberValuePair.setName(ast.newSimpleName("value")); StringLiteral stringLiteral = ast.newStringLiteral(); stringLiteral.setLiteralValue(elem.getValue()); memberValuePair.setValue(stringLiteral); normalAnnotation.values().add(memberValuePair); enumField.modifiers().add(normalAnnotation); } }
/** * Return true if the annotation content matches the input map (@Foo(f1 = v1 * , f2 = v2) * * @param annotation * input annotation to check * @param content * a Map object containing as key the expected member name and as * value the expected member value * @return true if the annotation is a normal annotation and if the content * matches the content parameter, false otherwise */ @SuppressWarnings("unchecked") public static boolean checkAnnotationContent(Annotation annotation, Map<String, Object> content) { boolean correct = false; // Test if this annotation is a Normal Member Annotation if (annotation != null && annotation.isNormalAnnotation()) { List<MemberValuePair> values = ((NormalAnnotation) annotation).values(); correct = true; for (int inc = 0; inc < values.size() && correct; inc++) { MemberValuePair mvp = values.get(inc); String memberName = mvp.getName().getFullyQualifiedName(); Object contentValue = content.get(memberName); correct = contentValue != null; Expression memberValue = mvp.getValue(); correct = checkSingleAnnotationValue(memberValue, contentValue); } } return correct; }
private static StringLiteral getGeneratedAnnotationComment(NormalAnnotation annotation) { MemberValuePair mvp = null; for (Object o : annotation.values()) { if (o instanceof MemberValuePair) { MemberValuePair tempMVP = (MemberValuePair) o; if (COMMENTS_PARAM.equals(tempMVP.getName().toString())) { mvp = tempMVP; break; } } } if (mvp != null && mvp.getValue() != null && mvp.getValue() instanceof StringLiteral) { StringLiteral literal = (StringLiteral) mvp.getValue(); return literal; } else{ return null; } }
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; }
protected MemberValuePair createAnnotationMemberValuePair(final AST ast, final String name, final String value) { final MemberValuePair mvp = ast.newMemberValuePair(); mvp.setName(ast.newSimpleName(name)); StringLiteral stringLiteral = ast.newStringLiteral(); stringLiteral.setLiteralValue(value); mvp.setValue(stringLiteral); return mvp; }
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; }
@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; }
/** * 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; }
@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; }
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; }
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); }
@Override public boolean visit(MemberValuePair node) { String memberName = node.getName().getIdentifier(); Expression memberValue = node.getValue(); Annotation currentAnno = annoStack.peek(); parseMemberValuePair(currentAnno, memberName, memberValue); return true; }
@Override public boolean visit(MemberValuePair node) { node.getName().accept(this); this.fBuffer.append("=");//$NON-NLS-1$ node.getValue().accept(this); return false; }
@Override public boolean visit(NormalAnnotation node) { this.fBuffer.append("@");//$NON-NLS-1$ node.getTypeName().accept(this); this.fBuffer.append("(");//$NON-NLS-1$ for (Iterator<MemberValuePair> it= node.values().iterator(); it.hasNext();) { MemberValuePair p= it.next(); p.accept(this); if (it.hasNext()) { this.fBuffer.append(",");//$NON-NLS-1$ } } this.fBuffer.append(")");//$NON-NLS-1$ return false; }
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, 5, image); proposals.add(proposal); }
public boolean visit(NormalAnnotation node) { this.buffer.append("@");//$NON-NLS-1$ node.getTypeName().accept(this); this.buffer.append("(");//$NON-NLS-1$ for (Iterator it = node.values().iterator(); it.hasNext(); ) { MemberValuePair p = (MemberValuePair) it.next(); p.accept(this); if (it.hasNext()) { this.buffer.append(",");//$NON-NLS-1$ } } this.buffer.append(")");//$NON-NLS-1$ return false; }
@SuppressWarnings("unchecked") public void writeProgramAnnotation(QProgram program) { QConversion conversion = program.getFacet(QConversion.class); if (conversion != null) { MarkerAnnotation conversionAnnotation = getAST().newMarkerAnnotation(); switch (conversion.getStatus()) { case POSSIBLE: break; case SUPPORTED: writeImport(Supported.class); conversionAnnotation.setTypeName(getAST().newSimpleName(Supported.class.getSimpleName())); getTarget().modifiers().add(conversionAnnotation); break; case TODO: writeImport(ToDo.class); conversionAnnotation.setTypeName(getAST().newSimpleName(ToDo.class.getSimpleName())); getTarget().modifiers().add(conversionAnnotation); break; case UNSUPPORTED: writeImport(Unsupported.class); conversionAnnotation.setTypeName(getAST().newSimpleName(Unsupported.class.getSimpleName())); getTarget().modifiers().add(conversionAnnotation); break; } } // @Program(name=) NormalAnnotation programAnnotation = getAST().newNormalAnnotation(); programAnnotation.setTypeName(getAST().newSimpleName(Program.class.getSimpleName())); MemberValuePair memberValuePair = getAST().newMemberValuePair(); memberValuePair.setName(getAST().newSimpleName("name")); StringLiteral stringLiteral = getAST().newStringLiteral(); stringLiteral.setLiteralValue(program.getName()); memberValuePair.setValue(stringLiteral); programAnnotation.values().add(memberValuePair); getTarget().modifiers().add(0, programAnnotation); }
private Optional<Expression> expectedException(Annotation annotation) { return ((NormalAnnotation) annotation).values().stream() .filter(value -> ((MemberValuePair) value).getName().getFullyQualifiedName().equals("expected")) .map(value -> ((MemberValuePair) value).getValue()).findFirst(); }
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 = JavaPluginImages.get( JavaPluginImages .IMG_TOOL_DELETE); // JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE); ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal( label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_ANNOTATION, image); proposals.add(proposal); }
/** {@inheritDoc} */ @Override public void endVisit(MemberValuePair node) { logger.warn("Method endVisitMemberRef for " + node + " for " + node + " not implemented!"); super.endVisit(node); }
/** {@inheritDoc} */ @Override public boolean visit(MemberValuePair node) { logger.warn("Method visitMemberValuePair for " + node + " not implemented!"); return super.visit(node); }
@Override public boolean visit(final MemberValuePair node) { return false; }
@Override public boolean visit(final MemberValuePair node) { // will never be visited assert false; return false; }