public int compare(MappingWithDirection o1, MappingWithDirection o2) { Class<?> class1 = o1.mapping.sideA.isAssignableFrom(target) ? o1.mapping.sideA : o1.mapping.sideB; Class<?> class2 = o2.mapping.sideA.isAssignableFrom(target) ? o2.mapping.sideA : o2.mapping.sideB; return class1 == class2 ? 0 : class1.isAssignableFrom(class2) ? -1 : 1; }
public final boolean assertInstanceOf(String $label, Class<?> $klass, Object $obj) { if ($obj == null) { $unitFailures++; $log.warn( messageFail( $label, "null is never an instance of anything, and certainly not " + $klass + "."), new AssertionFailed()); return false; } try { $klass.cast($obj); $log.debug( messagePass( $label, "\"" + $obj.getClass().getCanonicalName() + "\" is an instance of \"" + $klass.getCanonicalName() + "\"")); return true; } catch (ClassCastException $e) { $unitFailures++; $log.warn(messageFail($label, $e.getMessage() + "."), new AssertionFailed()); return false; } }
private void generateView( Map<String, DesignDocument.View> views, Method me, Class<?> handledType) { String name = me.getName(); if (!name.startsWith("findBy") && !name.equals("getAll")) { throw new ViewGenerationException( String.format( "The method: %s in %s annotated with GenerateView does not conform to the naming convention of 'findByXxxx'", name, me.getDeclaringClass())); } Class<?> type = resolveReturnType(me); if (type == null) { if (handledType != null) { type = handledType; } else { throw new ViewGenerationException( "Could not resolve return type for method: %s in %s", me.getName(), me.getDeclaringClass()); } } String typeDiscriminator = resolveTypeDiscriminator(type); if (name.equals("getAll")) { if (typeDiscriminator.length() < 1) { throw new ViewGenerationException( String.format( "Cannot generate 'all' view for %s. No type discriminator could be resolved. Try annotate unique field(s) with @TypeDiscriminator", type.getDeclaringClass())); } views.put("all", generateAllView(typeDiscriminator)); return; } String finderName = name.substring(6); String fieldName = resolveFieldName(me, finderName); Method getter = findMethod(type, "get" + fieldName); if (getter == null) { // try pluralis fieldName += "s"; getter = findMethod(type, "get" + fieldName); } if (getter == null) { throw new ViewGenerationException( "Could not generate view for method %s. No get method found for property %s in %s", name, name.substring(6), type); } fieldName = firstCharToLowerCase(fieldName); DesignDocument.View view; if (isIterable(getter.getReturnType())) { view = generateFindByIterableView(fieldName, typeDiscriminator); } else { view = generateFindByView(fieldName, typeDiscriminator); } views.put("by_" + firstCharToLowerCase(finderName), view); }
private String readVersionFromManifest(Class<?> api) throws IOException { String className = api.getSimpleName() + ".class"; URL resource = api.getResource(className); log.trace("get resource for {} -> {} -> {}", new Object[] {className, pathOf(api), resource}); if (resource == null) return null; String version = null; String classPath = resource.toString(); if (classPath.startsWith("jar:")) version = version(classPath.substring(0, classPath.lastIndexOf("!") + 1)); if (version == null && classPath.endsWith(pathOf(api) + ".class")) version = version(classPath.substring(0, classPath.length() - pathOf(api).length() - 7)); if (version == null && classPath.contains("/WEB-INF/")) version = version(classPath.substring(0, classPath.lastIndexOf("/WEB-INF/"))); log.debug("version {} for {} from {}.", version, api, classPath); return version; }
private DesignDocument.View loadViewFromFile( Map<String, DesignDocument.View> views, View input, Class<?> repositoryClass) { try { InputStream in = repositoryClass.getResourceAsStream(input.file()); if (in == null) { throw new FileNotFoundException("Could not load view file with path: " + input.file()); } String json = IOUtils.toString(in, "UTF-8"); return mapper().readValue(json.replaceAll("\n", ""), DesignDocument.View.class); } catch (Exception e) { throw Exceptions.propagate(e); } }
public String getVersion(Class<?> api) { log.trace("getVersion for {}", api); Package pkg = api.getPackage(); log.trace(" -> {}/{}", pkg.getImplementationVersion(), pkg.getSpecificationVersion()); String version = pkg.getSpecificationVersion(); if (version == null) version = pkg.getImplementationVersion(); if (version == null) { try { version = readVersionFromManifest(api); } catch (IOException e) { log.error("Could not extract version for " + api, e); return null; } } return version; }
private String resolveTypeDiscriminator(final Class<?> persistentType) { final List<String> discrimintators = new ArrayList<String>(); TypeDiscriminator td = persistentType.getAnnotation(TypeDiscriminator.class); if (td != null) { if (td.value().length() == 0) { throw new ViewGenerationException( String.format( "@TypeDiscriminator declared on type level must specify custom discriminator condition", persistentType)); } if (hasTypeDiscriminatorFieldOrMethod(persistentType)) { throw new ViewGenerationException( String.format( "@TypeDiscriminator declared on type level may not be combined with @TypeDiscriminator in fields or on methods", persistentType)); } return td.value(); } eachField( persistentType, new Predicate<Field>() { public boolean apply(Field input) { if (hasAnnotation(input, TypeDiscriminator.class)) { discrimintators.add("doc." + input.getName()); } return false; } }); eachMethod( persistentType, new Predicate<Method>() { public boolean apply(Method input) { if (hasAnnotation(input, TypeDiscriminator.class)) { discrimintators.add("doc." + firstCharToLowerCase(input.getName().substring(3))); } return true; } }); return Joiner.join(discrimintators, " && "); }
private String pathOf(Class<?> api) { return api.getCanonicalName().replace('.', '/'); }