private void checkExtends(
     ClassDeclaration classDeclaration, PackageDeclaration packageDeclaration) {
   String scName = classDeclaration.getSuperclass().toString();
   int lt = scName.indexOf('<');
   if (lt != -1) {
     scName = scName.substring(0, lt);
   }
   int dot = scName.lastIndexOf('.');
   if (dot != -1) {
     String superClassPackageName = scName.substring(0, dot);
     if (!superClassPackageName.equals(packageDeclaration.getQualifiedName())) {
       scName = ""; // force error below
     } else {
       scName = scName.substring(dot + 1);
     }
   }
   if (!scName.equals(classDeclaration.getSimpleName() + "Gen")) {
     error(
         classDeclaration,
         classDeclaration.getQualifiedName()
             + " must extend "
             + classDeclaration.getQualifiedName()
             + "Gen for @Bean to work properly");
   }
 }
Example #2
0
 @Test(result = "NestedClass")
 String getSimpleName2() {
   return nested.getSimpleName();
 }
  @Override
  public void process() {
    final AnnotationTypeDeclaration beanAnn =
        (AnnotationTypeDeclaration) env_.getTypeDeclaration(Bean.class.getName());
    for (Declaration declaration : env_.getDeclarationsAnnotatedWith(beanAnn)) {
      try {
        if (!(declaration instanceof ClassDeclaration)) {
          error(declaration, "You can only annotate class declarations with @Bean");
          return;
        }

        Thing data = new Thing("data");

        ClassDeclaration classDeclaration = (ClassDeclaration) declaration;
        PackageDeclaration packageDeclaration = classDeclaration.getPackage();

        Map<String, AnnotationValue> beanValues =
            getAnnotationValues(classDeclaration, "com.javadude.annotation.Bean");

        checkExtends(classDeclaration, packageDeclaration); // check that the class extends XXXGen
        processSuperclass(data, beanValues); // process the superclass attribute
        processProperties(data, beanValues);
        processObservers(data, beanValues);
        processNullObjects(data, beanValues);
        processDelegates(data, beanValues);
        processDefaultMethods(data, classDeclaration);

        setValue(data, beanValues, "cloneable", false);
        setValue(data, beanValues, "defineEqualsAndHashCode", false);
        setValue(data, beanValues, "equalsAndHashCodeCallSuper", false);
        setValue(data, beanValues, "definePropertyNameConstants", false);
        setValue(data, beanValues, "defineCreatePropertyMap", false);
        data.put("date", new Date());
        data.put("year", Calendar.getInstance().get(Calendar.YEAR));
        data.put("className", classDeclaration.getSimpleName());
        data.put("packageName", packageDeclaration.getQualifiedName());

        data.checkAllValuesSet(declaration, this);

        Filer f = env_.getFiler();
        PrintWriter pw = f.createSourceFile(classDeclaration.getQualifiedName() + "Gen");

        if (nestedEclipse) {
          // debugging in eclipse -- reread the template each time
          FileReader fileReader =
              new FileReader(
                  workspace
                      + "/com.javadude.annotation/template/$packageName$/$className$Gen.java");
          template = new TemplateReader().readTemplate(fileReader);
        }
        int spacesForLeadingTabs = i(beanValues, "spacesForLeadingTabs", -1);
        String padding = null;
        if (spacesForLeadingTabs != -1) {
          padding = "";
          for (int i = 0; i < spacesForLeadingTabs; i++) {
            padding += ' ';
          }
        }
        template.process(new Symbols(data), pw, -1, padding);
        pw.close();
      } catch (ThreadDeath e) {
        throw e;
      } catch (ExpressionException e) {
        error(declaration, "@Bean generator error: " + e.getMessage());
      } catch (Throwable t) {
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        t.printStackTrace(printWriter);
        printWriter.close();
        error(declaration, "Unexpected exception: " + stringWriter.toString());
      }
    }
  }