Java Code Examples for org.apache.bcel.Repository
The following examples show how to use
org.apache.bcel.Repository. These examples are extracted from open source projects.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: uyuni Source File: FindStrings.java License: GNU General Public License v2.0 | 6 votes |
/** * Main class, to find strings in class files. * @param args Arguments to program. */ public static void main(String[] args) { try { JavaClass clazz = Repository.lookupClass(args[0]); ConstantPool cp = clazz.getConstantPool(); Constant[] consts = cp.getConstantPool(); for (int i = 0; i < consts.length; i++) { if (consts[i] instanceof ConstantString) { System.out.println("Found String: " + ((ConstantString)consts[i]).getBytes(cp)); } } } catch (Exception e) { e.printStackTrace(); } }
Example 2
Source Project: JQF Source File: ParserTest.java License: BSD 2-Clause "Simplified" License | 6 votes |
@Fuzz public void verifyJavaClass(@From(JavaClassGenerator.class) JavaClass javaClass) throws IOException { try { Repository.addClass(javaClass); Verifier verifier = StatelessVerifierFactory.getVerifier(javaClass.getClassName()); VerificationResult result; result = verifier.doPass1(); assumeThat(result.getMessage(), result.getStatus(), is(VerificationResult.VERIFIED_OK)); result = verifier.doPass2(); assumeThat(result.getMessage(), result.getStatus(), is(VerificationResult.VERIFIED_OK)); for (int i = 0; i < javaClass.getMethods().length; i++) { result = verifier.doPass3a(i); assumeThat(result.getMessage(), result.getStatus(), is(VerificationResult.VERIFIED_OK)); } } finally { Repository.clearCache(); } }
Example 3
Source Project: ApkToolPlus Source File: Pass2Verifier.java License: Apache License 2.0 | 6 votes |
/** * Pass 2 is the pass where static properties of the * class file are checked without looking into "Code" * arrays of methods. * This verification pass is usually invoked when * a class is resolved; and it may be possible that * this verification pass has to load in other classes * such as superclasses or implemented interfaces. * Therefore, Pass 1 is run on them.<BR> * Note that most referenced classes are <B>not</B> loaded * in for verification or for an existance check by this * pass; only the syntactical correctness of their names * and descriptors (a.k.a. signatures) is checked.<BR> * Very few checks that conceptually belong here * are delayed until pass 3a in JustIce. JustIce does * not only check for syntactical correctness but also * for semantical sanity - therefore it needs access to * the "Code" array of methods in a few cases. Please * see the pass 3a documentation, too. * * @see org.apache.bcel.verifier.statics.Pass3aVerifier */ public VerificationResult do_verify(){ VerificationResult vr1 = myOwner.doPass1(); if (vr1.equals(VerificationResult.VR_OK)){ // For every method, we could have information about the local variables out of LocalVariableTable attributes of // the Code attributes. localVariablesInfos = new LocalVariablesInfo[Repository.lookupClass(myOwner.getClassName()).getMethods().length]; VerificationResult vr = VerificationResult.VR_OK; // default. try{ constant_pool_entries_satisfy_static_constraints(); field_and_method_refs_are_valid(); every_class_has_an_accessible_superclass(); final_methods_are_not_overridden(); } catch (ClassConstraintException cce){ vr = new VerificationResult(VerificationResult.VERIFIED_REJECTED, cce.getMessage()); } return vr; } else return VerificationResult.VR_NOTYET; }
Example 4
Source Project: commons-bcel Source File: Pass3aVerifier.java License: Apache License 2.0 | 6 votes |
/** Checks if the constraints of operands of the said instruction(s) are satisfied. */ @Override public void visitINVOKESTATIC(final INVOKESTATIC o) { try { // INVOKESTATIC is a LoadClass; the Class where the referenced method is declared in, // is therefore resolved/verified. // INVOKESTATIC is an InvokeInstruction, the argument and return types are resolved/verified, // too. So are the allowed method names. final String classname = o.getClassName(constantPoolGen); final JavaClass jc = Repository.lookupClass(classname); final Method m = getMethodRecursive(jc, o); if (m == null) { constraintViolated(o, "Referenced method '"+o.getMethodName(constantPoolGen)+"' with expected signature '"+ o.getSignature(constantPoolGen) +"' not found in class '"+jc.getClassName()+"'."); } else if (! (m.isStatic())) { // implies it's not abstract, verified in pass 2. constraintViolated(o, "Referenced method '"+o.getMethodName(constantPoolGen)+"' has ACC_STATIC unset."); } } catch (final ClassNotFoundException e) { // FIXME: maybe not the best way to handle this throw new AssertionViolatedException("Missing class: " + e, e); } }
Example 5
Source Project: spotbugs Source File: Util.java License: GNU Lesser General Public License v2.1 | 6 votes |
/** * Determine the outer class of obj. * * @param obj * @return JavaClass for outer class, or null if obj is not an outer class * @throws ClassNotFoundException */ @CheckForNull public static JavaClass getOuterClass(JavaClass obj) throws ClassNotFoundException { for (Attribute a : obj.getAttributes()) { if (a instanceof InnerClasses) { for (InnerClass ic : ((InnerClasses) a).getInnerClasses()) { if (obj.getClassNameIndex() == ic.getInnerClassIndex()) { // System.out.println("Outer class is " + // ic.getOuterClassIndex()); ConstantClass oc = (ConstantClass) obj.getConstantPool().getConstant(ic.getOuterClassIndex()); String ocName = oc.getBytes(obj.getConstantPool()); return Repository.lookupClass(ocName); } } } } return null; }
Example 6
Source Project: commons-bcel Source File: Pass3aVerifier.java License: Apache License 2.0 | 6 votes |
/** Checks if the constraints of operands of the said instruction(s) are satisfied. */ @Override public void visitINVOKEVIRTUAL(final INVOKEVIRTUAL o) { try { // INVOKEVIRTUAL is a LoadClass; the Class where the referenced method is declared in, // is therefore resolved/verified. // INVOKEVIRTUAL is an InvokeInstruction, the argument and return types are resolved/verified, // too. So are the allowed method names. final String classname = o.getClassName(constantPoolGen); final JavaClass jc = Repository.lookupClass(classname); final Method m = getMethodRecursive(jc, o); if (m == null) { constraintViolated(o, "Referenced method '"+o.getMethodName(constantPoolGen)+"' with expected signature '"+ o.getSignature(constantPoolGen)+"' not found in class '"+jc.getClassName()+"'."); } if (! (jc.isClass())) { constraintViolated(o, "Referenced class '"+jc.getClassName()+"' is an interface, but not a class as expected."); } } catch (final ClassNotFoundException e) { // FIXME: maybe not the best way to handle this throw new AssertionViolatedException("Missing class: " + e, e); } }
Example 7
Source Project: spotbugs Source File: Hierarchy.java License: GNU Lesser General Public License v2.1 | 6 votes |
/** * Find a field with given name defined in given class. * * @param className * the name of the class * @param fieldName * the name of the field * @return the Field, or null if no such field could be found */ public static Field findField(String className, String fieldName) throws ClassNotFoundException { JavaClass jclass = Repository.lookupClass(className); while (jclass != null) { Field[] fieldList = jclass.getFields(); for (Field field : fieldList) { if (field.getName().equals(fieldName)) { return field; } } jclass = jclass.getSuperClass(); } return null; }
Example 8
Source Project: spotbugs Source File: DeepSubtypeAnalysis.java License: GNU Lesser General Public License v2.1 | 6 votes |
public static double isDeepSerializable(@DottedClassName String refSig) throws ClassNotFoundException { if (storedException != null) { throw storedException; } if (isPrimitiveComponentClass(refSig)) { if (DEBUG) { System.out.println("regSig \"" + refSig + "\" is primitive component class"); } return 1.0; } String refName = getComponentClass(refSig); if (Values.DOTTED_JAVA_LANG_OBJECT.equals(refName)) { return 0.99; } JavaClass refJavaClass = Repository.lookupClass(refName); return isDeepSerializable(refJavaClass); }
Example 9
Source Project: cacheonix-core Source File: ReferenceDAO.java License: GNU Lesser General Public License v2.1 | 6 votes |
/** * Adds a reference to a field. * @param aFieldRef the field reference. */ public void addFieldReference(FieldReference aFieldRef) { final String className = aFieldRef.getClassName(); JavaClass javaClass = Repository.lookupClass(className); final String fieldName = aFieldRef.getName(); // search up the class hierarchy for the class containing the // method definition. FieldDefinition fieldDef = null; while ((javaClass != null) && (fieldDef == null)) { final JavaClassDefinition javaClassDef = (JavaClassDefinition) mJavaClasses.get(javaClass); if (javaClassDef != null) { fieldDef = javaClassDef.findFieldDef(fieldName); if (fieldDef != null) { fieldDef.addReference(aFieldRef); } } //search the parent javaClass = javaClass.getSuperClass(); } }
Example 10
Source Project: cacheonix-core Source File: ReferenceDAO.java License: GNU Lesser General Public License v2.1 | 6 votes |
/** * Finds the definition of the field of a field reference. * @param aFieldRef the reference to a field. * @return the definition of the field for aFieldRef. */ public FieldDefinition findFieldDef(FieldReference aFieldRef) { final String className = aFieldRef.getClassName(); JavaClass javaClass = Repository.lookupClass(className); final String fieldName = aFieldRef.getName(); // search up the class hierarchy for the class containing the // method definition. FieldDefinition result = null; while ((javaClass != null) && (result == null)) { final JavaClassDefinition javaClassDef = (JavaClassDefinition) mJavaClasses.get(javaClass); if (javaClassDef != null) { result = javaClassDef.findFieldDef(fieldName); } //search the parent javaClass = javaClass.getSuperClass(); } return result; }
Example 11
Source Project: cacheonix-core Source File: JavaClassDefinition.java License: GNU Lesser General Public License v2.1 | 6 votes |
/** * Finds the narrowest method that is compatible with a method. * An invocation of the given method can be resolved as an invocation * of the narrowest method. * @param aClassName the class for the method. * @param aMethodName the name of the method. * @param aArgTypes the types for the method. * @return the narrowest compatible method. */ public MethodDefinition findNarrowestMethod( String aClassName, String aMethodName, Type[] aArgTypes) { MethodDefinition result = null; final String javaClassName = mJavaClass.getClassName(); if (Repository.instanceOf(aClassName, javaClassName)) { // check all for (int i = 0; i < mMethodDefs.length; i++) { // TODO: check access privileges if (mMethodDefs[i].isCompatible(aMethodName, aArgTypes)) { if (result == null) { result = mMethodDefs[i]; } //else if (mMethodDefs[i].isAsNarrow(result)) { else if (result.isCompatible(mMethodDefs[i])) { result = mMethodDefs[i]; } } } } return result; }
Example 12
Source Project: cacheonix-core Source File: ClassFileSetCheck.java License: GNU Lesser General Public License v2.1 | 6 votes |
/** @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor */ public void visitSet(Set aSet) { // register the JavaClasses in the Repository Repository.clearCache(); Iterator it = aSet.iterator(); while (it.hasNext()) { final JavaClass javaClass = (JavaClass) it.next(); Repository.addClass(javaClass); } // visit the visitors it = getObjectSetVisitors().iterator(); while (it.hasNext()) { final IObjectSetVisitor visitor = (IObjectSetVisitor) it.next(); visitor.visitSet(aSet); } }
Example 13
Source Project: contribution Source File: ReferenceDAO.java License: GNU Lesser General Public License v2.1 | 6 votes |
/** * Adds a reference to a field. * @param aFieldRef the field reference. */ public void addFieldReference(FieldReference aFieldRef) { final String className = aFieldRef.getClassName(); JavaClass javaClass = Repository.lookupClass(className); final String fieldName = aFieldRef.getName(); // search up the class hierarchy for the class containing the // method definition. FieldDefinition fieldDef = null; while ((javaClass != null) && (fieldDef == null)) { final JavaClassDefinition javaClassDef = (JavaClassDefinition) mJavaClasses.get(javaClass); if (javaClassDef != null) { fieldDef = javaClassDef.findFieldDef(fieldName); if (fieldDef != null) { fieldDef.addReference(aFieldRef); } } //search the parent javaClass = javaClass.getSuperClass(); } }
Example 14
Source Project: contribution Source File: ReferenceDAO.java License: GNU Lesser General Public License v2.1 | 6 votes |
/** * Finds the definition of the field of a field reference. * @param aFieldRef the reference to a field. * @return the definition of the field for aFieldRef. */ public FieldDefinition findFieldDef(FieldReference aFieldRef) { final String className = aFieldRef.getClassName(); JavaClass javaClass = Repository.lookupClass(className); final String fieldName = aFieldRef.getName(); // search up the class hierarchy for the class containing the // method definition. FieldDefinition result = null; while ((javaClass != null) && (result == null)) { final JavaClassDefinition javaClassDef = (JavaClassDefinition) mJavaClasses.get(javaClass); if (javaClassDef != null) { result = javaClassDef.findFieldDef(fieldName); } //search the parent javaClass = javaClass.getSuperClass(); } return result; }
Example 15
Source Project: contribution Source File: JavaClassDefinition.java License: GNU Lesser General Public License v2.1 | 6 votes |
/** * Finds the narrowest method that is compatible with a method. * An invocation of the given method can be resolved as an invocation * of the narrowest method. * @param aClassName the class for the method. * @param aMethodName the name of the method. * @param aArgTypes the types for the method. * @return the narrowest compatible method. */ public MethodDefinition findNarrowestMethod( String aClassName, String aMethodName, Type[] aArgTypes) { MethodDefinition result = null; final String javaClassName = mJavaClass.getClassName(); if (Repository.instanceOf(aClassName, javaClassName)) { // check all for (int i = 0; i < mMethodDefs.length; i++) { // TODO: check access privileges if (mMethodDefs[i].isCompatible(aMethodName, aArgTypes)) { if (result == null) { result = mMethodDefs[i]; } //else if (mMethodDefs[i].isAsNarrow(result)) { else if (result.isCompatible(mMethodDefs[i])) { result = mMethodDefs[i]; } } } } return result; }
Example 16
Source Project: spacewalk Source File: FindStrings.java License: GNU General Public License v2.0 | 6 votes |
/** * Main class, to find strings in class files. * @param args Arguments to program. */ public static void main(String[] args) { try { JavaClass clazz = Repository.lookupClass(args[0]); ConstantPool cp = clazz.getConstantPool(); Constant[] consts = cp.getConstantPool(); for (int i = 0; i < consts.length; i++) { if (consts[i] instanceof ConstantString) { System.out.println("Found String: " + ((ConstantString)consts[i]).getBytes(cp)); } } } catch (Exception e) { e.printStackTrace(); } }
Example 17
Source Project: commons-bcel Source File: TransitiveHull.java License: Apache License 2.0 | 6 votes |
private void add(String class_name) { class_name = class_name.replace('/', '.'); for (final String anIgnored : ignored) { if (Pattern.matches(anIgnored, class_name)) { return; } } try { final JavaClass clazz = Repository.lookupClass(class_name); if (set.add(clazz)) { queue.enqueue(clazz); } } catch (final ClassNotFoundException e) { throw new IllegalStateException("Missing class: " + e.toString()); } }
Example 18
Source Project: commons-bcel Source File: TransitiveHull.java License: Apache License 2.0 | 6 votes |
public static void main(final String[] argv) { JavaClass java_class; try { if (argv.length == 0) { System.err.println("transitive: No input files specified"); } else { if ((java_class = Repository.lookupClass(argv[0])) == null) { java_class = new ClassParser(argv[0]).parse(); } final TransitiveHull hull = new TransitiveHull(java_class); hull.start(); System.out.println(Arrays.asList(hull.getClassNames())); } } catch (final Exception e) { e.printStackTrace(); } }
Example 19
Source Project: ApkToolPlus Source File: ObjectType.java License: Apache License 2.0 | 5 votes |
/** * If "this" doesn't reference a class, it references an interface * or a non-existant entity. */ public boolean referencesClass(){ JavaClass jc = Repository.lookupClass(class_name); if (jc == null) return false; else return jc.isClass(); }
Example 20
Source Project: ApkToolPlus Source File: ObjectType.java License: Apache License 2.0 | 5 votes |
/** * If "this" doesn't reference an interface, it references a class * or a non-existant entity. */ public boolean referencesInterface(){ JavaClass jc = Repository.lookupClass(class_name); if (jc == null) return false; else return !jc.isClass(); }
Example 21
Source Project: commons-bcel Source File: VerifyDialog.java License: Apache License 2.0 | 5 votes |
/** Machine-generated. */ public void pass4Button_ActionPerformed( final java.awt.event.ActionEvent actionEvent ) { pass2Button_ActionPerformed(actionEvent); Color color = Color.green; final Verifier v = VerifierFactory.getVerifier(class_name); VerificationResult vr = v.doPass2(); if (vr.getStatus() == VerificationResult.VERIFIED_OK) { JavaClass jc = null; try { jc = Repository.lookupClass(class_name); final int nr = jc.getMethods().length; for (int i = 0; i < nr; i++) { vr = v.doPass3b(i); if (vr.getStatus() != VerificationResult.VERIFIED_OK) { color = Color.red; break; } } } catch (final ClassNotFoundException ex) { // FIXME: report the error ex.printStackTrace(); } } else { color = Color.yellow; } getPass3Panel().setBackground(color); getPass3Panel().repaint(); }
Example 22
Source Project: ApkToolPlus Source File: Pass2Verifier.java License: Apache License 2.0 | 5 votes |
/** * Ensures that <B>final</B> methods are not overridden. * <B>Precondition to run this method: * constant_pool_entries_satisfy_static_constraints() and * every_class_has_an_accessible_superclass() have to be invoked before * (in that order).</B> * * @throws ClassConstraintException otherwise. * @see #constant_pool_entries_satisfy_static_constraints() * @see #every_class_has_an_accessible_superclass() */ private void final_methods_are_not_overridden(){ HashMap<String, String> hashmap = new HashMap<String, String>(); JavaClass jc = Repository.lookupClass(myOwner.getClassName()); int supidx = -1; while (supidx != 0){ supidx = jc.getSuperclassNameIndex(); Method[] methods = jc.getMethods(); for (int i=0; i<methods.length; i++){ String name_and_sig = (methods[i].getName()+methods[i].getSignature()); if (hashmap.containsKey(name_and_sig)){ if (methods[i].isFinal()){ throw new ClassConstraintException("Method '"+name_and_sig+"' in class '"+hashmap.get(name_and_sig)+"' overrides the final (not-overridable) definition in class '"+jc.getClassName()+"'."); } else{ if (!methods[i].isStatic()){ // static methods don't inherit hashmap.put(name_and_sig, jc.getClassName()); } } } else{ if (!methods[i].isStatic()){ // static methods don't inherit hashmap.put(name_and_sig, jc.getClassName()); } } } jc = Repository.lookupClass(jc.getSuperclassName()); // Well, for OBJECT this returns OBJECT so it works (could return anything but must not throw an Exception). } }
Example 23
Source Project: ApkToolPlus Source File: Pass2Verifier.java License: Apache License 2.0 | 5 votes |
/** * Ensures that the constant pool entries satisfy the static constraints * as described in The Java Virtual Machine Specification, 2nd Edition. * * @throws ClassConstraintException otherwise. */ private void constant_pool_entries_satisfy_static_constraints(){ // Most of the consistency is handled internally by BCEL; here // we only have to verify if the indices of the constants point // to constants of the appropriate type and such. JavaClass jc = Repository.lookupClass(myOwner.getClassName()); new CPESSC_Visitor(jc); // constructor implicitely traverses jc }
Example 24
Source Project: ApkToolPlus Source File: Pass1Verifier.java License: Apache License 2.0 | 5 votes |
/** Used to load in and return the myOwner-matching JavaClass object when needed. Avoids loading in a class file when it's not really needed! */ private JavaClass getJavaClass(){ if (jc == null){ jc = Repository.lookupClass(myOwner.getClassName()); } return jc; }
Example 25
Source Project: commons-bcel Source File: VerifierAppFrame.java License: Apache License 2.0 | 5 votes |
synchronized void pass3bJList_valueChanged( final ListSelectionEvent e ) { if (e.getValueIsAdjusting()) { return; } final Verifier v = VerifierFactory.getVerifier(current_class); final StringBuilder all3bmsg = new StringBuilder(); boolean all3bok = true; boolean rejected = false; for (int i = 0; i < pass3bJList.getModel().getSize(); i++) { if (pass3bJList.isSelectedIndex(i)) { final VerificationResult vr = v.doPass3b(i); if (vr.getStatus() == VerificationResult.VERIFIED_REJECTED) { all3bok = false; rejected = true; } JavaClass jc = null; try { jc = Repository.lookupClass(v.getClassName()); all3bmsg.append("Method '").append(jc.getMethods()[i]).append("': ") .append(vr.getMessage().replace('\n', ' ')).append("\n\n"); } catch (final ClassNotFoundException ex) { // FIXME: handle the error ex.printStackTrace(); } } } pass3bTextPane.setText(all3bmsg.toString()); pass3bTextPane.setBackground(all3bok ? Color.green : (rejected ? Color.red : Color.yellow)); }
Example 26
Source Project: ApkToolPlus Source File: BCELifier.java License: Apache License 2.0 | 5 votes |
/** Default main method */ public static void main(String[] argv) throws Exception { JavaClass java_class; String name = argv[0]; if((java_class = Repository.lookupClass(name)) == null) java_class = new ClassParser(name).parse(); // May throw IOException BCELifier bcelifier = new BCELifier(java_class, System.out); bcelifier.start(); }
Example 27
Source Project: spotbugs Source File: ReadReturnShouldBeChecked.java License: GNU Lesser General Public License v2.1 | 5 votes |
private boolean isBufferedInputStream() { try { if (lastCallClass.startsWith("[")) { return false; } return Repository.instanceOf(lastCallClass, "java.io.BufferedInputStream"); } catch (ClassNotFoundException e) { return false; } }
Example 28
Source Project: spotbugs Source File: RedundantInterfaces.java License: GNU Lesser General Public License v2.1 | 5 votes |
@Override public void visitClassContext(ClassContext classContext) { JavaClass obj = classContext.getJavaClass(); String superClassName = obj.getSuperclassName(); if (Values.DOTTED_JAVA_LANG_OBJECT.equals(superClassName)) { return; } String[] interfaceNames = obj.getInterfaceNames(); if ((interfaceNames == null) || (interfaceNames.length == 0)) { return; } try { JavaClass superObj = obj.getSuperClass(); SortedSet<String> redundantInfNames = new TreeSet<>(); for (String interfaceName : interfaceNames) { if (!"java.io.Serializable".equals(interfaceName)) { JavaClass inf = Repository.lookupClass(interfaceName); if (superObj.instanceOf(inf)) { redundantInfNames.add(inf.getClassName()); } } } if (redundantInfNames.size() > 0) { BugInstance bug = new BugInstance(this, "RI_REDUNDANT_INTERFACES", LOW_PRIORITY).addClass(obj); for (String redundantInfName : redundantInfNames) { bug.addClass(redundantInfName).describe("INTERFACE_TYPE"); } bugReporter.reportBug(bug); } } catch (ClassNotFoundException cnfe) { bugReporter.reportMissingClass(cnfe); } }
Example 29
Source Project: tutorials Source File: ViewBytecodeUnitTest.java License: MIT License | 5 votes |
@Test public void whenUsingBCEL_thenReadBytecode() throws ClassNotFoundException { JavaClass objectClazz = Repository.lookupClass("java.lang.Object"); assertEquals(objectClazz.getClassName(), "java.lang.Object"); assertEquals(objectClazz.getMethods().length, 14); assertTrue(objectClazz.toString().contains("public class java.lang.Object")); }
Example 30
Source Project: spotbugs Source File: BadAppletConstructor.java License: GNU Lesser General Public License v2.1 | 5 votes |
public BadAppletConstructor(BugReporter bugReporter) { this.bugReporter = bugReporter; JavaClass appletClass = null; try { appletClass = Repository.lookupClass("java.applet.Applet"); } catch (ClassNotFoundException cnfe) { bugReporter.reportMissingClass(cnfe); } this.appletClass = appletClass; }