Пример #1
1
 @Override
 public int hashCode() {
   int result = super.hashCode();
   result = 31 * result + (context != null ? context.hashCode() : 0);
   result = 31 * result + (className != null ? className.hashCode() : 0);
   result = 31 * result + (superClass != null ? superClass.hashCode() : 0);
   result = 31 * result + (interfaces != null ? interfaces.hashCode() : 0);
   result = 31 * result + (scope != null ? scope.hashCode() : 0);
   result = 31 * result + (isArray ? 1 : 0);
   result = 31 * result + dimensions;
   result = 31 * result + (isInterface ? 1 : 0);
   result = 31 * result + (isAbstract ? 1 : 0);
   result = 31 * result + (isFinal ? 1 : 0);
   result = 31 * result + (isStatic ? 1 : 0);
   result = 31 * result + (isInner ? 1 : 0);
   result = 31 * result + (methods != null ? methods.hashCode() : 0);
   result = 31 * result + (fields != null ? fields.hashCode() : 0);
   result = 31 * result + (constructors != null ? constructors.hashCode() : 0);
   result = 31 * result + (typeVariables != null ? typeVariables.hashCode() : 0);
   result = 31 * result + (reifiedFormOf != null ? reifiedFormOf.hashCode() : 0);
   result = 31 * result + (_nameCache != null ? _nameCache.hashCode() : 0);
   result = 31 * result + (_methodsCache != null ? Arrays.hashCode(_methodsCache) : 0);
   result = 31 * result + (_fieldsCache != null ? Arrays.hashCode(_fieldsCache) : 0);
   result = 31 * result + (_constructorsCache != null ? Arrays.hashCode(_constructorsCache) : 0);
   result = 31 * result + (generatedCache != null ? generatedCache.hashCode() : 0);
   return result;
 }
Пример #2
0
 public BuildMetaClass(Context context, String name) {
   super(null);
   this.className = name;
   this.context = Context.create(context);
   this.context.addVariable(Variable.create("this", this));
   context.attachClass(this);
 }
Пример #3
0
  /**
   * Tests that it is safe to call ensureMemberExposed() on any type of InjectionPoint. (This was an
   * actual bug that was not caught by the existing integration tests).
   */
  public void testEnsureMemberExposedWithConstructorInjectionPoint() throws Exception {
    final ClassStructureBuilder<? extends ClassStructureBuilder<?>> structureBuilder =
        ClassBuilder.define("my.FakeBootstrapper").publicScope().body();

    IOCProcessingContext processingContext =
        IOCProcessingContext.Builder.create()
            .logger(
                new TreeLogger() {
                  @Override
                  public TreeLogger branch(
                      Type type, String msg, Throwable caught, HelpInfo helpInfo) {
                    return null;
                  }

                  @Override
                  public boolean isLoggable(Type type) {
                    return false;
                  }

                  @Override
                  public void log(Type type, String msg, Throwable caught, HelpInfo helpInfo) {
                    System.out.println(type.getLabel() + ": " + msg);
                    if (caught != null) {
                      caught.printStackTrace();
                    }
                  }
                })
            .sourceWriter(new StringSourceWriter())
            .context(Context.create())
            .bootstrapClassInstance(structureBuilder.getClassDefinition())
            .bootstrapBuilder(structureBuilder)
            .blockBuilder(Stmt.do_())
            .packages(Collections.singleton(ConstructorInjectedBean.class.getPackage().getName()))
            .build();
    InjectionContext ctx =
        InjectionContext.Builder.create().processingContext(processingContext).build();
    MetaConstructor constructor =
        MetaClassFactory.get(ConstructorInjectedBean.class).getConstructor(FooService.class);
    InjectionPoint<Inject> injectionPoint =
        new InjectionPoint<Inject>(
            new Inject() {

              @Override
              public Class<? extends Annotation> annotationType() {
                return Inject.class;
              }
            },
            TaskType.Parameter,
            constructor,
            null,
            null,
            null,
            constructor.getParameters()[0],
            null,
            ctx);

    // holy crap that was a lot of setup. Here comes the actual test:

    injectionPoint.ensureMemberExposed();
  }
Пример #4
0
 public static Statement generate(final Context context, final Object o) {
   if (o instanceof VariableReference) {
     return context.getVariable(((VariableReference) o).getName());
   } else if (o instanceof Variable) {
     final Variable v = (Variable) o;
     if (context.isScoped(v)) {
       return v.getReference();
     } else {
       if (context.isPermissiveMode()) {
         return v.getReference();
       } else {
         throw new OutOfScopeException(
             "variable cannot be referenced from this scope: " + v.getName());
       }
     }
   } else if (o instanceof Statement) {
     ((Statement) o).generate(context);
     return (Statement) o;
   } else {
     return LiteralFactory.getLiteral(context, o);
   }
 }
Пример #5
0
  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof BuildMetaClass)) return false;
    if (!super.equals(o)) return false;

    BuildMetaClass that = (BuildMetaClass) o;

    if (dimensions != that.dimensions) return false;
    if (isAbstract != that.isAbstract) return false;
    if (isArray != that.isArray) return false;
    if (isFinal != that.isFinal) return false;
    if (isInner != that.isInner) return false;
    if (isInterface != that.isInterface) return false;
    if (isStatic != that.isStatic) return false;
    if (!Arrays.equals(_constructorsCache, that._constructorsCache)) return false;
    if (!Arrays.equals(_fieldsCache, that._fieldsCache)) return false;
    if (!Arrays.equals(_methodsCache, that._methodsCache)) return false;
    if (_nameCache != null ? !_nameCache.equals(that._nameCache) : that._nameCache != null)
      return false;
    if (className != null ? !className.equals(that.className) : that.className != null)
      return false;
    if (constructors != null ? !constructors.equals(that.constructors) : that.constructors != null)
      return false;
    if (context != null ? !context.equals(that.context) : that.context != null) return false;
    if (fields != null ? !fields.equals(that.fields) : that.fields != null) return false;
    if (generatedCache != null
        ? !generatedCache.equals(that.generatedCache)
        : that.generatedCache != null) return false;
    if (interfaces != null ? !interfaces.equals(that.interfaces) : that.interfaces != null)
      return false;
    if (methods != null ? !methods.equals(that.methods) : that.methods != null) return false;
    if (reifiedFormOf != null
        ? !reifiedFormOf.equals(that.reifiedFormOf)
        : that.reifiedFormOf != null) return false;
    if (scope != that.scope) return false;
    if (superClass != null ? !superClass.equals(that.superClass) : that.superClass != null)
      return false;
    if (typeVariables != null
        ? !typeVariables.equals(that.typeVariables)
        : that.typeVariables != null) return false;

    return true;
  }
Пример #6
0
  public static void assertAssignableTypes(
      final Context context, final MetaClass from, final MetaClass to) {
    if (!to.asBoxed().isAssignableFrom(from.asBoxed())) {
      if (to.isArray()
          && from.isArray()
          && GenUtil.getArrayDimensions(to) == GenUtil.getArrayDimensions(from)
          && to.getOuterComponentType().isAssignableFrom(from.getOuterComponentType())) {
        return;
      }

      if (!context.isPermissiveMode()) {
        if (classAliases.contains(from.getFullyQualifiedName())
            && classAliases.contains(to.getFullyQualifiedName())) {
          // handle convertibility between MetaClass API and java Class reference.
          return;
        }

        throw new InvalidTypeException(
            to.getFullyQualifiedName() + " is not assignable from " + from.getFullyQualifiedName());
      }
    }
  }
Пример #7
0
  @Override
  public String toJavaString() {
    if (generatedCache != null) return generatedCache;

    StringBuilder buf = new StringBuilder(512);

    if (classComment != null) {
      buf.append(new Comment(classComment).generate(null)).append("\n");
    }

    context.addVariable(Variable.create("this", this));

    for (Annotation a : annotations) {
      buf.append(new AnnotationLiteral(a).getCanonicalString(context));
      buf.append(" ");
    }

    if (!annotations.isEmpty()) buf.append("\n");

    buf.append("\n");

    buf.append(scope.getCanonicalName());

    if (isAbstract) {
      buf.append(" abstract");
    }

    if (isStatic) {
      buf.append(" static");
    }

    if (isInterface()) {
      buf.append(" interface ").append(getName());
    } else {
      buf.append(" class ").append(getName());
    }

    if (getSuperClass() != null) {
      buf.append(" extends ")
          .append(LoadClassReference.getClassReference(getSuperClass(), context));
    }

    if (interfaces.size() != 0) {
      buf.append(" implements ");

      Iterator<MetaClass> iter = interfaces.iterator();
      while (iter.hasNext()) {
        buf.append(LoadClassReference.getClassReference(iter.next(), context));
        if (iter.hasNext()) buf.append(", ");
      }
    }

    superClass = (superClass != null) ? superClass : MetaClassFactory.get(Object.class);
    context.addVariable(Variable.create("super", superClass));

    buf.append(" {\n");

    buf.append(membersToString());

    StringBuilder headerBuffer = new StringBuilder(128);

    if (!getPackageName().isEmpty() && !isInner)
      headerBuffer.append("package ").append(getPackageName()).append(";\n");

    if (!context.getRequiredImports().isEmpty()) headerBuffer.append("\n");

    if (!isInner) {
      for (String cls : context.getRequiredImports()) {
        if (getFullyQualifiedName().equals(cls)) {
          continue;
        } else {
          String pkg = getPackageName();
          if (cls.startsWith(pkg)) {
            if (cls.substring(pkg.length() + 1).indexOf('.') == -1) {
              continue;
            }
          }
        }

        headerBuffer.append("import ").append(cls).append(";\n");
      }
    }

    return generatedCache =
        PrettyPrinter.prettyPrintJava(headerBuffer.toString() + buf.append("}\n").toString());
  }
Пример #8
0
  public static void assertIsIterable(final Statement statement) {
    final Class<?> cls = statement.getType().asClass();

    if (!cls.isArray() && !Iterable.class.isAssignableFrom(cls))
      throw new TypeNotIterableException(statement.generate(Context.create()));
  }