Exemplo n.º 1
0
 @Test
 public void checkThatOtherMethodsAreNonStatic() throws NoSuchMethodException {
   Method spin = Earth.class.getDeclaredMethod("spin");
   assertFalse("Method spin() should not be static", Modifier.isStatic(spin.getModifiers()));
   Method warmUp = Earth.class.getDeclaredMethod("warmUp");
   assertFalse("Method warmUp() should not be static", Modifier.isStatic(warmUp.getModifiers()));
 }
Exemplo n.º 2
0
 @Test
 public void checkEarthIsAbstract() throws NoSuchMethodException {
   assertTrue("Earth class should be abstract", Modifier.isAbstract(Earth.class.getModifiers()));
   assertTrue(
       "Earth class spin() method should be abstract",
       Modifier.isAbstract(Earth.class.getMethod("spin").getModifiers()));
   assertTrue(
       "Earth class warmUp() method should be abstract",
       Modifier.isAbstract(Earth.class.getMethod("warmUp").getModifiers()));
 }
Exemplo n.º 3
0
 @Test
 public void checkThatSingletonContainsStaticPrivateInstanceField() {
   Field field = findSingletonStaticPrivateInstanceField();
   assertNotNull("No static private instance field found.", field);
   assertTrue(
       "Field pointing to the Earth instance should be static",
       Modifier.isStatic(field.getModifiers()));
   assertTrue(
       "Field pointing to the Earth instance should be private",
       Modifier.isPrivate(field.getModifiers()));
 }
Exemplo n.º 4
0
 @Test
 public void checkThatConstructorIsProtected() {
   for (Constructor<?> constr : Earth.class.getDeclaredConstructors()) {
     assertTrue(
         "Constructors should be protected to allow subclasses of singleton",
         Modifier.isProtected(constr.getModifiers()));
   }
 }
Exemplo n.º 5
0
 private Method checkThatSingletonContainsStaticInstanceAccessorMethod(Class clazz) {
   for (Method method : clazz.getDeclaredMethods()) {
     if (method.getReturnType() == clazz && method.getParameterTypes().length == 0) {
       assertTrue(
           "Method returning the Earth instance should be static",
           Modifier.isStatic(method.getModifiers()));
       return method;
     }
   }
   fail("No static accessor method found.");
   return null; // will never be called.
 }
Exemplo n.º 6
0
  public TypeUniverse() {
    Providers providers =
        Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend().getProviders();
    metaAccess = providers.getMetaAccess();
    constantReflection = providers.getConstantReflection();
    snippetReflection = Graal.getRequiredCapability(SnippetReflectionProvider.class);
    Unsafe theUnsafe = null;
    try {
      theUnsafe = Unsafe.getUnsafe();
    } catch (Exception e) {
      try {
        Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe");
        theUnsafeField.setAccessible(true);
        theUnsafe = (Unsafe) theUnsafeField.get(null);
      } catch (Exception e1) {
        throw (InternalError) new InternalError("unable to initialize unsafe").initCause(e1);
      }
    }
    unsafe = theUnsafe;

    Class<?>[] initialClasses = {
      void.class,
      boolean.class,
      byte.class,
      short.class,
      char.class,
      int.class,
      float.class,
      long.class,
      double.class,
      Object.class,
      Class.class,
      ClassLoader.class,
      String.class,
      Serializable.class,
      Cloneable.class,
      Test.class,
      TestMetaAccessProvider.class,
      List.class,
      Collection.class,
      Map.class,
      Queue.class,
      HashMap.class,
      LinkedHashMap.class,
      IdentityHashMap.class,
      AbstractCollection.class,
      AbstractList.class,
      ArrayList.class
    };
    for (Class<?> c : initialClasses) {
      addClass(c);
    }
    for (Field f : Constant.class.getDeclaredFields()) {
      int mods = f.getModifiers();
      if (f.getType() == Constant.class
          && Modifier.isPublic(mods)
          && Modifier.isStatic(mods)
          && Modifier.isFinal(mods)) {
        try {
          Constant c = (Constant) f.get(null);
          if (c != null) {
            constants.add(c);
          }
        } catch (Exception e) {
        }
      }
    }
    for (Class<?> c : classes) {
      if (c != void.class && !c.isArray()) {
        constants.add(snippetReflection.forObject(Array.newInstance(c, 42)));
      }
    }
    constants.add(snippetReflection.forObject(new ArrayList<>()));
    constants.add(snippetReflection.forObject(new IdentityHashMap<>()));
    constants.add(snippetReflection.forObject(new LinkedHashMap<>()));
    constants.add(snippetReflection.forObject(new TreeMap<>()));
    constants.add(snippetReflection.forObject(new ArrayDeque<>()));
    constants.add(snippetReflection.forObject(new LinkedList<>()));
    constants.add(snippetReflection.forObject("a string"));
    constants.add(snippetReflection.forObject(42));
    constants.add(snippetReflection.forObject(String.class));
    constants.add(snippetReflection.forObject(String[].class));
  }