List of usage examples for java.lang.reflect Field toString
public String toString()
From source file:Main.java
public static void main(String[] args) throws Exception { MyClass c = new MyClass(); Class cls = c.getClass();/*from www. j a v a 2 s. c o m*/ Field sField = cls.getField("string1"); System.out.println("Public field found: " + sField.toString()); }
From source file:Main.java
public static void main(String[] args) { try {/*from www . ja v a2 s . co m*/ MyClass c = new MyClass(); Class cls = c.getClass(); Field lVal = cls.getDeclaredField("l"); System.out.println("Field = " + lVal.toString()); } catch (Exception e) { System.out.println(e.toString()); } }
From source file:tech.sirwellington.alchemy.generator.ObjectGenerators.java
private static <T> void tryInjectField(T instance, Field field, Map<Class<?>, AlchemyGenerator<?>> generatorMappings) { try {/*from w w w . jav a2 s . c o m*/ injectField(instance, field, generatorMappings); } catch (IllegalAccessException | IllegalArgumentException ex) { LOG.warn("Could not inject field {}", field.toString(), ex); } }
From source file:edu.umass.cs.gigapaxos.paxospackets.PaxosPacket.java
protected static boolean isStatic(Field field) { String[] tokens = field.toString().split("\\s+"); for (String token : tokens) if (token.equals("static")) return true; return false; }
From source file:net.sourceforge.stripes.integration.spring.SpringHelper.java
/** * Looks for all methods and fields annotated with {@code @SpringBean} and attempts * to lookup and inject a managed bean into the field/property. If any annotated * element cannot be injected an exception is thrown. * * @param bean the bean into which to inject spring beans * @param ctx the Spring application context *//*from w ww .ja va 2 s. c o m*/ public static void injectBeans(Object bean, ApplicationContext ctx) { // First inject any values using annotated methods for (Method m : getMethods(bean.getClass())) { try { SpringBean springBean = m.getAnnotation(SpringBean.class); boolean nameSupplied = !"".equals(springBean.value()); String name = nameSupplied ? springBean.value() : methodToPropertyName(m); Class<?> beanType = m.getParameterTypes()[0]; Object managedBean = findSpringBean(ctx, name, beanType, !nameSupplied); m.invoke(bean, managedBean); } catch (Exception e) { throw new StripesRuntimeException( "Exception while trying to lookup and inject " + "a Spring bean into a bean of type " + bean.getClass().getSimpleName() + " using method " + m.toString(), e); } } // And then inject any properties that are annotated for (Field f : getFields(bean.getClass())) { try { SpringBean springBean = f.getAnnotation(SpringBean.class); boolean nameSupplied = !"".equals(springBean.value()); String name = nameSupplied ? springBean.value() : f.getName(); Object managedBean = findSpringBean(ctx, name, f.getType(), !nameSupplied); f.set(bean, managedBean); } catch (Exception e) { throw new StripesRuntimeException( "Exception while trying to lookup and inject " + "a Spring bean into a bean of type " + bean.getClass().getSimpleName() + " using field access on field " + f.toString(), e); } } }
From source file:org.jenkinsci.plugins.workflow.support.steps.ExecutorStepTest.java
@Initializer(before = InitMilestone.JOB_LOADED) public static void replaceWorkspacePath() throws Exception { final File prj = new File(Jenkins.getInstance().getRootDir(), "jobs/p"); final File workspace = new File(prj, "workspace"); final String ORIG_WS = "/space/tmp/AbstractStepExecutionImpl-upgrade/jobs/p/workspace"; final String newWs = workspace.getAbsolutePath(); File controlDir = new File(workspace, ".eb6272d3"); if (!controlDir.isDirectory()) { return;/*from w w w .j av a2 s. co m*/ } System.err.println("Patching " + controlDir); RiverReader.customResolver = new ObjectResolver() { @Override public Object readResolve(Object replacement) { Class<?> c = replacement.getClass(); //System.err.println("replacing " + c.getName()); while (c != Object.class) { for (Field f : c.getDeclaredFields()) { if (f.getType() == String.class) { try { f.setAccessible(true); Object v = f.get(replacement); if (ORIG_WS.equals(v)) { //System.err.println("patching " + f); f.set(replacement, newWs); patchedFields.add(f.toString()); } else if (newWs.equals(v)) { //System.err.println(f + " was already patched, somehow?"); } else { //System.err.println("some other value " + v + " for " + f); } } catch (Exception x) { x.printStackTrace(); } } } c = c.getSuperclass(); } return replacement; } @Override public Object writeReplace(Object original) { throw new IllegalStateException(); } }; Files.walkFileTree(prj.toPath(), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { File f = file.toFile(); String name = f.getName(); if (name.equals("program.dat")) { /* TODO could not get this to work; stream appeared corrupted: patchedFiles.add(name); String origContent = FileUtils.readFileToString(f, StandardCharsets.ISO_8859_1); String toReplace = String.valueOf((char) Protocol.ID_STRING_SMALL) + String.valueOf((char) ORIG_WS.length()) + ORIG_WS; int newLen = newWs.length(); String replacement = String.valueOf((char) Protocol.ID_STRING_MEDIUM) + String.valueOf((char) (newLen & 0xff00) >> 8) + String.valueOf((char) newLen & 0xff) + newWs; // TODO breaks if not ASCII String replacedContent = origContent.replace(toReplace, replacement); assertNotEquals("failed to replace " + toReplace + "", replacedContent, origContent); FileUtils.writeStringToFile(f, replacedContent, StandardCharsets.ISO_8859_1); */ } else { String origContent = FileUtils.readFileToString(f, StandardCharsets.ISO_8859_1); String replacedContent = origContent.replace(ORIG_WS, newWs); if (!replacedContent.equals(origContent)) { patchedFiles.add(name); FileUtils.writeStringToFile(f, replacedContent, StandardCharsets.ISO_8859_1); } } return super.visitFile(file, attrs); } }); FilePath controlDirFP = new FilePath(controlDir); controlDirFP.child("jenkins-result.txt").write("0", null); FilePath log = controlDirFP.child("jenkins-log.txt"); log.write(log.readToString() + "simulated later output\n", null); }
From source file:com.glaf.core.util.ReflectUtils.java
public static void setField(Field field, Object object, Object value) { try {// w ww . j a v a 2s . co m field.setAccessible(true); field.set(object, value); } catch (IllegalArgumentException e) { throw new RuntimeException("Could not set field " + field.toString(), e); } catch (IllegalAccessException e) { throw new RuntimeException("Could not set field " + field.toString(), e); } }
From source file:ru.portal.gwt.gwtportal.server.GWTServiceImpl.java
@Override public List<TableFieldsDto> getTableNameWithFields(TableDto tableDto) { List<TableFieldsDto> result = new ArrayList<>(); Set<EntityType<?>> set = tableService.getEntityTypesByAnnotationClass(PortalTable.class); if (tableDto == null) { for (EntityType<?> entityType : set) { TableDto dto = new TableDto(entityType.getBindableJavaType().getName(), entityType.getBindableJavaType().getAnnotation(PortalTable.class).title(), entityType.getBindableJavaType().getName()); result.add(dto);/*from w w w.ja v a2 s.c o m*/ // List<User> users = new ArrayList<>(); // for (int i = 0; i < 150; i++) { // users.add(new User("admin" + i, "admin", true)); // } // userService.save(users); // // // List<Role> roles = new ArrayList<>(); // for (int i = 0; i < 200; i++) { // roles.add(new Role("admin" + i)); // } // // roleService.save(roles); } } else { for (EntityType<?> entityType : set) { if (entityType.getBindableJavaType().getName().equals(tableDto.getId())) { Field[] fields = entityType.getBindableJavaType().getDeclaredFields(); for (Field field : fields) { TableFieldsDto dto = new TableFieldsDto( entityType.getBindableJavaType().getName() + field.getName(), field.getName(), field.toString()); result.add(dto); } } } } return result; }
From source file:org.grouplens.grapht.util.FieldProxy.java
/** * Resolve this proxy into a {@link Field} instance. * @return The {@link Field} represented by this proxy. * @throws ClassNotFoundException If the proxy's declaring type cannot be resolved. * @throws NoSuchFieldException If the field does not exist on the declaring type. *//* w ww. j a v a2 s . c om*/ public Field resolve() throws ClassNotFoundException, NoSuchFieldException { Field cachedField = field; if (cachedField == null) { Class<?> cls = declaringClass.resolve(); field = cachedField = cls.getDeclaredField(fieldName); } // REVIEW Do we want to test equality or assignability? if (!cachedField.getType().equals(fieldType.resolve())) { throw new NoSuchFieldException("type mismatch on " + cachedField.toString()); } return cachedField; }
From source file:net.mindengine.blogix.db.readers.ObjectReader.java
private void setFieldValue(T objectInstance, Field field, String fieldValue) { Object convertedFieldValue = convertFieldValue(fieldValue, field); field.setAccessible(true);// w w w .j av a2 s .co m try { field.set(objectInstance, convertedFieldValue); } catch (Exception e) { throw new RuntimeException("Cannot set value to field " + field.toString()); } }