public Field findSingletonStaticPrivateInstanceField() { for (Field field : Earth.class.getDeclaredFields()) { if (field.getType() == Earth.class) { return field; } } return null; }
@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())); }
/** * @throws NoSuchRecordFieldException * @throws IOException */ @Test public void testComputedField() throws NoSuchRecordFieldException, IOException { List<Panel> panelList = database.getPanels(); for (Panel panel : panelList) { List<Field> fieldList = panel.getFields(); for (Iterator<Record> recordIterator = panel.recordIterator(); recordIterator.hasNext(); ) { Record record = recordIterator.next(); Iterator<Field> fieldIterator = fieldList.iterator(); while (fieldIterator.hasNext()) { Field field = fieldIterator.next(); if (field.getInitialization() != null) { assertTrue(field.getInitialization() != null); if (field.isComputedField()) { assertTrue(field.isComputedField()); assertTrue(record.getValueAsString(field.getNumber()) == null); } else { assertFalse(field.isComputedField()); assertTrue(record.getValueAsString(field.getNumber()) != null); } } else { if (field.getLink() == null || field.getLink().getType() == LinkType.DATA_LINK) { assertTrue(record.getValueAsString(field.getNumber()) != null); } } } } } }
private void replaceEarth(String earthclass) throws Exception { System.setProperty("earthclass", earthclass); Field instance = findSingletonStaticPrivateInstanceField(); instance.setAccessible(true); instance.set(null, null); }
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)); }