private String packageNameFromCompilationUnit() { PackageDeclaration packageDeclaration = compilationUnit.getPackage(); if (packageDeclaration == null) { throw new IllegalArgumentException("Source file does not declare a package. Default package is unsupported."); } return packageDeclaration.getName().toString(); } }
@Override public void visit(PackageDeclaration n, Object arg) { if (pack == null) { pack = new CGEntry(CGEntry.TYPE.PACKAGE, null, null, n.getName().toString()); if (isDebug()) System.out.println("P:" + pack); list.add(pack); } super.visit(n, arg); }
private AnnotationExpr findAnnotation(final BodyDeclaration n, String fullyQualifiedName, boolean foundAnnImport) { final String simpleName = ClassUtils.getShortClassName(fullyQualifiedName); final List<AnnotationExpr> annotations = n.getAnnotations() != null ? n.getAnnotations() : new ArrayList<AnnotationExpr>(); for (AnnotationExpr ae : annotations) { final String name = ae.getName().toString(); if ((simpleName.equals(name) && foundAnnImport)) { LOG.info("found " + ae + " on " + getTypeOrFieldNameForMsg(n) + "."); return ae; } if (fullyQualifiedName.equals(name)) { LOG.info("found " + ae + " on " + getTypeOrFieldNameForMsg(n) + "."); return ae; } } return null; }
public void build(DumpVisitorFactory factory, List<String> excludedMethods) { UnitTestVisitor unitTestVisitor = new UnitTestVisitor(name); // Iterator<ImportDeclaration> itImports = compilationUnit.getImports().iterator(); while (itImports.hasNext()) { if (GroovyTestGeneration.class.getName().equals(itImports.next().getName().toString())) { itImports.remove(); } } // for (String dep : deps) { int i = dep.lastIndexOf("."); String depPackage = dep.substring(0, i) + ".groovy"; String depImport = depPackage + dep.substring(i); compilationUnit.getImports().add(new ImportDeclaration(new NameExpr(depImport), false, false)); } // unitTestVisitor.visit(compilationUnit, excludedMethods); sb.append(compilationUnit.toString(factory)); }
private boolean imported(List<ImportDeclaration> imports, String fullyQualifiedName) { final String packageName = ClassUtils.getPackageName(fullyQualifiedName); for (final ImportDeclaration i : imports) { if (!i.isStatic()) { final String importName = i.getName().toString(); if (i.isAsterisk()) { if (packageName.equals(importName)) { if ( LOG.isDebugEnabled() ) { LOG.debug("found import " + packageName + ".* on " + getTypeNameForMsg(i) + "."); } return true; } } else { if (fullyQualifiedName.equals(importName)) { if ( LOG.isDebugEnabled() ) { LOG.debug("found import " + fullyQualifiedName + " on " + getTypeNameForMsg(i) + "."); } return true; } } } } return false; }
@SuppressWarnings("unchecked") public XAnnotation<?> parse(final AnnotationExpr annotationElement) throws AnnotationExpressionParseException { Validate.notNull(annotationElement, "Annotation expression must not be null."); final String className = annotationElement.getName().toString(); try { final Class<?> draftClass = ClassUtils.getClass(classLoader, className); if (!Annotation.class.isAssignableFrom(draftClass)) throw new AnnotationExpressionParseException(annotationElement, new IllegalArgumentException(MessageFormat.format( "The class [{0}] is not an annotation class.", draftClass.getName()))); final Class<? extends Annotation> annotationClass = (Class<? extends Annotation>) draftClass; final XAnnotationField<?>[] fields = parseFields(annotationElement, annotationClass); @SuppressWarnings("rawtypes") final XAnnotation<?> xannotation = new XAnnotation(annotationClass, fields); return xannotation; } catch (ClassNotFoundException cnfex) { throw new AnnotationExpressionParseException(annotationElement, new AnnotationClassNotFoundException(className, cnfex)); } }
@Override public void visit(MethodDeclaration method, A result) { if (CollectionUtils.isEmpty(method.getAnnotations())) { return; } for (AnnotationExpr annotation : method.getAnnotations()) { if (!annotation.getClass().equals(NormalAnnotationExpr.class)) { continue; } NormalAnnotationExpr annot = (NormalAnnotationExpr) annotation; if (annot.getName().toString().equals(SampleCode.class.getSimpleName()) && !CollectionUtils.isEmpty(annot.getPairs())) { for (MemberValuePair pair : annot.getPairs()) { // get the 'api' parameter from the annotation to let us know which api this function belongs to if (StringUtils.equals(pair.getName(), "api") && !StringUtils.isBlank(pair.getValue().toString())) { result.put(getCacheRowKey(type, pair.getValue().toString().replace("\"", "")), stripTestPrefix(method.getName()), stripCurlyBrackets(method.getBody().toString())); return; } } } } } }
throw new IllegalStateException("The additional imports should not be static or star imports"); final boolean imported = imported(imports, aImport.getName().toString()); if (!imported) { LOG.info("adding import " + aImport.getName().toString() + " to " + getTypeNameForMsg(n) + "."); imports.add(aImport);
@Override public final NodeData resolve(Node node, String mappedClass) { if (!(node instanceof FieldDeclaration)) { throw new IllegalArgumentException("this annotation belongs only on FieldDeclaration"); } final FieldDeclaration field = (FieldDeclaration) node; if (ResolverUtil.canFieldBeAnnotated(field)) { final TypeDeclaration dclr = (TypeDeclaration) node.getParentNode(); final String name = dclr.getName(); final String pckg = ((CompilationUnit) dclr.getParentNode()).getPackage().getName().toString(); final String fullyQualifiedClass = pckg + "." + name; final boolean mappedColumn = OjbUtil.isMappedColumn(mappedClass, ParserUtil.getFieldName(field), descriptorRepositories); if (mappedColumn) { return getAnnotationNodes(fullyQualifiedClass, ParserUtil.getFieldName(field), mappedClass); } } return null; }
@Override public NodeData resolve(Node node, String mappedClass) { if (!(node instanceof ClassOrInterfaceDeclaration)) { throw new IllegalArgumentException("this annotation belongs only on ClassOrInterfaceDeclaration"); } final TypeDeclaration dclr = (TypeDeclaration) node; if (!(dclr.getParentNode() instanceof CompilationUnit)) { //handling nested classes return null; } final String name = dclr.getName(); final String pckg = ((CompilationUnit) dclr.getParentNode()).getPackage().getName().toString(); final String enclosingClass = pckg + "." + name; final Collection<String> customizedFieldsOnNode = getFieldsOnNode(dclr, getCustomizedFields(mappedClass)); if (customizedFieldsOnNode == null || customizedFieldsOnNode.isEmpty()) { LOG.info(ResolverUtil.logMsgForClass(enclosingClass, mappedClass) + " has no customized fields"); return null; } return new NodeData(new SingleMemberAnnotationExpr(new NameExpr(SIMPLE_NAME), new NameExpr("CreateCustomizerFor" + customizedFieldsOnNode.toString())), new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); }
@Override public NodeData resolve(Node node, String mappedClass) { if (!(node instanceof ClassOrInterfaceDeclaration)) { throw new IllegalArgumentException("this annotation belongs only on ClassOrInterfaceDeclaration"); } final TypeDeclaration dclr = (TypeDeclaration) node; if (!(dclr.getParentNode() instanceof CompilationUnit)) { //handling nested classes return null; } final String name = dclr.getName(); final String pckg = ((CompilationUnit) dclr.getParentNode()).getPackage().getName().toString(); final String enclosingClass = pckg + "." + name; if (!enclosingClass.equals(mappedClass)) { return new NodeData(new MarkerAnnotationExpr(new NameExpr(SIMPLE_NAME)), new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); } return null; } }
@Override public NodeData resolve(Node node, String mappedClass) { if (!(node instanceof ClassOrInterfaceDeclaration)) { throw new IllegalArgumentException("this annotation belongs only on ClassOrInterfaceDeclaration"); } final TypeDeclaration dclr = (TypeDeclaration) node; if (!(dclr.getParentNode() instanceof CompilationUnit)) { //handling nested classes return null; } final String name = dclr.getName(); final String pckg = ((CompilationUnit) dclr.getParentNode()).getPackage().getName().toString(); final String enclosingClass = pckg + "." + name; final ClassDescriptor cd = OjbUtil.findClassDescriptor(enclosingClass, descriptorRepositories); if (cd != null) { final String tableName = getMappedTable(enclosingClass); if (tableName == null) { LOG.error(ResolverUtil.logMsgForClass(enclosingClass, mappedClass) + " table could not be found"); return null; } return new NodeData(new NormalAnnotationExpr(new NameExpr(SIMPLE_NAME), Collections.singletonList(new MemberValuePair("name", new StringLiteralExpr(upperCaseTableName ? tableName.toUpperCase() : tableName)))), new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); } return null; }
@Override public NodeData resolve(Node node, String mappedClass) { if (!(node instanceof ClassOrInterfaceDeclaration)) { throw new IllegalArgumentException("this annotation belongs only on ClassOrInterfaceDeclaration"); } final TypeDeclaration dclr = (TypeDeclaration) node; if (!(dclr.getParentNode() instanceof CompilationUnit)) { //handling nested classes return null; } final String name = dclr.getName(); final String pckg = ((CompilationUnit) dclr.getParentNode()).getPackage().getName().toString(); final String enclosingClass = pckg + "." + name; final ClassDescriptor cd = OjbUtil.findClassDescriptor(enclosingClass, descriptorRepositories); if (cd != null) { return new NodeData(new MarkerAnnotationExpr(new NameExpr(SIMPLE_NAME)), new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); } return null; } }