List of usage examples for org.eclipse.jdt.core.dom MemberValuePair setName
public void setName(SimpleName name)
From source file:ch.acanda.eclipse.pmd.java.resolution.SuppressWarningsQuickFix.java
License:Open Source License
/** * Creates the member value pairs of the annotation. *//*from w ww . j ava 2 s .c om*/ @SuppressWarnings("unchecked") private void createAnnotationValues(final NormalAnnotation existingAnnotation, final NormalAnnotation annotation) { final AST ast = annotation.getAST(); final List<MemberValuePair> values = annotation.values(); final List<MemberValuePair> existingValues = existingAnnotation.values(); for (final MemberValuePair existingPair : existingValues) { if ("value".equals(existingPair.getName().getFullyQualifiedName())) { final MemberValuePair pair = (MemberValuePair) ast.createInstance(MemberValuePair.class); pair.setName(ASTUtil.copy(existingPair.getName())); pair.setValue(createArrayInitializer(existingPair.getValue())); values.add(pair); } else { values.add(ASTUtil.copy(existingPair)); } } }
From source file:com.crispico.flower.mp.codesync.code.java.adapter.JavaAnnotationModelAdapter.java
License:Open Source License
@Override public Iterable<?> getContainmentFeatureIterable(Object element, Object feature, Iterable<?> correspondingIterable) { if (AstCacheCodePackage.eINSTANCE.getAnnotation_Values().equals(feature)) { if (element instanceof NormalAnnotation) { return ((NormalAnnotation) element).values(); }// w w w .ja va 2 s . c om if (element instanceof SingleMemberAnnotation) { AST ast = AST.newAST(AST.JLS4); MemberValuePair pair = ast.newMemberValuePair(); Expression value = ((SingleMemberAnnotation) element).getValue(); ASTNode newValue = ASTNode.copySubtree(ast, value); pair.setName(ast.newSimpleName(CodeSyncCodePlugin.SINGLE_MEMBER_ANNOTATION_VALUE_NAME)); pair.setValue((Expression) newValue); return Collections.singletonList(pair); } return Collections.emptyList(); } return super.getContainmentFeatureIterable(element, feature, correspondingIterable); }
From source file:com.crispico.flower.mp.codesync.code.java.adapter.JavaMemberValuePairModelAdapter.java
License:Open Source License
@Override public void setValueFeatureValue(Object element, Object feature, Object value) { if (AstCacheCodePackage.eINSTANCE.getAnnotationValue_Name().equals(feature)) { MemberValuePair pair = (MemberValuePair) element; String name = (String) value; pair.setName(pair.getAST().newSimpleName(name)); }/*ww w . j av a 2 s. c o m*/ if (AstCacheCodePackage.eINSTANCE.getAnnotationValue_Value().equals(feature)) { MemberValuePair pair = (MemberValuePair) element; String expression = (String) value; pair.setValue(getExpressionFromString(pair.getAST(), expression)); } super.setValueFeatureValue(element, feature, value); }
From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.JavaSyncUtils.java
License:Open Source License
/** * Creates a new appropriate {@link Annotation java Annotation} from the * given {@link EAnnotation modelEAnnotation}. The returned * {@link Annotation} can be a {@link MarkerAnnotation}, a * {@link SingleMemberAnnotation} or a {@link NormalAnnotation} according to * the <code>modelEAnnotation</code> features. * //from w w w. j av a 2 s . com * @param ast * the parent {@link AST} used to create the new * {@link Annotation} * @param modelEAnnotation * the model element {@link EAnnotation} * @return a new unparented {@link Annotation} * * @author Luiza */ @SuppressWarnings("unchecked") public static Annotation createNewJavaAnnotationFromModelEAnnotation(AST ast, EAnnotation modelEAnnotation) { EMap<String, String> detailsMap = modelEAnnotation.getDetails(); Annotation newAnnotation = null; if (detailsMap.size() == 0) { newAnnotation = ast.newMarkerAnnotation(); } else if (detailsMap.size() == 1 && detailsMap.get("") != null) { String value = detailsMap.get(""); newAnnotation = ast.newSingleMemberAnnotation(); ((SingleMemberAnnotation) newAnnotation).setValue(makeExpression(ast, value)); } else { newAnnotation = ast.newNormalAnnotation(); for (Entry<String, String> mapEntry : detailsMap) { MemberValuePair mvp = ast.newMemberValuePair(); mvp.setName(ast.newSimpleName(mapEntry.getKey())); mvp.setValue(makeExpression(ast, mapEntry.getValue())); ((NormalAnnotation) newAnnotation).values().add(mvp); } } newAnnotation.setTypeName(ast.newName(modelEAnnotation.getSource())); return newAnnotation; }
From source file:com.halware.nakedide.eclipse.ext.annot.ast.AbstractAnnotationEvaluatorOrModifier.java
License:Open Source License
private MemberValuePair createMemberValuePair(AST ast, String elementName, Object value) { MemberValuePair newValuePair = ast.newMemberValuePair(); newValuePair.setName(ast.newSimpleName(elementName)); newValuePair.setValue(AstUtils.createExpression(ast, value)); return newValuePair; }
From source file:com.halware.nakedide.eclipse.ext.annot.utils.dali.ASTTools.java
License:Open Source License
@SuppressWarnings("unchecked") public static void addLiteralMemberValuePair(NormalAnnotation annotation, String name, String value) { AST ast = annotation.getAST();//ww w . j av a2s .com List list = (List) annotation.getStructuralProperty(NormalAnnotation.VALUES_PROPERTY); MemberValuePair newValuePair = ast.newMemberValuePair(); newValuePair.setName(ast.newSimpleName(name)); newValuePair.setValue(ASTTools.newStringLiteral(ast, value)); list.add(newValuePair); }
From source file:com.liferay.ide.project.core.modules.NewLiferayModuleProjectOpMethods.java
License:Open Source License
@SuppressWarnings("unchecked") public static void addProperties(File dest, List<String> properties) throws Exception { try {/* w w w . ja va 2 s. c om*/ if (properties == null || properties.size() < 1) { return; } ASTParser parser = ASTParser.newParser(AST.JLS8); String readContents = FileUtil.readContents(dest, true); parser.setSource(readContents.toCharArray()); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setResolveBindings(true); final CompilationUnit cu = (CompilationUnit) parser.createAST(new NullProgressMonitor()); cu.recordModifications(); Document document = new Document(new String(readContents)); cu.accept(new ASTVisitor() { @Override public boolean visit(NormalAnnotation node) { if (node.getTypeName().getFullyQualifiedName().equals("Component")) { ASTRewrite rewrite = ASTRewrite.create(cu.getAST()); AST ast = cu.getAST(); List<ASTNode> values = node.values(); boolean hasProperty = false; for (ASTNode astNode : values) { if (astNode instanceof MemberValuePair) { MemberValuePair pairNode = (MemberValuePair) astNode; if (pairNode.getName().getFullyQualifiedName().equals("property")) { Expression express = pairNode.getValue(); if (express instanceof ArrayInitializer) { ListRewrite lrw = rewrite.getListRewrite(express, ArrayInitializer.EXPRESSIONS_PROPERTY); ArrayInitializer initializer = (ArrayInitializer) express; List<ASTNode> expressions = (List<ASTNode>) initializer.expressions(); ASTNode propertyNode = null; for (int i = properties.size() - 1; i >= 0; i--) { StringLiteral stringLiteral = ast.newStringLiteral(); stringLiteral.setLiteralValue(properties.get(i)); if (expressions.size() > 0) { propertyNode = expressions.get(expressions.size() - 1); lrw.insertAfter(stringLiteral, propertyNode, null); } else { lrw.insertFirst(stringLiteral, null); } } } hasProperty = true; } } } if (hasProperty == false) { ListRewrite clrw = rewrite.getListRewrite(node, NormalAnnotation.VALUES_PROPERTY); ASTNode lastNode = values.get(values.size() - 1); ArrayInitializer newArrayInitializer = ast.newArrayInitializer(); MemberValuePair propertyMemberValuePair = ast.newMemberValuePair(); propertyMemberValuePair.setName(ast.newSimpleName("property")); propertyMemberValuePair.setValue(newArrayInitializer); clrw.insertBefore(propertyMemberValuePair, lastNode, null); ListRewrite newLrw = rewrite.getListRewrite(newArrayInitializer, ArrayInitializer.EXPRESSIONS_PROPERTY); for (String property : properties) { StringLiteral stringLiteral = ast.newStringLiteral(); stringLiteral.setLiteralValue(property); newLrw.insertAt(stringLiteral, 0, null); } } try (FileOutputStream fos = new FileOutputStream(dest)) { TextEdit edits = rewrite.rewriteAST(document, null); edits.apply(document); fos.write(document.get().getBytes()); fos.flush(); } catch (Exception e) { ProjectCore.logError(e); } } return super.visit(node); } }); } catch (Exception e) { ProjectCore.logError("error when adding properties to " + dest.getAbsolutePath(), e); } }
From source file:de.dentrassi.varlink.generator.JdtGenerator.java
License:Open Source License
@SuppressWarnings("unchecked") private void createInterface(final AST ast, final CompilationUnit cu, final Interface iface, final String name) { final TypeDeclaration td = ast.newTypeDeclaration(); cu.types().add(td);//from w w w . j av a 2s . co m td.setInterface(true); td.setName(ast.newSimpleName(name)); make(td, PUBLIC_KEYWORD); final NormalAnnotation ann = JdtHelper.addAnnotation(td, "de.dentrassi.varlink.spi.Interface"); { final MemberValuePair mvpName = ast.newMemberValuePair(); ann.values().add(mvpName); mvpName.setName(ast.newSimpleName("name")); mvpName.setValue(JdtHelper.newStringLiteral(ast, iface.getName())); } { final MemberValuePair mvpFactory = ast.newMemberValuePair(); ann.values().add(mvpFactory); mvpFactory.setName(ast.newSimpleName("factory")); final TypeLiteral fn = ast.newTypeLiteral(); fn.setType(ast.newSimpleType(ast.newName(name + "Impl.Factory"))); mvpFactory.setValue(fn); } // create types createTypes(td, iface); // create errors createErrors(td, iface); /* * * public interface Async { public CompletableFuture<List<Netdev>> list(); } * * public interface Sync { public List<Netdev> list(); } * * public Async async(); * * public default Sync sync() { return new Sync() { * * @Override public List<Netdev> list() { return Syncer.await(async().list()); } * }; } */ // create async { // public interface Async { ... } final TypeDeclaration async = ast.newTypeDeclaration(); td.bodyDeclarations().add(async); async.setInterface(true); make(async, PUBLIC_KEYWORD); async.setName(ast.newSimpleName("Async")); forMethods(ast, iface, (m, md) -> { make(md, PUBLIC_KEYWORD); async.bodyDeclarations().add(md); makeAsync(md); }); // public Async async(); final MethodDeclaration md = ast.newMethodDeclaration(); td.bodyDeclarations().add(md); md.setName(ast.newSimpleName("async")); make(md, PUBLIC_KEYWORD); final Type rt = ast.newSimpleType(ast.newSimpleName("Async")); md.setReturnType2(rt); } // create sync { // public interface Sync { ... } final TypeDeclaration sync = ast.newTypeDeclaration(); td.bodyDeclarations().add(sync); sync.setInterface(true); make(sync, PUBLIC_KEYWORD); sync.setName(ast.newSimpleName("Sync")); // methods forMethods(ast, iface, (m, md) -> { make(md, PUBLIC_KEYWORD); sync.bodyDeclarations().add(md); }); { final MethodDeclaration smd = ast.newMethodDeclaration(); smd.setName(ast.newSimpleName("sync")); make(smd, PUBLIC_KEYWORD, DEFAULT_KEYWORD); td.bodyDeclarations().add(smd); final Block body = ast.newBlock(); smd.setBody(body); final ReturnStatement ret = ast.newReturnStatement(); body.statements().add(ret); final ClassInstanceCreation cic = ast.newClassInstanceCreation(); cic.setType(ast.newSimpleType(ast.newName("Sync"))); ret.setExpression(cic); smd.setReturnType2(ast.newSimpleType(ast.newName("Sync"))); final AnonymousClassDeclaration acc = ast.newAnonymousClassDeclaration(); cic.setAnonymousClassDeclaration(acc); forMethods(ast, iface, (m, md) -> { make(md, PUBLIC_KEYWORD); acc.bodyDeclarations().add(md); final Block mbody = ast.newBlock(); md.setBody(mbody); // return Syncer.await(async().list()); final MethodInvocation await = ast.newMethodInvocation(); await.setExpression(ast.newName("de.dentrassi.varlink.spi.Syncer")); await.setName(ast.newSimpleName("await")); final MethodInvocation asyncCall = ast.newMethodInvocation(); asyncCall.setName(ast.newSimpleName("async")); final MethodInvocation mcall = ast.newMethodInvocation(); mcall.setName(ast.newSimpleName(m.getName())); mcall.setExpression(asyncCall); await.arguments().add(mcall); // add arguments for (final String argName : m.getParameters().keySet()) { mcall.arguments().add(ast.newSimpleName(argName)); } if (m.getReturnTypes().isEmpty()) { mbody.statements().add(ast.newExpressionStatement(await)); } else { final ReturnStatement rs = ast.newReturnStatement(); rs.setExpression(await); mbody.statements().add(rs); } }); } } }
From source file:es.bsc.servicess.ide.editors.ImplementationFormPage.java
License:Apache License
private void modifyConstraintInCompilationUnit(ICompilationUnit cu, String methodName, String constraintName, String constraintValue, String localtypeName) throws JavaModelException, MalformedTreeException, BadLocationException { if (cu != null) { Document document = new Document(cu.getSource()); // IDocument document = textFileBuffer.getDocument(); log.debug("Loading document for modify constraint " + constraintName + " in method " + methodName); ASTParser parser = ASTParser.newParser(AST.JLS3); // handles JDK // 1.0, 1.1, // 1.2, 1.3, // 1.4, 1.5, 1.6 parser.setSource(cu);// w ww .j av a 2 s . c o m // In order to parse 1.5 code, some compiler options need to be set // to 1.5 // Map options = JavaCore.getOptions(); // JavaCore.setComplianceOptions(JavaCore.VERSION_1_6, options); // parser.setCompilerOptions(options); CompilationUnit result = (CompilationUnit) parser.createAST(null); if (result != null) { result.recordModifications(); AST ast = result.getAST(); java.util.List<AbstractTypeDeclaration> types = result.types(); log.debug("pack: " + result.getPackage().toString() + " types: " + result.types().size()); if (result.types().size() > 0) { boolean find = false; for (AbstractTypeDeclaration type : types) { log.debug("Type: " + type.getName().getIdentifier() + "(" + type.getName().getFullyQualifiedName() + ")"); if (type.getName().getIdentifier().equals(localtypeName)) { MethodDeclaration[] methods = ((TypeDeclaration) type).getMethods(); for (MethodDeclaration m : methods) { log.debug("method FQDN: " + m.getName().getFullyQualifiedName() + " identifier: " + m.getName().getIdentifier()); if (m.getName().getIdentifier().equals(methodName)) { java.util.List<IExtendedModifier> mods = m.modifiers(); for (IExtendedModifier mod : mods) { log.debug("modifier: " + mod.getClass().toString()); if (mod.isAnnotation()) { if (((Annotation) mod).isNormalAnnotation()) { log.debug("annotation: " + ((NormalAnnotation) mod).getTypeName().toString()); if (((NormalAnnotation) mod).getTypeName().toString() .equals("Constraints")) { java.util.List<MemberValuePair> vals = ((NormalAnnotation) mod) .values(); MemberValuePair value = null; for (MemberValuePair v : vals) { log.debug("member: " + v.getName().getIdentifier()); if (v.getName().getIdentifier().equals(constraintName)) { value = v; break; } } Expression qn = ConstraintsUtils.convertValueToExpression( constraintName, constraintValue, ast); if (value == null) { value = ast.newMemberValuePair(); value.setName(ast.newSimpleName(constraintName)); value.setValue(qn); log.debug("Adding property to annotation: " + value.toString()); vals.add(value); } else { value.setValue(qn); log.debug("Changing direction: " + value.toString()); } find = true; break; } } } } if (find) break; else { ImportDeclaration imp = ast.newImportDeclaration(); imp.setName(ast.newName("integratedtoolkit.types.annotations.Constraints")); if (!result.imports().contains(imp)) result.imports().add(imp); NormalAnnotation annotation = ast.newNormalAnnotation(); annotation.setTypeName(ast.newSimpleName("Constraints")); java.util.List<MemberValuePair> vals = annotation.values(); MemberValuePair value = ast.newMemberValuePair(); value.setName(ast.newSimpleName(constraintName)); value.setValue(ConstraintsUtils.convertValueToExpression(constraintName, constraintValue, ast)); log.debug("Adding property to annotation: " + value.toString()); vals.add(value); m.modifiers().add(0, annotation); find = true; } } } if (find) break; } } if (find) { TextEdit edits = result.rewrite(document, cu.getJavaProject().getOptions(true)); edits.apply(document); String newSource = document.get(); cu.getBuffer().setContents(newSource); cu.save(null, true); log.debug("writting modifications " + newSource); } else { log.warn("Varaible and annotation not found"); } } else { log.warn("No types found in the Compilation unit from AST"); } } else { log.error("Error parsing Compilation unit with AST"); } } else { log.error("Error getting Interface Compilation Unit"); } }
From source file:es.bsc.servicess.ide.editors.ImplementationFormPage.java
License:Apache License
/** * Modify parameter element//from w w w . java 2 s . c om * @param serviceClass Orchestration class name * @param methodName Element method name * @param p Parameter * @throws PartInitException * @throws JavaModelException * @throws MalformedTreeException * @throws BadLocationException * @throws SAXException * @throws IOException * @throws ParserConfigurationException */ private void modifyDirection(String serviceClass, String methodName, CoreElementParameter p) throws PartInitException, JavaModelException, MalformedTreeException, BadLocationException, SAXException, IOException, ParserConfigurationException { log.debug("Modifying direction for core element" + serviceClass + "." + methodName + " parameter " + p.getName() + "(" + p.getDirection() + ")"); ICompilationUnit cu = getCEInterface(serviceClass, ((ServiceFormEditor) getEditor()).getProject(), ((ServiceFormEditor) getEditor()).getProjectMetadata()); if (cu != null) { Document document = new Document(cu.getSource()); log.debug(document.get()); String localtypeName = serviceClass.subSequence(serviceClass.lastIndexOf(".") + 1, serviceClass.length()) + "Itf"; ASTParser parser = ASTParser.newParser(AST.JLS3); // handles JDK // 1.0, 1.1, // 1.2, 1.3, // 1.4, 1.5, 1.6 parser.setSource(cu); CompilationUnit result = (CompilationUnit) parser.createAST(null); if (result != null) { result.recordModifications(); log.debug(result.toString()); AST ast = result.getAST(); java.util.List<AbstractTypeDeclaration> types = result.types(); log.debug("pack: " + result.getPackage().toString() + " types: " + result.types().size()); if (result.types().size() > 0) { boolean find = false; for (AbstractTypeDeclaration type : types) { log.debug("Type: " + type.getName().getIdentifier() + "(" + type.getName().getFullyQualifiedName() + ")"); if (type.getName().getIdentifier().equals(localtypeName)) { MethodDeclaration[] methods = ((TypeDeclaration) type).getMethods(); for (MethodDeclaration m : methods) { log.debug("method FQDN: " + m.getName().getFullyQualifiedName() + " identifier: " + m.getName().getIdentifier()); if (m.getName().getIdentifier().equals(methodName)) { java.util.List<SingleVariableDeclaration> pars = m.parameters(); for (SingleVariableDeclaration var : pars) { log.debug("var FQDN: " + var.getName().getFullyQualifiedName() + " identifier: " + var.getName().getIdentifier()); if (var.getName().getIdentifier().equals(p.getName())) { java.util.List<IExtendedModifier> mods = var.modifiers(); for (IExtendedModifier mod : mods) { log.debug("modifier: " + mod.getClass().toString()); if (mod.isAnnotation()) { if (((Annotation) mod).isNormalAnnotation()) { log.debug("annotation: " + ((NormalAnnotation) mod) .getTypeName().toString()); if (((NormalAnnotation) mod).getTypeName().toString() .equals("Parameter")) { java.util.List<MemberValuePair> vals = ((NormalAnnotation) mod) .values(); MemberValuePair dir_value = null; for (MemberValuePair v : vals) { log.debug("member: " + v.getName().getIdentifier()); if (v.getName().getIdentifier() .equals("direction")) { dir_value = v; break; } } if (dir_value == null) { dir_value = ast.newMemberValuePair(); dir_value.setName(ast.newSimpleName("direction")); QualifiedName qn = ast.newQualifiedName( ast.newSimpleName("Direction"), ast.newSimpleName(p.getDirection())); dir_value.setValue(qn); log.debug("Adding property to annotation: " + dir_value.toString()); vals.add(dir_value); } else { QualifiedName ex = (QualifiedName) dir_value .getValue(); log.debug("ValueClass: " + ex.getClass()); ex.setName(ast.newSimpleName(p.getDirection())); log.debug("Changing direction: " + dir_value.toString()); } find = true; break; } } } } if (find) break; } } if (find) break; } } if (find) break; } } if (find) { TextEdit edits = result.rewrite(document, cu.getJavaProject().getOptions(true)); edits.apply(document); String newSource = document.get(); cu.getBuffer().setContents(newSource); cu.save(null, true); log.debug("writting modifications " + newSource); } else { log.warn("Varaible and annotation not found"); } } else { log.warn("No types found in the Compilation unit from AST"); } } else { log.error("Error parsing Compilation unit with AST"); } } else { log.error("Error getting Interface Compilation Unit"); } }