private static Object lookupBinaryType(Object binding) throws Exception { Object lookupEnvironment = field(binding, "environment"); Object compoundClassName = field(binding, "compoundName"); Object nameEnvironment = field(lookupEnvironment, "nameEnvironment"); Object nameEnvironmentAnswer = method(nameEnvironment, "findType", new Class[]{char[][].class}, compoundClassName); Object binaryType = method(nameEnvironmentAnswer, "getBinaryType", new Class[0]); return binaryType; }
private static Object lookupBinaryType(Object binding) throws Exception { Object lookupEnvironment = field(binding, "environment"); Object compoundClassName = field(binding, "compoundName"); Object nameEnvironment = field(lookupEnvironment, "nameEnvironment"); Object nameEnvironmentAnswer = method(nameEnvironment, "findType", new Class<?>[]{char[][].class}, compoundClassName); Object binaryType = method(nameEnvironmentAnswer, "getBinaryType", new Class<?>[0]); return binaryType; }
private static List<Object> findBinaryTypeOrder(Object binding) throws Exception { Object binaryType = lookupBinaryType(binding); final Object[] sortedMethods = (Object[]) method(binaryType, "getMethods"); final Object[] sortedFields = (Object[]) method(binaryType, "getFields"); if (sortedFields != null) { sortedElements.addAll(Arrays.asList(sortedFields)); final Object[] sortedTypes = (Object[]) method(binaryType, "getMemberTypes", new Class[0]); if (sortedTypes != null) { sortedElements.addAll(Arrays.asList(sortedTypes)); Class<?> elementClass = sortedElement.getClass(); if (binaryMethod.isAssignableFrom(elementClass)) { char[] selector = (char[]) method(sortedElement, "getSelector"); Object[] foundBindings = (Object[]) method(binding, "getMethods", new Class[]{char[].class}, selector); if (foundBindings == null || foundBindings.length == 0) { continue; bindings.add(foundBindings[0]); } else { char[] idescriptor = (char[]) method(sortedElement, "getMethodDescriptor"); for (Object foundBinding : foundBindings) { char[] descriptor = (char[]) method(foundBinding, "signature"); if (descriptor == null && idescriptor == null || Arrays.equals(descriptor, idescriptor)) { bindings.add(foundBinding); char[] selector = (char[]) method(sortedElement, "getName"); Object foundField = method(binding, "getField", new Class[]{char[].class, boolean.class}, selector, true); if (foundField != null) {
private static List<Object> findBinaryTypeOrder(Object binding) throws Exception { Object binaryType = lookupBinaryType(binding); final Object[] sortedMethods = (Object[]) method(binaryType, "getMethods"); final Object[] sortedFields = (Object[]) method(binaryType, "getFields"); if (sortedFields != null) { sortedElements.addAll(Arrays.asList(sortedFields)); final Object[] sortedTypes = (Object[]) method(binaryType, "getMemberTypes", new Class<?>[0]); if (sortedTypes != null) { sortedElements.addAll(Arrays.asList(sortedTypes)); Class<?> elementClass = sortedElement.getClass(); if (binaryMethod.isAssignableFrom(elementClass)) { char[] selector = (char[]) method(sortedElement, "getSelector"); Object[] foundBindings = (Object[]) method(binding, "getMethods", new Class<?>[]{char[].class}, selector); if (foundBindings == null || foundBindings.length == 0) { continue; bindings.add(foundBindings[0]); } else { char[] idescriptor = (char[]) method(sortedElement, "getMethodDescriptor"); for (Object foundBinding : foundBindings) { char[] descriptor = (char[]) method(foundBinding, "signature"); if (descriptor == null && idescriptor == null || Arrays.equals(descriptor, idescriptor)) { bindings.add(foundBinding); char[] selector = (char[]) method(sortedElement, "getName"); Object foundField = method(binding, "getField", new Class<?>[]{char[].class, boolean.class}, selector, true); if (foundField != null) {
private static char[] getSource(Element element) throws Exception { /* * Binding binding = ((ElementImpl)element)._binding; char[] source = null; if (binding * instanceof MethodBinding) { source = ((MethodBinding) * binding).sourceMethod().compilationResult.getCompilationUnit().getContents(); } else if * (binding instanceof SourceTypeBinding) { source = * ((SourceTypeBinding)binding).scope.referenceContext * .compilationResult.compilationUnit.getContents(); } return source; */ Object binding = field(element, "_binding"); Class<?> methodBindingClass = Class.forName("org.eclipse.jdt.internal.compiler.lookup.MethodBinding"); Class<?> referenceBindingClass = Class.forName("org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding"); char[] source = null; if (methodBindingClass.isAssignableFrom(binding.getClass())) { Object sourceMethod = method(binding, "sourceMethod"); if (sourceMethod == null) { return null; } source = (char[]) method(method(field(sourceMethod, "compilationResult"), "getCompilationUnit"), "getContents"); } else if (referenceBindingClass.isAssignableFrom(binding.getClass())) { source = (char[]) method(field(field(field(field(binding, "scope"), "referenceContext"), "compilationResult"), "compilationUnit"), "getContents"); } return source; }
@Override public String getMethodBody(ProcessingEnvironment env, ExecutableElement method) { try { char[] source = getSource(method); if (source == null) { return null; } /* * AbstractMethodDeclaration decl = * ((MethodBinding)(((ElementImpl)method)._binding)).sourceMethod(); int bodyStart = * decl.bodyStart; int bodyEnd = decl.bodyEnd; */ Object decl = method(field(method, "_binding"), "sourceMethod"); int bodyStart = (int) field(decl, "bodyStart"); int bodyEnd = (int) field(decl, "bodyEnd"); int length = bodyEnd - bodyStart; char[] target = new char[length]; System.arraycopy(source, bodyStart, target, 0, length); return new String(target); } catch (Exception e) { return ElementUtils.printException(e); } }